Skip to content

Commit dc3abb8

Browse files
committed
feat(docusaurus-website): auto populate webpack aliases
1 parent f20780e commit dc3abb8

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

modules/docusaurus-website/src/get-default-config.ts

+68-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'fs';
12
import {resolve, dirname} from 'path';
23
import lightCodeTheme from 'prism-react-renderer/themes/nightOwlLight';
34
import darkCodeTheme from 'prism-react-renderer/themes/nightOwl';
@@ -28,6 +29,32 @@ function normalizeSidebarItem(item: SidebarItem | string): SidebarItem {
2829
return item;
2930
}
3031

32+
function getAlias(packageRoot: string, target: {[moduleName: string]: string}) {
33+
const packageJsonFile = resolve(packageRoot, 'package.json');
34+
if (fs.existsSync(packageJsonFile)) {
35+
const packageInfo = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8'));
36+
target[packageInfo.name] = resolve(packageRoot, 'src');
37+
}
38+
return null;
39+
}
40+
41+
function getAliases(root: string): {[moduleName: string]: string} {
42+
const result: {[moduleName: string]: string} = {};
43+
const parentPath = resolve(root, './modules');
44+
45+
if (fs.existsSync(parentPath)) {
46+
// monorepo
47+
for (const item of fs.readdirSync(parentPath)) {
48+
const itemPath = resolve(parentPath, item);
49+
getAlias(itemPath, result);
50+
}
51+
} else {
52+
getAlias(root, result);
53+
}
54+
55+
return result;
56+
}
57+
3158
export type OcularWebsiteConfig = {
3259
/** Name of the project */
3360
projectName: string;
@@ -42,9 +69,16 @@ export type OcularWebsiteConfig = {
4269
siteUrl: string;
4370

4471
/** Custom webpack config */
45-
webpackConfig: object;
72+
webpackConfig?: object;
73+
74+
/** Path to repo root, relative to the current directory
75+
* @default ".."
76+
*/
77+
rootDir?: string;
4678

47-
/** Path to documentation pages (.md and .mdx), relative to the current directory */
79+
/** Path to documentation pages (.md and .mdx), relative to the current directory
80+
* @default "../docs"
81+
*/
4882
docsDir?: string;
4983
/** Documentation table of contents */
5084
docsTableOfContents: SidebarItem[];
@@ -60,17 +94,30 @@ export type OcularWebsiteConfig = {
6094

6195
export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
6296
const siteUrl = new URL(config.siteUrl);
97+
const {
98+
projectName,
99+
tagline,
100+
repoUrl,
101+
rootDir = '..',
102+
docsDir = '../docs',
103+
search = false,
104+
docsTableOfContents,
105+
examplesDir,
106+
exampleTableOfContents,
107+
webpackConfig = {}
108+
} = config;
109+
const hasExamples = Boolean(examplesDir && exampleTableOfContents);
63110

64111
return {
65-
title: config.projectName,
66-
tagline: config.tagline,
112+
title: projectName,
113+
tagline,
67114
url: siteUrl.origin,
68115
baseUrl: siteUrl.pathname,
69116
onBrokenLinks: 'warn',
70117
onBrokenMarkdownLinks: 'warn',
71118
favicon: '/favicon.png',
72119
organizationName: 'visgl',
73-
projectName: config.projectName,
120+
projectName,
74121
trailingSlash: false,
75122
staticDirectories: ['static', resolve(cwd, '../static')],
76123

@@ -79,9 +126,9 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
79126
'classic',
80127
{
81128
docs: {
82-
path: config.docsDir ?? '../docs',
83-
sidebarItemsGenerator: () => config.docsTableOfContents.map(normalizeSidebarItem),
84-
editUrl: `${config.repoUrl}/tree/master/docs`
129+
path: docsDir,
130+
sidebarItemsGenerator: () => docsTableOfContents.map(normalizeSidebarItem),
131+
editUrl: `${repoUrl}/tree/master/docs`
85132
},
86133
theme: {
87134
customCss: [resolve(cwd, '../src/styles.css')]
@@ -95,29 +142,28 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
95142
'@vis.gl/docusaurus-website/plugin-webpack-config',
96143
deepmerge(
97144
{
98-
debug: true,
99145
resolve: {
100-
modules: [resolve('node_modules'), resolve('../node_modules')],
101-
alias: {}
146+
modules: [resolve('node_modules'), resolve(rootDir, 'node_modules')],
147+
alias: getAliases(rootDir)
102148
},
103149
plugins: [],
104150
module: {}
105151
},
106-
config.webpackConfig
152+
webpackConfig
107153
)
108154
],
109-
config.examplesDir && [
155+
hasExamples && [
110156
'@docusaurus/plugin-content-docs',
111157
{
112158
id: 'examples',
113-
path: config.examplesDir,
159+
path: examplesDir,
114160
routeBasePath: 'examples',
115-
sidebarItemsGenerator: () => config.exampleTableOfContents?.map(normalizeSidebarItem),
161+
sidebarItemsGenerator: () => exampleTableOfContents?.map(normalizeSidebarItem),
116162
breadcrumbs: false,
117163
docItemComponent: resolve(cwd, './components/doc-item-component.js')
118164
}
119165
],
120-
config.search === 'local' && [
166+
search === 'local' && [
121167
'@cmfcmf/docusaurus-search-local',
122168
{
123169
// Options here
@@ -127,14 +173,14 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
127173

128174
themeConfig: {
129175
navbar: {
130-
title: config.projectName,
176+
title: projectName,
131177
logo: {
132178
alt: 'vis.gl Logo',
133179
src: '/visgl-logo-dark.png',
134180
srcDark: '/visgl-logo-light.png'
135181
},
136182
items: [
137-
config.examplesDir && {
183+
hasExamples && {
138184
to: '/examples',
139185
position: 'left',
140186
label: 'Examples'
@@ -145,7 +191,7 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
145191
label: 'Docs'
146192
},
147193
{
148-
href: config.repoUrl,
194+
href: repoUrl,
149195
label: 'GitHub',
150196
position: 'right'
151197
}
@@ -177,7 +223,7 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
177223
label: 'deck.gl-community',
178224
href: 'https://visgl.github.io/deck.gl-community/'
179225
}
180-
].filter((item) => item.label !== config.projectName)
226+
].filter((item) => item.label !== projectName)
181227
},
182228
{
183229
title: 'More',
@@ -188,14 +234,14 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
188234
},
189235
{
190236
label: 'GitHub',
191-
href: config.repoUrl
237+
href: repoUrl
192238
}
193239
]
194240
}
195241
],
196242
copyright: `Copyright © ${new Date().getFullYear()} OpenJS Foundation`
197243
},
198-
algolia: typeof config.search === 'object' ? config.search : undefined,
244+
algolia: typeof search === 'object' ? search : undefined,
199245
prism: {
200246
theme: lightCodeTheme,
201247
darkTheme: darkCodeTheme

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"paths": {
2727
"@vis.gl/dev-tools": ["modules/dev-tools/src"],
28-
"@vis.gl/ts-plugins": ["modules/ts-plugins/src"]
28+
"@vis.gl/ts-plugins": ["modules/ts-plugins/src"],
29+
"@vis.gl/docusaurus-website": ["modules/docusaurus-website/src"]
2930
}
3031
}

0 commit comments

Comments
 (0)