@docusaurus/core 2.0.0-beta.15 → 2.0.0-beta.16

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 (131) hide show
  1. package/bin/beforeCli.mjs +136 -0
  2. package/bin/{docusaurus.js → docusaurus.mjs} +62 -40
  3. package/lib/babel/preset.d.ts +1 -2
  4. package/lib/babel/preset.js +5 -4
  5. package/lib/choosePort.js +22 -30
  6. package/lib/client/App.d.ts +1 -2
  7. package/lib/client/App.js +13 -8
  8. package/lib/client/LinksCollector.js +1 -1
  9. package/lib/client/PendingNavigation.d.ts +4 -4
  10. package/lib/client/PendingNavigation.js +4 -6
  11. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.d.ts +8 -0
  12. package/lib/client/baseUrlIssueBanner/BaseUrlIssueBanner.js +15 -10
  13. package/lib/client/client-lifecycles-dispatcher.d.ts +2 -5
  14. package/lib/client/client-lifecycles-dispatcher.js +5 -7
  15. package/lib/client/clientEntry.js +11 -5
  16. package/lib/client/docusaurus.js +6 -4
  17. package/lib/client/exports/BrowserOnly.d.ts +1 -2
  18. package/lib/client/exports/BrowserOnly.js +2 -3
  19. package/lib/client/exports/ComponentCreator.d.ts +1 -2
  20. package/lib/client/exports/ComponentCreator.js +7 -6
  21. package/lib/client/exports/ErrorBoundary.d.ts +2 -2
  22. package/lib/client/exports/ErrorBoundary.js +1 -2
  23. package/lib/client/exports/Head.d.ts +2 -3
  24. package/lib/client/exports/Head.js +3 -4
  25. package/lib/client/exports/Interpolate.js +9 -12
  26. package/lib/client/exports/Link.d.ts +11 -5
  27. package/lib/client/exports/Link.js +13 -7
  28. package/lib/client/exports/Translate.js +2 -1
  29. package/lib/client/exports/browserContext.js +3 -2
  30. package/lib/client/exports/docusaurusContext.js +1 -1
  31. package/lib/client/exports/isInternalUrl.js +1 -1
  32. package/lib/client/exports/renderRoutes.d.ts +1 -2
  33. package/lib/client/exports/renderRoutes.js +1 -2
  34. package/lib/client/exports/router.d.ts +1 -1
  35. package/lib/client/exports/router.js +1 -1
  36. package/lib/client/exports/useDocusaurusContext.d.ts +1 -2
  37. package/lib/client/exports/useDocusaurusContext.js +1 -2
  38. package/lib/client/flat.d.ts +1 -2
  39. package/lib/client/flat.js +1 -2
  40. package/lib/client/normalizeLocation.d.ts +2 -3
  41. package/lib/client/normalizeLocation.js +1 -2
  42. package/lib/client/prefetch.d.ts +1 -2
  43. package/lib/client/prefetch.js +1 -2
  44. package/lib/client/preload.d.ts +2 -1
  45. package/lib/client/preload.js +2 -1
  46. package/lib/client/serverEntry.js +23 -19
  47. package/lib/client/theme-fallback/Error/index.d.ts +10 -0
  48. package/lib/client/theme-fallback/Error/index.js +21 -29
  49. package/lib/client/theme-fallback/Layout/index.d.ts +10 -0
  50. package/lib/client/theme-fallback/Layout/index.js +10 -19
  51. package/lib/client/theme-fallback/Loading/index.d.ts +9 -0
  52. package/lib/client/theme-fallback/Loading/index.js +46 -114
  53. package/lib/{server/versions/__tests/index.test.d.ts → client/theme-fallback/NotFound/index.d.ts} +2 -1
  54. package/lib/client/theme-fallback/NotFound/index.js +9 -16
  55. package/lib/client/theme-fallback/Root/index.d.ts +10 -0
  56. package/lib/client/theme-fallback/Root/index.js +2 -5
  57. package/lib/commands/build.js +33 -34
  58. package/lib/commands/clear.js +23 -11
  59. package/lib/commands/deploy.js +12 -11
  60. package/lib/commands/external.d.ts +2 -2
  61. package/lib/commands/external.js +1 -1
  62. package/lib/commands/serve.js +3 -2
  63. package/lib/commands/start.js +4 -4
  64. package/lib/commands/swizzle/actions.d.ts +23 -0
  65. package/lib/commands/swizzle/actions.js +102 -0
  66. package/lib/commands/swizzle/common.d.ts +33 -0
  67. package/lib/commands/swizzle/common.js +57 -0
  68. package/lib/commands/swizzle/components.d.ts +29 -0
  69. package/lib/commands/swizzle/components.js +165 -0
  70. package/lib/commands/swizzle/config.d.ts +10 -0
  71. package/lib/commands/swizzle/config.js +77 -0
  72. package/lib/commands/swizzle/context.d.ts +8 -0
  73. package/lib/commands/swizzle/context.js +30 -0
  74. package/lib/commands/swizzle/index.d.ts +8 -0
  75. package/lib/commands/swizzle/index.js +115 -0
  76. package/lib/commands/swizzle/prompts.d.ts +12 -0
  77. package/lib/commands/swizzle/prompts.js +110 -0
  78. package/lib/commands/swizzle/tables.d.ts +9 -0
  79. package/lib/commands/swizzle/tables.js +116 -0
  80. package/lib/commands/swizzle/themes.d.ts +20 -0
  81. package/lib/commands/swizzle/themes.js +105 -0
  82. package/lib/commands/writeHeadingIds.d.ts +1 -1
  83. package/lib/commands/writeHeadingIds.js +13 -14
  84. package/lib/commands/writeTranslations.js +10 -7
  85. package/lib/index.d.ts +10 -9
  86. package/lib/index.js +20 -19
  87. package/lib/server/brokenLinks.js +30 -20
  88. package/lib/server/config.js +1 -1
  89. package/lib/server/configValidation.d.ts +1 -1
  90. package/lib/server/configValidation.js +32 -23
  91. package/lib/server/duplicateRoutes.js +2 -4
  92. package/lib/server/html-tags/htmlTags.js +1 -2
  93. package/lib/server/i18n.d.ts +0 -1
  94. package/lib/server/i18n.js +16 -26
  95. package/lib/server/index.d.ts +1 -1
  96. package/lib/server/index.js +17 -15
  97. package/lib/server/loadSetup.d.ts +1 -2
  98. package/lib/server/loadSetup.js +2 -2
  99. package/lib/server/moduleShorthand.js +1 -1
  100. package/lib/server/plugins/index.js +9 -9
  101. package/lib/server/plugins/init.d.ts +11 -1
  102. package/lib/server/plugins/init.js +23 -28
  103. package/lib/server/plugins/pluginIds.js +4 -3
  104. package/lib/server/presets/index.d.ts +2 -2
  105. package/lib/server/presets/index.js +3 -3
  106. package/lib/server/routes.js +13 -7
  107. package/lib/server/themes/alias.d.ts +1 -1
  108. package/lib/server/themes/alias.js +5 -6
  109. package/lib/server/themes/index.d.ts +2 -2
  110. package/lib/server/themes/index.js +10 -9
  111. package/lib/server/translations/translations.js +10 -11
  112. package/lib/server/translations/translationsExtractor.js +20 -19
  113. package/lib/server/versions/index.d.ts +2 -3
  114. package/lib/server/versions/index.js +22 -21
  115. package/lib/webpack/base.d.ts +2 -2
  116. package/lib/webpack/base.js +30 -22
  117. package/lib/webpack/client.d.ts +1 -1
  118. package/lib/webpack/client.js +7 -4
  119. package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +12 -2
  120. package/lib/webpack/plugins/ChunkAssetPlugin.js +17 -10
  121. package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +5 -5
  122. package/lib/webpack/plugins/CleanWebpackPlugin.js +5 -4
  123. package/lib/webpack/server.d.ts +1 -1
  124. package/lib/webpack/server.js +6 -5
  125. package/lib/webpack/utils.d.ts +3 -3
  126. package/lib/webpack/utils.js +17 -37
  127. package/package.json +56 -56
  128. package/bin/beforeCli.js +0 -124
  129. package/lib/commands/swizzle.d.ts +0 -9
  130. package/lib/commands/swizzle.js +0 -236
  131. package/lib/server/versions/__tests/index.test.js +0 -26
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@docusaurus/core",
3
3
  "description": "Easy to Maintain Open Source Documentation Websites",
4
- "version": "2.0.0-beta.15",
4
+ "version": "2.0.0-beta.16",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -21,7 +21,7 @@
21
21
  "directory": "packages/docusaurus"
22
22
  },
23
23
  "bin": {
24
- "docusaurus": "bin/docusaurus.js"
24
+ "docusaurus": "bin/docusaurus.mjs"
25
25
  },
26
26
  "scripts": {
27
27
  "build": "tsc && tsc -p tsconfig.client.json && node copyUntypedFiles.mjs",
@@ -31,57 +31,59 @@
31
31
  "url": "https://github.com/facebook/docusaurus/issues"
32
32
  },
33
33
  "dependencies": {
34
- "@babel/core": "^7.16.0",
35
- "@babel/generator": "^7.16.0",
34
+ "@babel/core": "^7.17.5",
35
+ "@babel/generator": "^7.17.3",
36
36
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
37
- "@babel/plugin-transform-runtime": "^7.16.0",
38
- "@babel/preset-env": "^7.16.4",
39
- "@babel/preset-react": "^7.16.0",
40
- "@babel/preset-typescript": "^7.16.0",
41
- "@babel/runtime": "^7.16.3",
42
- "@babel/runtime-corejs3": "^7.16.3",
43
- "@babel/traverse": "^7.16.3",
44
- "@docusaurus/cssnano-preset": "2.0.0-beta.15",
45
- "@docusaurus/logger": "2.0.0-beta.15",
46
- "@docusaurus/mdx-loader": "2.0.0-beta.15",
37
+ "@babel/plugin-transform-runtime": "^7.17.0",
38
+ "@babel/preset-env": "^7.16.11",
39
+ "@babel/preset-react": "^7.16.7",
40
+ "@babel/preset-typescript": "^7.16.7",
41
+ "@babel/runtime": "^7.17.2",
42
+ "@babel/runtime-corejs3": "^7.17.2",
43
+ "@babel/traverse": "^7.17.3",
44
+ "@docusaurus/cssnano-preset": "2.0.0-beta.16",
45
+ "@docusaurus/logger": "2.0.0-beta.16",
46
+ "@docusaurus/mdx-loader": "2.0.0-beta.16",
47
47
  "@docusaurus/react-loadable": "5.5.2",
48
- "@docusaurus/utils": "2.0.0-beta.15",
49
- "@docusaurus/utils-common": "2.0.0-beta.15",
50
- "@docusaurus/utils-validation": "2.0.0-beta.15",
51
- "@slorber/static-site-generator-webpack-plugin": "^4.0.0",
52
- "@svgr/webpack": "^6.0.0",
53
- "autoprefixer": "^10.3.5",
54
- "babel-loader": "^8.2.2",
48
+ "@docusaurus/utils": "2.0.0-beta.16",
49
+ "@docusaurus/utils-common": "2.0.0-beta.16",
50
+ "@docusaurus/utils-validation": "2.0.0-beta.16",
51
+ "@slorber/static-site-generator-webpack-plugin": "^4.0.1",
52
+ "@svgr/webpack": "^6.2.1",
53
+ "autoprefixer": "^10.4.2",
54
+ "babel-loader": "^8.2.3",
55
55
  "babel-plugin-dynamic-import-node": "2.3.0",
56
- "boxen": "^5.0.1",
57
- "chokidar": "^3.5.2",
58
- "clean-css": "^5.1.5",
56
+ "boxen": "^6.2.1",
57
+ "chokidar": "^3.5.3",
58
+ "clean-css": "^5.2.4",
59
+ "cli-table3": "^0.6.1",
60
+ "combine-promises": "^1.1.0",
59
61
  "commander": "^5.1.0",
60
- "copy-webpack-plugin": "^10.2.0",
61
- "core-js": "^3.18.0",
62
- "css-loader": "^6.5.1",
63
- "css-minimizer-webpack-plugin": "^3.3.1",
64
- "cssnano": "^5.0.8",
62
+ "copy-webpack-plugin": "^10.2.4",
63
+ "core-js": "^3.21.1",
64
+ "css-loader": "^6.6.0",
65
+ "css-minimizer-webpack-plugin": "^3.4.1",
66
+ "cssnano": "^5.0.17",
65
67
  "del": "^6.0.0",
66
68
  "detect-port": "^1.3.0",
67
69
  "escape-html": "^1.0.3",
68
70
  "eta": "^1.12.3",
69
71
  "file-loader": "^6.2.0",
70
- "fs-extra": "^10.0.0",
71
- "html-minifier-terser": "^6.0.2",
72
+ "fs-extra": "^10.0.1",
73
+ "html-minifier-terser": "^6.1.0",
72
74
  "html-tags": "^3.1.0",
73
- "html-webpack-plugin": "^5.4.0",
75
+ "html-webpack-plugin": "^5.5.0",
74
76
  "import-fresh": "^3.3.0",
75
77
  "is-root": "^2.1.0",
76
78
  "leven": "^3.1.0",
77
- "lodash": "^4.17.20",
78
- "mini-css-extract-plugin": "^1.6.0",
79
+ "lodash": "^4.17.21",
80
+ "mini-css-extract-plugin": "^2.5.3",
79
81
  "nprogress": "^0.2.0",
80
- "postcss": "^8.3.7",
81
- "postcss-loader": "^6.1.1",
82
- "prompts": "^2.4.1",
82
+ "postcss": "^8.4.6",
83
+ "postcss-loader": "^6.2.1",
84
+ "prompts": "^2.4.2",
83
85
  "react-dev-utils": "^12.0.0",
84
- "react-helmet": "^6.1.0",
86
+ "react-helmet-async": "^1.2.3",
85
87
  "react-loadable": "npm:@docusaurus/react-loadable@5.5.2",
86
88
  "react-loadable-ssr-addon-v5-slorber": "^1.0.1",
87
89
  "react-router": "^5.2.0",
@@ -91,35 +93,33 @@
91
93
  "rtl-detect": "^1.0.4",
92
94
  "semver": "^7.3.4",
93
95
  "serve-handler": "^6.1.3",
94
- "shelljs": "^0.8.4",
95
- "strip-ansi": "^6.0.0",
96
- "terser-webpack-plugin": "^5.2.4",
96
+ "shelljs": "^0.8.5",
97
+ "terser-webpack-plugin": "^5.3.1",
97
98
  "tslib": "^2.3.1",
98
99
  "update-notifier": "^5.1.0",
99
100
  "url-loader": "^4.1.1",
100
- "wait-on": "^6.0.0",
101
- "webpack": "^5.61.0",
102
- "webpack-bundle-analyzer": "^4.4.2",
103
- "webpack-dev-server": "^4.7.1",
101
+ "wait-on": "^6.0.1",
102
+ "webpack": "^5.69.1",
103
+ "webpack-bundle-analyzer": "^4.5.0",
104
+ "webpack-dev-server": "^4.7.4",
104
105
  "webpack-merge": "^5.8.0",
105
106
  "webpackbar": "^5.0.2"
106
107
  },
107
108
  "devDependencies": {
108
- "@docusaurus/module-type-aliases": "2.0.0-beta.15",
109
- "@docusaurus/types": "2.0.0-beta.15",
110
- "@types/copy-webpack-plugin": "^8.0.1",
111
- "@types/detect-port": "^1.3.0",
112
- "@types/mini-css-extract-plugin": "^1.4.3",
109
+ "@docusaurus/module-type-aliases": "2.0.0-beta.16",
110
+ "@docusaurus/types": "2.0.0-beta.16",
111
+ "@types/detect-port": "^1.3.2",
113
112
  "@types/nprogress": "^0.2.0",
114
- "@types/react-dom": "^17.0.9",
115
- "@types/react-helmet": "^6.0.0",
116
- "@types/react-router-config": "^5.0.1",
113
+ "@types/react-dom": "^17.0.11",
114
+ "@types/react-router-config": "^5.0.6",
117
115
  "@types/rtl-detect": "^1.0.0",
118
116
  "@types/serve-handler": "^6.1.1",
119
- "@types/wait-on": "^5.2.0",
117
+ "@types/update-notifier": "^5.1.0",
118
+ "@types/wait-on": "^5.3.1",
120
119
  "@types/webpack-bundle-analyzer": "^4.4.1",
121
120
  "react-test-renderer": "^17.0.2",
122
- "tmp-promise": "^3.0.2"
121
+ "tmp-promise": "^3.0.3",
122
+ "tree-node-cli": "^1.5.2"
123
123
  },
124
124
  "peerDependencies": {
125
125
  "react": "^16.8.4 || ^17.0.0",
@@ -128,5 +128,5 @@
128
128
  "engines": {
129
129
  "node": ">=14"
130
130
  },
131
- "gitHead": "6cfad16436c07d8d11e5c2e1486dc59afd483e33"
131
+ "gitHead": "eb43c4d4f95a4fb97dc9bb9dc615413e0dc2e1e7"
132
132
  }
package/bin/beforeCli.js DELETED
@@ -1,124 +0,0 @@
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
- // @ts-check
9
-
10
- const logger = require('@docusaurus/logger').default;
11
- const fs = require('fs-extra');
12
- const semver = require('semver');
13
- const path = require('path');
14
- const updateNotifier = require('update-notifier');
15
- const boxen = require('boxen');
16
-
17
- const {
18
- name,
19
- version,
20
- engines: {node: requiredVersion},
21
- } = require('../package.json');
22
-
23
- // Notify user if @docusaurus packages is outdated
24
- //
25
- // Note: this is a 2-step process to avoid delaying cli usage by awaiting a response:
26
- // - 1st run: trigger background job to check releases + store result
27
- // - 2nd run: display potential update to users
28
- //
29
- // Note: even if the
30
- //
31
- // cache data is stored in ~/.config/configstore/update-notifier-@docusaurus
32
- //
33
- const notifier = updateNotifier({
34
- pkg: {
35
- name,
36
- version,
37
- },
38
- // Check is in background so it's fine to use a small value like 1h
39
- // Use 0 for debugging
40
- updateCheckInterval: 1000 * 60 * 60,
41
- // updateCheckInterval: 0
42
- });
43
-
44
- // Hacky way to ensure we check for updates on first run
45
- // Note: the notification will only happen in the 2nd run
46
- // See https://github.com/yeoman/update-notifier/issues/209
47
- try {
48
- if (
49
- notifier.config &&
50
- !notifier.disabled &&
51
- Date.now() - notifier.config.get('lastUpdateCheck') < 50
52
- ) {
53
- notifier.config.set('lastUpdateCheck', 0);
54
- notifier.check();
55
- }
56
- } catch (e) {
57
- // Do not stop cli if this fails, see https://github.com/facebook/docusaurus/issues/5400
58
- logger.error(e);
59
- }
60
-
61
- // We don't want to display update message for canary releases
62
- // See https://github.com/facebook/docusaurus/issues/5378
63
- function ignoreUpdate(update) {
64
- const isCanaryRelease =
65
- update && update.current && update.current.startsWith('0.0.0');
66
- return isCanaryRelease;
67
- }
68
-
69
- if (
70
- notifier.config &&
71
- notifier.update &&
72
- semver.lt(notifier.update.current, notifier.update.latest)
73
- ) {
74
- // Because notifier clears cached data after reading it, leading to notifier not consistently displaying the update
75
- // See https://github.com/yeoman/update-notifier/issues/209
76
- notifier.config.set('update', notifier.update);
77
-
78
- if (ignoreUpdate(notifier.update)) {
79
- // @ts-expect-error: it works
80
- return;
81
- }
82
-
83
- // eslint-disable-next-line import/no-dynamic-require, global-require
84
- const sitePkg = require(path.resolve(process.cwd(), 'package.json'));
85
- const siteDocusaurusPackagesForUpdate = Object.keys({
86
- ...sitePkg.dependencies,
87
- ...sitePkg.devDependencies,
88
- })
89
- .filter((p) => p.startsWith('@docusaurus'))
90
- .map((p) => p.concat('@latest'))
91
- .join(' ');
92
- const isYarnUsed = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'));
93
- const upgradeCommand = isYarnUsed
94
- ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
95
- : `npm i ${siteDocusaurusPackagesForUpdate}`;
96
-
97
- /** @type {import('boxen').Options} */
98
- const boxenOptions = {
99
- padding: 1,
100
- margin: 1,
101
- align: 'center',
102
- borderColor: 'yellow',
103
- borderStyle: 'round',
104
- };
105
-
106
- const docusaurusUpdateMessage = boxen(
107
- `Update available ${logger.dim(
108
- `${notifier.update.current}`,
109
- )} → ${logger.green(`${notifier.update.latest}`)}
110
-
111
- To upgrade Docusaurus packages with the latest version, run the following command:
112
- ${logger.code(upgradeCommand)}`,
113
- boxenOptions,
114
- );
115
-
116
- console.log(docusaurusUpdateMessage);
117
- }
118
-
119
- // notify user if node version needs to be updated
120
- if (!semver.satisfies(process.version, requiredVersion)) {
121
- logger.error('Minimum Node.js version not met :(');
122
- logger.info`You are using Node.js number=${process.version}, Requirement: Node.js number=${requiredVersion}.`;
123
- process.exit(1);
124
- }
@@ -1,9 +0,0 @@
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
- import type { PluginConfig } from '@docusaurus/types';
8
- export declare function getPluginNames(plugins: PluginConfig[]): string[];
9
- export default function swizzle(siteDir: string, themeName?: string, componentName?: string, typescript?: boolean, danger?: boolean): Promise<void>;
@@ -1,236 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Facebook, Inc. and its affiliates.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.getPluginNames = void 0;
10
- const tslib_1 = require("tslib");
11
- const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
12
- const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
13
- const import_fresh_1 = (0, tslib_1.__importDefault)(require("import-fresh"));
14
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
15
- const leven_1 = (0, tslib_1.__importDefault)(require("leven"));
16
- const lodash_1 = require("lodash");
17
- const utils_1 = require("@docusaurus/utils");
18
- const server_1 = require("../server");
19
- const init_1 = (0, tslib_1.__importDefault)(require("../server/plugins/init"));
20
- const utils_validation_1 = require("@docusaurus/utils-validation");
21
- function getPluginNames(plugins) {
22
- return plugins
23
- .filter((plugin) => typeof plugin === 'string' ||
24
- (Array.isArray(plugin) && typeof plugin[0] === 'string'))
25
- .map((plugin) => {
26
- const pluginPath = Array.isArray(plugin) ? plugin[0] : plugin;
27
- if (typeof pluginPath === 'string') {
28
- let packagePath = path_1.default.dirname(pluginPath);
29
- while (packagePath) {
30
- if (fs_extra_1.default.existsSync(path_1.default.join(packagePath, 'package.json'))) {
31
- break;
32
- }
33
- else {
34
- packagePath = path_1.default.dirname(packagePath);
35
- }
36
- }
37
- if (packagePath === '.') {
38
- return pluginPath;
39
- }
40
- return (0, import_fresh_1.default)(path_1.default.join(packagePath, 'package.json')).name;
41
- }
42
- return '';
43
- })
44
- .filter((plugin) => plugin !== '');
45
- }
46
- exports.getPluginNames = getPluginNames;
47
- const formatComponentName = (componentName) => componentName
48
- .replace(/(\/|\\)index\.(js|tsx|ts|jsx)/, '')
49
- .replace(/\.(js|tsx|ts|jsx)/, '');
50
- function readComponent(themePath) {
51
- function walk(dir) {
52
- let results = [];
53
- const list = fs_extra_1.default.readdirSync(dir);
54
- list.forEach((file) => {
55
- const fullPath = path_1.default.join(dir, file);
56
- const stat = fs_extra_1.default.statSync(fullPath);
57
- if (stat && stat.isDirectory()) {
58
- results = results.concat(walk(fullPath));
59
- }
60
- else if (!/\.css|\.d\.ts|\.d\.map/.test(fullPath)) {
61
- results.push(fullPath);
62
- }
63
- });
64
- return results;
65
- }
66
- return walk(themePath).map((filePath) => formatComponentName(path_1.default.relative(themePath, filePath)));
67
- }
68
- // load components from theme based on configurations
69
- function getComponentName(themePath, plugin, danger) {
70
- var _a, _b;
71
- // support both commonjs and ES style exports
72
- const getSwizzleComponentList = (_b = (_a = plugin.default) === null || _a === void 0 ? void 0 : _a.getSwizzleComponentList) !== null && _b !== void 0 ? _b : plugin.getSwizzleComponentList;
73
- if (getSwizzleComponentList) {
74
- const allowedComponent = getSwizzleComponentList();
75
- if (danger) {
76
- return readComponent(themePath);
77
- }
78
- return allowedComponent;
79
- }
80
- return readComponent(themePath);
81
- }
82
- function themeComponents(themePath, plugin) {
83
- const components = colorCode(themePath, plugin);
84
- if (components.length === 0) {
85
- return 'No component to swizzle.';
86
- }
87
- return `Theme components available for swizzle.
88
-
89
- ${logger_1.default.green(logger_1.default.bold('green =>'))} safe: lower breaking change risk
90
- ${logger_1.default.red(logger_1.default.bold('red =>'))} unsafe: higher breaking change risk
91
-
92
- ${components.join('\n')}
93
- `;
94
- }
95
- function colorCode(themePath, plugin) {
96
- var _a, _b;
97
- // support both commonjs and ES style exports
98
- const getSwizzleComponentList = (_b = (_a = plugin.default) === null || _a === void 0 ? void 0 : _a.getSwizzleComponentList) !== null && _b !== void 0 ? _b : plugin.getSwizzleComponentList;
99
- const components = readComponent(themePath);
100
- const allowedComponent = getSwizzleComponentList
101
- ? getSwizzleComponentList()
102
- : [];
103
- const [greenComponents, redComponents] = (0, lodash_1.partition)(components, (comp) => allowedComponent.includes(comp));
104
- return [
105
- ...greenComponents.map((component) => `${logger_1.default.green(logger_1.default.bold('safe:'))} ${component}`),
106
- ...redComponents.map((component) => `${logger_1.default.red(logger_1.default.bold('unsafe:'))} ${component}`),
107
- ];
108
- }
109
- async function swizzle(siteDir, themeName, componentName, typescript, danger) {
110
- var _a, _b, _c, _d, _e;
111
- const context = await (0, server_1.loadContext)(siteDir);
112
- const pluginConfigs = (0, server_1.loadPluginConfigs)(context);
113
- const pluginNames = getPluginNames(pluginConfigs);
114
- const plugins = await (0, init_1.default)({
115
- pluginConfigs,
116
- context,
117
- });
118
- const themeNames = pluginNames.filter((_, index) => typescript
119
- ? plugins[index].getTypeScriptThemePath
120
- : plugins[index].getThemePath);
121
- if (!themeName) {
122
- logger_1.default.info `Themes available for swizzle: name=${themeNames}`;
123
- return;
124
- }
125
- let pluginModule;
126
- try {
127
- pluginModule = (0, import_fresh_1.default)(themeName);
128
- }
129
- catch {
130
- let suggestion;
131
- themeNames.forEach((name) => {
132
- if ((0, leven_1.default)(name, themeName) < 4) {
133
- suggestion = name;
134
- }
135
- });
136
- logger_1.default.error `Theme name=${themeName} not found. ${suggestion
137
- ? logger_1.default.interpolate `Did you mean name=${suggestion}?`
138
- : logger_1.default.interpolate `Themes available for swizzle: ${themeNames}`}`;
139
- process.exit(1);
140
- }
141
- let pluginOptions = {};
142
- const resolvedThemeName = require.resolve(themeName);
143
- // find the plugin from list of plugin and get options if specified
144
- pluginConfigs.forEach((pluginConfig) => {
145
- // plugin can be a [string], [string,object] or string.
146
- if (Array.isArray(pluginConfig) && typeof pluginConfig[0] === 'string') {
147
- if (require.resolve(pluginConfig[0]) === resolvedThemeName) {
148
- if (pluginConfig.length === 2) {
149
- const [, options] = pluginConfig;
150
- pluginOptions = options;
151
- }
152
- }
153
- }
154
- });
155
- // support both commonjs and ES style exports
156
- const validateOptions = (_b = (_a = pluginModule.default) === null || _a === void 0 ? void 0 : _a.validateOptions) !== null && _b !== void 0 ? _b : pluginModule.validateOptions;
157
- if (validateOptions) {
158
- pluginOptions = validateOptions({
159
- validate: utils_validation_1.normalizePluginOptions,
160
- options: pluginOptions,
161
- });
162
- }
163
- // support both commonjs and ES style exports
164
- const plugin = (_c = pluginModule.default) !== null && _c !== void 0 ? _c : pluginModule;
165
- const pluginInstance = await plugin(context, pluginOptions);
166
- const themePath = typescript
167
- ? (_d = pluginInstance.getTypeScriptThemePath) === null || _d === void 0 ? void 0 : _d.call(pluginInstance)
168
- : (_e = pluginInstance.getThemePath) === null || _e === void 0 ? void 0 : _e.call(pluginInstance);
169
- if (!themePath) {
170
- logger_1.default.warn(typescript
171
- ? logger_1.default.interpolate `name=${themeName} does not provide TypeScript theme code via ${'getTypeScriptThemePath()'}.`
172
- : logger_1.default.interpolate `name=${themeName} does not provide any theme code.`);
173
- process.exit(1);
174
- }
175
- if (!componentName) {
176
- logger_1.default.info(themeComponents(themePath, pluginModule));
177
- return;
178
- }
179
- const components = getComponentName(themePath, pluginModule, Boolean(danger));
180
- const formattedComponentName = formatComponentName(componentName);
181
- const isComponentExists = components.find((component) => component === formattedComponentName);
182
- let mostSuitableComponent = componentName;
183
- if (!isComponentExists) {
184
- let mostSuitableMatch = componentName;
185
- let score = formattedComponentName.length;
186
- components.forEach((component) => {
187
- if (component.toLowerCase() === formattedComponentName.toLowerCase()) {
188
- // may be components with same lowercase key, try to match closest component
189
- const currentScore = (0, leven_1.default)(formattedComponentName, component);
190
- if (currentScore < score) {
191
- score = currentScore;
192
- mostSuitableMatch = component;
193
- }
194
- }
195
- });
196
- if (mostSuitableMatch !== componentName) {
197
- mostSuitableComponent = mostSuitableMatch;
198
- logger_1.default.error `Component name=${componentName} doesn't exist.`;
199
- logger_1.default.info `name=${mostSuitableComponent} is swizzled instead of name=${componentName}.`;
200
- }
201
- }
202
- let fromPath = path_1.default.join(themePath, mostSuitableComponent);
203
- let toPath = path_1.default.resolve(siteDir, utils_1.THEME_PATH, mostSuitableComponent);
204
- // Handle single TypeScript/JavaScript file only.
205
- // E.g: if <fromPath> does not exist, we try to swizzle <fromPath>.(ts|tsx|js) instead
206
- if (!fs_extra_1.default.existsSync(fromPath)) {
207
- if (fs_extra_1.default.existsSync(`${fromPath}.ts`)) {
208
- [fromPath, toPath] = [`${fromPath}.ts`, `${toPath}.ts`];
209
- }
210
- else if (fs_extra_1.default.existsSync(`${fromPath}.tsx`)) {
211
- [fromPath, toPath] = [`${fromPath}.tsx`, `${toPath}.tsx`];
212
- }
213
- else if (fs_extra_1.default.existsSync(`${fromPath}.js`)) {
214
- [fromPath, toPath] = [`${fromPath}.js`, `${toPath}.js`];
215
- }
216
- else {
217
- let suggestion;
218
- components.forEach((name) => {
219
- if ((0, leven_1.default)(name, mostSuitableComponent) < 3) {
220
- suggestion = name;
221
- }
222
- });
223
- logger_1.default.error `Component name=${mostSuitableComponent} not found. ${suggestion
224
- ? logger_1.default.interpolate `Did you mean name=${suggestion} ?`
225
- : themeComponents(themePath, pluginModule)}`;
226
- process.exit(1);
227
- }
228
- }
229
- if (!components.includes(mostSuitableComponent) && !danger) {
230
- logger_1.default.error `name=${mostSuitableComponent} is an internal component and has a higher breaking change probability. If you want to swizzle it, use the code=${'--danger'} flag.`;
231
- process.exit(1);
232
- }
233
- await fs_extra_1.default.copy(fromPath, toPath);
234
- logger_1.default.success `Copied code=${mostSuitableComponent ? `${themeName} ${mostSuitableComponent}` : themeName} to path=${path_1.default.relative(process.cwd(), toPath)}.`;
235
- }
236
- exports.default = swizzle;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Facebook, Inc. and its affiliates.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- const tslib_1 = require("tslib");
10
- const __1 = require("..");
11
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
12
- describe('getPluginVersion', () => {
13
- it('Can detect external packages plugins versions of correctly.', () => {
14
- expect((0, __1.getPluginVersion)(path_1.default.join(__dirname, '..', '__fixtures__', 'dummy-plugin.js'),
15
- // Make the plugin appear external.
16
- path_1.default.join(__dirname, '..', '..', '..', '..', '..', '..', 'website'))).toEqual({ type: 'package', version: 'random-version' });
17
- });
18
- it('Can detect project plugins versions correctly.', () => {
19
- expect((0, __1.getPluginVersion)(path_1.default.join(__dirname, '..', '__fixtures__', 'dummy-plugin.js'),
20
- // Make the plugin appear project local.
21
- path_1.default.join(__dirname, '..', '__fixtures__'))).toEqual({ type: 'project' });
22
- });
23
- it('Can detect local packages versions correctly.', () => {
24
- expect((0, __1.getPluginVersion)('/', '/')).toEqual({ type: 'local' });
25
- });
26
- });