@docusaurus/core 2.0.0-beta.8bda3b2db → 2.0.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/bin/beforeCli.js +125 -0
  2. package/bin/docusaurus.js +36 -105
  3. package/lib/.tsbuildinfo +1 -1
  4. package/lib/babel/preset.js +1 -1
  5. package/lib/choosePort.js +10 -11
  6. package/lib/client/.tsbuildinfo +1 -1
  7. package/lib/client/App.js +8 -22
  8. package/lib/client/PendingNavigation.d.ts +24 -1
  9. package/lib/client/PendingNavigation.js +1 -1
  10. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +1 -0
  11. package/lib/client/docusaurus.js +0 -1
  12. package/lib/client/exports/BrowserOnly.js +5 -3
  13. package/lib/client/exports/Interpolate.js +4 -0
  14. package/lib/client/exports/Link.js +11 -11
  15. package/lib/client/exports/Translate.d.ts +2 -2
  16. package/lib/client/exports/Translate.js +13 -9
  17. package/lib/client/exports/browserContext.d.ts +11 -0
  18. package/lib/client/exports/browserContext.js +21 -0
  19. package/lib/client/exports/constants.js +1 -11
  20. package/lib/client/exports/{context.d.ts → docusaurusContext.d.ts} +5 -3
  21. package/lib/client/exports/docusaurusContext.js +25 -0
  22. package/lib/client/exports/useBaseUrl.js +1 -1
  23. package/lib/client/exports/useDocusaurusContext.js +2 -7
  24. package/lib/client/exports/useGlobalData.js +1 -5
  25. package/lib/client/exports/{context.js → useIsBrowser.d.ts} +1 -2
  26. package/lib/client/exports/useIsBrowser.js +11 -0
  27. package/lib/client/normalizeLocation.d.ts +1 -3
  28. package/lib/client/prefetch.js +0 -1
  29. package/lib/client/serverEntry.js +11 -27
  30. package/lib/commands/build.js +26 -23
  31. package/lib/commands/clear.js +2 -2
  32. package/lib/commands/commandUtils.js +2 -2
  33. package/lib/commands/deploy.js +24 -14
  34. package/lib/commands/external.js +4 -4
  35. package/lib/commands/serve.js +13 -13
  36. package/lib/commands/start.js +74 -72
  37. package/lib/commands/swizzle.js +33 -33
  38. package/lib/commands/writeHeadingIds.d.ts +8 -5
  39. package/lib/commands/writeHeadingIds.js +32 -31
  40. package/lib/commands/writeTranslations.js +25 -11
  41. package/lib/constants.d.ts +1 -0
  42. package/lib/constants.js +4 -1
  43. package/lib/server/brokenLinks.js +13 -13
  44. package/lib/server/config.js +4 -4
  45. package/lib/server/configValidation.js +4 -3
  46. package/lib/server/duplicateRoutes.js +2 -2
  47. package/lib/server/html-tags/htmlTags.js +5 -5
  48. package/lib/server/html-tags/index.d.ts +2 -2
  49. package/lib/server/html-tags/index.js +3 -3
  50. package/lib/server/i18n.js +4 -4
  51. package/lib/server/index.js +99 -62
  52. package/lib/server/loadSetup.js +3 -3
  53. package/lib/server/plugins/applyRouteTrailingSlash.d.ts +2 -1
  54. package/lib/server/plugins/applyRouteTrailingSlash.js +3 -3
  55. package/lib/server/plugins/index.d.ts +3 -4
  56. package/lib/server/plugins/index.js +41 -27
  57. package/lib/server/plugins/init.d.ts +2 -6
  58. package/lib/server/plugins/init.js +7 -10
  59. package/lib/server/plugins/pluginIds.d.ts +2 -2
  60. package/lib/server/plugins/pluginIds.js +2 -2
  61. package/lib/server/presets/index.js +4 -7
  62. package/lib/server/routes.js +41 -23
  63. package/lib/server/themes/alias.d.ts +3 -2
  64. package/lib/server/themes/alias.js +24 -12
  65. package/lib/server/themes/index.d.ts +6 -2
  66. package/lib/server/themes/index.js +19 -9
  67. package/lib/server/translations/translations.d.ts +4 -5
  68. package/lib/server/translations/translations.js +9 -9
  69. package/lib/server/translations/translationsExtractor.d.ts +2 -3
  70. package/lib/server/translations/translationsExtractor.js +58 -47
  71. package/lib/server/utils.d.ts +8 -2
  72. package/lib/server/utils.js +8 -10
  73. package/lib/server/versions/__fixtures__/dummy-plugin.d.ts +0 -0
  74. package/lib/server/versions/__tests/index.test.js +5 -5
  75. package/lib/server/versions/index.js +6 -6
  76. package/lib/webpack/base.js +28 -19
  77. package/lib/webpack/client.js +8 -17
  78. package/lib/webpack/plugins/CleanWebpackPlugin.js +2 -5
  79. package/lib/webpack/plugins/LogPlugin.js +3 -4
  80. package/lib/webpack/plugins/WaitPlugin.js +4 -4
  81. package/lib/webpack/react-dev-utils-webpack5/evalSourceMapMiddleware.d.ts +2 -0
  82. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.d.ts +5 -0
  83. package/lib/webpack/react-dev-utils-webpack5/formatWebpackMessages.js +1 -1
  84. package/lib/webpack/react-dev-utils-webpack5/webpackHotDevClient.d.ts +1 -0
  85. package/lib/webpack/server.js +11 -7
  86. package/lib/webpack/sharedModuleAliases.js +1 -1
  87. package/lib/webpack/utils.d.ts +2 -1
  88. package/lib/webpack/utils.js +28 -25
  89. package/package.json +45 -39
  90. package/tsconfig.client.json +1 -2
  91. package/tsconfig.json +5 -4
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ const chalk = require('chalk');
9
+ const fs = require('fs-extra');
10
+ const semver = require('semver');
11
+ const path = require('path');
12
+ const updateNotifier = require('update-notifier');
13
+ const boxen = require('boxen');
14
+
15
+ const {
16
+ name,
17
+ version,
18
+ engines: {node: requiredVersion},
19
+ } = require('../package.json');
20
+
21
+ // Notify user if @docusaurus packages is outdated
22
+ //
23
+ // Note: this is a 2-step process to avoid delaying cli usage by awaiting a response:
24
+ // - 1st run: trigger background job to check releases + store result
25
+ // - 2nd run: display potential update to users
26
+ //
27
+ // Note: even if the
28
+ //
29
+ // cache data is stored in ~/.config/configstore/update-notifier-@docusaurus
30
+ //
31
+ const notifier = updateNotifier({
32
+ pkg: {
33
+ name,
34
+ version,
35
+ },
36
+ // Check is in background so it's fine to use a small value like 1h
37
+ // Use 0 for debugging
38
+ updateCheckInterval: 1000 * 60 * 60,
39
+ // updateCheckInterval: 0
40
+ });
41
+
42
+ // Hacky way to ensure we check for updates on first run
43
+ // Note: the notification will only happen in the 2nd run
44
+ // See https://github.com/yeoman/update-notifier/issues/209
45
+ try {
46
+ if (
47
+ notifier.config &&
48
+ !notifier.disabled &&
49
+ Date.now() - notifier.config.get('lastUpdateCheck') < 50
50
+ ) {
51
+ notifier.config.set('lastUpdateCheck', 0);
52
+ notifier.check();
53
+ }
54
+ } catch (e) {
55
+ // Do not stop cli if this fails, see https://github.com/facebook/docusaurus/issues/5400
56
+ console.error(e);
57
+ }
58
+
59
+ // We don't want to display update message for canary releases
60
+ // See https://github.com/facebook/docusaurus/issues/5378
61
+ function ignoreUpdate(update) {
62
+ const isCanaryRelease =
63
+ update && update.current && update.current.startsWith('0.0.0');
64
+ return isCanaryRelease;
65
+ }
66
+
67
+ if (
68
+ notifier.config &&
69
+ notifier.update &&
70
+ notifier.update.current !== notifier.update.latest
71
+ ) {
72
+ // Because notifier clears cached data after reading it, leading to notifier not consistently displaying the update
73
+ // See https://github.com/yeoman/update-notifier/issues/209
74
+ notifier.config.set('update', notifier.update);
75
+
76
+ if (ignoreUpdate(notifier.update)) {
77
+ return;
78
+ }
79
+
80
+ // eslint-disable-next-line import/no-dynamic-require, global-require
81
+ const sitePkg = require(path.resolve(process.cwd(), 'package.json'));
82
+ const siteDocusaurusPackagesForUpdate = Object.keys({
83
+ ...sitePkg.dependencies,
84
+ ...sitePkg.devDependencies,
85
+ })
86
+ .filter((p) => p.startsWith('@docusaurus'))
87
+ .map((p) => p.concat('@latest'))
88
+ .join(' ');
89
+ const isYarnUsed = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'));
90
+ const upgradeCommand = isYarnUsed
91
+ ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
92
+ : `npm i ${siteDocusaurusPackagesForUpdate}`;
93
+
94
+ const boxenOptions = {
95
+ padding: 1,
96
+ margin: 1,
97
+ align: 'center',
98
+ borderColor: 'yellow',
99
+ borderStyle: 'round',
100
+ };
101
+
102
+ const docusaurusUpdateMessage = boxen(
103
+ `Update available ${chalk.dim(`${notifier.update.current}`)}${chalk.reset(
104
+ ' → ',
105
+ )}${chalk.green(
106
+ `${notifier.update.latest}`,
107
+ )}\n\nTo upgrade Docusaurus packages with the latest version, run the following command:\n${chalk.cyan(
108
+ `${upgradeCommand}`,
109
+ )}`,
110
+ boxenOptions,
111
+ );
112
+
113
+ console.log(docusaurusUpdateMessage);
114
+ }
115
+
116
+ // notify user if node version needs to be updated
117
+ if (!semver.satisfies(process.version, requiredVersion)) {
118
+ console.log(
119
+ chalk.red(`\nMinimum Node version not met :(`) +
120
+ chalk.yellow(
121
+ `\n\nYou are using Node ${process.version}. We require Node ${requiredVersion} or up!\n`,
122
+ ),
123
+ );
124
+ process.exit(1);
125
+ }
package/bin/docusaurus.js CHANGED
@@ -1,7 +1,4 @@
1
1
  #!/usr/bin/env node
2
-
3
- // TODO remove when fixed: https://github.com/Stuk/eslint-plugin-header/issues/39
4
- /* eslint-disable header/header */
5
2
  /**
6
3
  * Copyright (c) Facebook, Inc. and its affiliates.
7
4
  *
@@ -10,12 +7,8 @@
10
7
  */
11
8
 
12
9
  const chalk = require('chalk');
13
- const fs = require('fs-extra');
14
- const semver = require('semver');
15
- const path = require('path');
10
+ const fs = require('fs');
16
11
  const cli = require('commander');
17
- const updateNotifier = require('update-notifier');
18
- const boxen = require('boxen');
19
12
  const {
20
13
  build,
21
14
  swizzle,
@@ -27,69 +20,10 @@ const {
27
20
  writeTranslations,
28
21
  writeHeadingIds,
29
22
  } = require('../lib');
30
- const {
31
- name,
32
- version,
33
- engines: {node: requiredVersion},
34
- } = require('../package.json');
35
-
36
- // notify user if @docusaurus packages is outdated
37
- const notifier = updateNotifier({
38
- pkg: {
39
- name,
40
- version,
41
- },
42
- });
43
23
 
44
- // allow the user to be notified for updates on the first run
45
- if (notifier.lastUpdateCheck === Date.now()) {
46
- notifier.lastUpdateCheck = 0;
47
- }
48
-
49
- if (notifier.update && semver.gt(this.update.latest, this.update.current)) {
50
- // eslint-disable-next-line import/no-dynamic-require, global-require
51
- const sitePkg = require(path.resolve(process.cwd(), 'package.json'));
52
- const siteDocusaurusPackagesForUpdate = Object.keys(sitePkg.dependencies)
53
- .filter((p) => p.startsWith('@docusaurus'))
54
- .map((p) => p.concat('@latest'))
55
- .join(' ');
56
- const isYarnUsed = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'));
57
- const upgradeCommand = isYarnUsed
58
- ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
59
- : `npm i ${siteDocusaurusPackagesForUpdate}`;
24
+ require('./beforeCli');
60
25
 
61
- const boxenOptions = {
62
- padding: 1,
63
- margin: 1,
64
- align: 'center',
65
- borderColor: 'yellow',
66
- borderStyle: 'round',
67
- };
68
-
69
- const docusaurusUpdateMessage = boxen(
70
- `Update available ${chalk.dim(`${notifier.update.current}`)}${chalk.reset(
71
- ' → ',
72
- )}${chalk.green(
73
- `${notifier.update.latest}`,
74
- )}\n\nTo upgrade Docusaurus packages with the latest version, run the following command:\n${chalk.cyan(
75
- `${upgradeCommand}`,
76
- )}`,
77
- boxenOptions,
78
- );
79
-
80
- console.log(docusaurusUpdateMessage);
81
- }
82
-
83
- // notify user if node version needs to be updated
84
- if (!semver.satisfies(process.version, requiredVersion)) {
85
- console.log(
86
- chalk.red(`\nMinimum Node.js version not met :(`) +
87
- chalk.yellow(
88
- `\n\nYou are using Node.js ${process.version}. We require Node.js ${requiredVersion} or up!\n`,
89
- ),
90
- );
91
- process.exit(1);
92
- }
26
+ const resolveDir = (dir = '.') => fs.realpathSync(dir);
93
27
 
94
28
  cli.version(require('../package.json').version).usage('<command> [options]');
95
29
 
@@ -116,8 +50,8 @@ cli
116
50
  '--no-minify',
117
51
  'build website without minimizing JS bundles (default: false)',
118
52
  )
119
- .action((siteDir = '.', {bundleAnalyzer, config, outDir, locale, minify}) => {
120
- build(path.resolve(siteDir), {
53
+ .action((siteDir, {bundleAnalyzer, config, outDir, locale, minify}) => {
54
+ build(resolveDir(siteDir), {
121
55
  bundleAnalyzer,
122
56
  outDir,
123
57
  config,
@@ -134,14 +68,8 @@ cli
134
68
  'copy TypeScript theme files when possible (default: false)',
135
69
  )
136
70
  .option('--danger', 'enable swizzle for internal component of themes')
137
- .action((themeName, componentName, siteDir = '.', {typescript, danger}) => {
138
- swizzle(
139
- path.resolve(siteDir),
140
- themeName,
141
- componentName,
142
- typescript,
143
- danger,
144
- );
71
+ .action((themeName, componentName, siteDir, {typescript, danger}) => {
72
+ swizzle(resolveDir(siteDir), themeName, componentName, typescript, danger);
145
73
  });
146
74
 
147
75
  cli
@@ -163,8 +91,8 @@ cli
163
91
  '--skip-build',
164
92
  'skip building website before deploy it (default: false)',
165
93
  )
166
- .action((siteDir = '.', {outDir, skipBuild, config}) => {
167
- deploy(path.resolve(siteDir), {
94
+ .action((siteDir, {outDir, skipBuild, config}) => {
95
+ deploy(resolveDir(siteDir), {
168
96
  outDir,
169
97
  config,
170
98
  skipBuild,
@@ -190,19 +118,17 @@ cli
190
118
  '--poll [interval]',
191
119
  'use polling rather than watching for reload (default: false). Can specify a poll interval in milliseconds',
192
120
  )
193
- .action(
194
- (siteDir = '.', {port, host, locale, config, hotOnly, open, poll}) => {
195
- start(path.resolve(siteDir), {
196
- port,
197
- host,
198
- locale,
199
- config,
200
- hotOnly,
201
- open,
202
- poll,
203
- });
204
- },
205
- );
121
+ .action((siteDir, {port, host, locale, config, hotOnly, open, poll}) => {
122
+ start(resolveDir(siteDir), {
123
+ port,
124
+ host,
125
+ locale,
126
+ config,
127
+ hotOnly,
128
+ open,
129
+ poll,
130
+ });
131
+ });
206
132
 
207
133
  cli
208
134
  .command('serve [siteDir]')
@@ -220,7 +146,7 @@ cli
220
146
  .option('-h, --host <host>', 'use specified host (default: localhost)')
221
147
  .action(
222
148
  (
223
- siteDir = '.',
149
+ siteDir,
224
150
  {
225
151
  dir = 'build',
226
152
  port = 3000,
@@ -229,7 +155,7 @@ cli
229
155
  config,
230
156
  },
231
157
  ) => {
232
- serve(path.resolve(siteDir), {
158
+ serve(resolveDir(siteDir), {
233
159
  dir,
234
160
  port,
235
161
  build: buildSite,
@@ -242,8 +168,8 @@ cli
242
168
  cli
243
169
  .command('clear [siteDir]')
244
170
  .description('Remove build artifacts.')
245
- .action((siteDir = '.') => {
246
- clear(path.resolve(siteDir));
171
+ .action((siteDir) => {
172
+ clear(resolveDir(siteDir));
247
173
  });
248
174
 
249
175
  cli
@@ -267,10 +193,10 @@ cli
267
193
  )
268
194
  .action(
269
195
  (
270
- siteDir = '.',
196
+ siteDir,
271
197
  {locale = undefined, override = false, messagePrefix = '', config},
272
198
  ) => {
273
- writeTranslations(path.resolve(siteDir), {
199
+ writeTranslations(resolveDir(siteDir), {
274
200
  locale,
275
201
  override,
276
202
  config,
@@ -280,11 +206,16 @@ cli
280
206
  );
281
207
 
282
208
  cli
283
- .command('write-heading-ids [contentDir]')
209
+ .command('write-heading-ids [contentDir] [files]')
284
210
  .description('Generate heading ids in Markdown content.')
285
- .action((siteDir = '.') => {
286
- writeHeadingIds(siteDir);
287
- });
211
+ .option(
212
+ '--maintain-case',
213
+ "keep the headings' casing, otherwise make all lowercase (default: false)",
214
+ )
215
+ .option('--overwrite', 'overwrite existing heading IDs (default: false)')
216
+ .action((siteDir, files, options) =>
217
+ writeHeadingIds(resolveDir(siteDir), files, options),
218
+ );
288
219
 
289
220
  cli.arguments('<command>').action((cmd) => {
290
221
  cli.outputHelp();
@@ -307,7 +238,7 @@ function isInternalCommand(command) {
307
238
 
308
239
  async function run() {
309
240
  if (!isInternalCommand(process.argv.slice(2)[0])) {
310
- await externalCommand(cli, path.resolve('.'));
241
+ await externalCommand(cli, resolveDir('.'));
311
242
  }
312
243
 
313
244
  cli.parse(process.argv);