1
+ import fs from 'fs' ;
1
2
import { resolve , dirname } from 'path' ;
2
3
import lightCodeTheme from 'prism-react-renderer/themes/nightOwlLight' ;
3
4
import darkCodeTheme from 'prism-react-renderer/themes/nightOwl' ;
@@ -28,6 +29,32 @@ function normalizeSidebarItem(item: SidebarItem | string): SidebarItem {
28
29
return item ;
29
30
}
30
31
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
+
31
58
export type OcularWebsiteConfig = {
32
59
/** Name of the project */
33
60
projectName : string ;
@@ -42,9 +69,16 @@ export type OcularWebsiteConfig = {
42
69
siteUrl : string ;
43
70
44
71
/** 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 ;
46
78
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
+ */
48
82
docsDir ?: string ;
49
83
/** Documentation table of contents */
50
84
docsTableOfContents : SidebarItem [ ] ;
@@ -60,17 +94,30 @@ export type OcularWebsiteConfig = {
60
94
61
95
export function getDocusaurusConfig ( config : OcularWebsiteConfig ) : Config {
62
96
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 ) ;
63
110
64
111
return {
65
- title : config . projectName ,
66
- tagline : config . tagline ,
112
+ title : projectName ,
113
+ tagline,
67
114
url : siteUrl . origin ,
68
115
baseUrl : siteUrl . pathname ,
69
116
onBrokenLinks : 'warn' ,
70
117
onBrokenMarkdownLinks : 'warn' ,
71
118
favicon : '/favicon.png' ,
72
119
organizationName : 'visgl' ,
73
- projectName : config . projectName ,
120
+ projectName,
74
121
trailingSlash : false ,
75
122
staticDirectories : [ 'static' , resolve ( cwd , '../static' ) ] ,
76
123
@@ -79,9 +126,9 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
79
126
'classic' ,
80
127
{
81
128
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`
85
132
} ,
86
133
theme : {
87
134
customCss : [ resolve ( cwd , '../src/styles.css' ) ]
@@ -95,29 +142,28 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
95
142
'@vis.gl/docusaurus-website/plugin-webpack-config' ,
96
143
deepmerge (
97
144
{
98
- debug : true ,
99
145
resolve : {
100
- modules : [ resolve ( 'node_modules' ) , resolve ( '../ node_modules') ] ,
101
- alias : { }
146
+ modules : [ resolve ( 'node_modules' ) , resolve ( rootDir , ' node_modules') ] ,
147
+ alias : getAliases ( rootDir )
102
148
} ,
103
149
plugins : [ ] ,
104
150
module : { }
105
151
} ,
106
- config . webpackConfig
152
+ webpackConfig
107
153
)
108
154
] ,
109
- config . examplesDir && [
155
+ hasExamples && [
110
156
'@docusaurus/plugin-content-docs' ,
111
157
{
112
158
id : 'examples' ,
113
- path : config . examplesDir ,
159
+ path : examplesDir ,
114
160
routeBasePath : 'examples' ,
115
- sidebarItemsGenerator : ( ) => config . exampleTableOfContents ?. map ( normalizeSidebarItem ) ,
161
+ sidebarItemsGenerator : ( ) => exampleTableOfContents ?. map ( normalizeSidebarItem ) ,
116
162
breadcrumbs : false ,
117
163
docItemComponent : resolve ( cwd , './components/doc-item-component.js' )
118
164
}
119
165
] ,
120
- config . search === 'local' && [
166
+ search === 'local' && [
121
167
'@cmfcmf/docusaurus-search-local' ,
122
168
{
123
169
// Options here
@@ -127,14 +173,14 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
127
173
128
174
themeConfig : {
129
175
navbar : {
130
- title : config . projectName ,
176
+ title : projectName ,
131
177
logo : {
132
178
alt : 'vis.gl Logo' ,
133
179
src : '/visgl-logo-dark.png' ,
134
180
srcDark : '/visgl-logo-light.png'
135
181
} ,
136
182
items : [
137
- config . examplesDir && {
183
+ hasExamples && {
138
184
to : '/examples' ,
139
185
position : 'left' ,
140
186
label : 'Examples'
@@ -145,7 +191,7 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
145
191
label : 'Docs'
146
192
} ,
147
193
{
148
- href : config . repoUrl ,
194
+ href : repoUrl ,
149
195
label : 'GitHub' ,
150
196
position : 'right'
151
197
}
@@ -177,7 +223,7 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
177
223
label : 'deck.gl-community' ,
178
224
href : 'https://visgl.github.io/deck.gl-community/'
179
225
}
180
- ] . filter ( ( item ) => item . label !== config . projectName )
226
+ ] . filter ( ( item ) => item . label !== projectName )
181
227
} ,
182
228
{
183
229
title : 'More' ,
@@ -188,14 +234,14 @@ export function getDocusaurusConfig(config: OcularWebsiteConfig): Config {
188
234
} ,
189
235
{
190
236
label : 'GitHub' ,
191
- href : config . repoUrl
237
+ href : repoUrl
192
238
}
193
239
]
194
240
}
195
241
] ,
196
242
copyright : `Copyright © ${ new Date ( ) . getFullYear ( ) } OpenJS Foundation`
197
243
} ,
198
- algolia : typeof config . search === 'object' ? config . search : undefined ,
244
+ algolia : typeof search === 'object' ? search : undefined ,
199
245
prism : {
200
246
theme : lightCodeTheme ,
201
247
darkTheme : darkCodeTheme
0 commit comments