@docusaurus/core 0.0.0-6069 → 0.0.0-6074

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.
@@ -99,14 +99,17 @@ async function buildLocale({ siteDir, locale, cliOptions, }) {
99
99
  const { props } = site;
100
100
  const { outDir, plugins, siteConfig } = props;
101
101
  const router = siteConfig.future.experimental_router;
102
+ const configureWebpackUtils = await (0, configure_1.createConfigureWebpackUtils)({ siteConfig });
102
103
  // We can build the 2 configs in parallel
103
104
  const [{ clientConfig, clientManifestPath }, { serverConfig, serverBundlePath }] = await logger_1.PerfLogger.async('Creating webpack configs', () => Promise.all([
104
105
  getBuildClientConfig({
105
106
  props,
106
107
  cliOptions,
108
+ configureWebpackUtils,
107
109
  }),
108
110
  getBuildServerConfig({
109
111
  props,
112
+ configureWebpackUtils,
110
113
  }),
111
114
  ]));
112
115
  // Run webpack to build JS bundle (client) and static html files (server).
@@ -189,12 +192,13 @@ async function executeBrokenLinksCheck({ props: { routes, siteConfig: { onBroken
189
192
  onBrokenAnchors,
190
193
  });
191
194
  }
192
- async function getBuildClientConfig({ props, cliOptions, }) {
195
+ async function getBuildClientConfig({ props, cliOptions, configureWebpackUtils, }) {
193
196
  const { plugins } = props;
194
197
  const result = await (0, client_1.createBuildClientConfig)({
195
198
  props,
196
199
  minify: cliOptions.minify ?? true,
197
200
  faster: props.siteConfig.future.experimental_faster,
201
+ configureWebpackUtils,
198
202
  bundleAnalyzer: cliOptions.bundleAnalyzer ?? false,
199
203
  });
200
204
  let { config } = result;
@@ -202,25 +206,22 @@ async function getBuildClientConfig({ props, cliOptions, }) {
202
206
  plugins,
203
207
  config,
204
208
  isServer: false,
205
- utils: await (0, configure_1.createConfigureWebpackUtils)({
206
- siteConfig: props.siteConfig,
207
- }),
209
+ configureWebpackUtils,
208
210
  });
209
211
  return { clientConfig: config, clientManifestPath: result.clientManifestPath };
210
212
  }
211
- async function getBuildServerConfig({ props }) {
213
+ async function getBuildServerConfig({ props, configureWebpackUtils, }) {
212
214
  const { plugins } = props;
213
215
  const result = await (0, server_1.default)({
214
216
  props,
217
+ configureWebpackUtils,
215
218
  });
216
219
  let { config } = result;
217
220
  config = (0, configure_1.executePluginsConfigureWebpack)({
218
221
  plugins,
219
222
  config,
220
223
  isServer: true,
221
- utils: await (0, configure_1.createConfigureWebpackUtils)({
222
- siteConfig: props.siteConfig,
223
- }),
224
+ configureWebpackUtils,
224
225
  });
225
226
  return { serverConfig: config, serverBundlePath: result.serverBundlePath };
226
227
  }
@@ -97,27 +97,32 @@ async function createDevServerConfig({ cliOptions, props, host, port, }) {
97
97
  },
98
98
  };
99
99
  }
100
- async function getStartClientConfig({ props, minify, poll, }) {
101
- const { plugins, siteConfig } = props;
100
+ async function getStartClientConfig({ props, minify, poll, configureWebpackUtils, }) {
101
+ const { plugins } = props;
102
102
  let { clientConfig: config } = await (0, client_1.createStartClientConfig)({
103
103
  props,
104
104
  minify,
105
105
  faster: props.siteConfig.future.experimental_faster,
106
106
  poll,
107
+ configureWebpackUtils,
107
108
  });
108
109
  config = (0, configure_1.executePluginsConfigureWebpack)({
109
110
  plugins,
110
111
  config,
111
112
  isServer: false,
112
- utils: await (0, configure_1.createConfigureWebpackUtils)({ siteConfig }),
113
+ configureWebpackUtils,
113
114
  });
114
115
  return config;
115
116
  }
116
117
  async function createWebpackDevServer({ props, cliOptions, openUrlContext, }) {
118
+ const configureWebpackUtils = await (0, configure_1.createConfigureWebpackUtils)({
119
+ siteConfig: props.siteConfig,
120
+ });
117
121
  const config = await getStartClientConfig({
118
122
  props,
119
123
  minify: cliOptions.minify ?? true,
120
124
  poll: cliOptions.poll,
125
+ configureWebpackUtils,
121
126
  });
122
127
  const compiler = (0, webpack_1.default)(config);
123
128
  registerWebpackE2ETestHook(compiler);
@@ -26,12 +26,14 @@ exports.DEFAULT_FASTER_CONFIG = {
26
26
  swcJsLoader: false,
27
27
  swcJsMinimizer: false,
28
28
  mdxCrossCompilerCache: false,
29
+ rspackBundler: false,
29
30
  };
30
31
  // When using the "faster: true" shortcut
31
32
  exports.DEFAULT_FASTER_CONFIG_TRUE = {
32
33
  swcJsLoader: true,
33
34
  swcJsMinimizer: true,
34
35
  mdxCrossCompilerCache: true,
36
+ rspackBundler: true,
35
37
  };
36
38
  exports.DEFAULT_FUTURE_CONFIG = {
37
39
  experimental_faster: exports.DEFAULT_FASTER_CONFIG,
@@ -147,6 +149,7 @@ const FASTER_CONFIG_SCHEMA = utils_validation_1.Joi.alternatives()
147
149
  swcJsLoader: utils_validation_1.Joi.boolean().default(exports.DEFAULT_FASTER_CONFIG.swcJsLoader),
148
150
  swcJsMinimizer: utils_validation_1.Joi.boolean().default(exports.DEFAULT_FASTER_CONFIG.swcJsMinimizer),
149
151
  mdxCrossCompilerCache: utils_validation_1.Joi.boolean().default(exports.DEFAULT_FASTER_CONFIG.mdxCrossCompilerCache),
152
+ rspackBundler: utils_validation_1.Joi.boolean().default(exports.DEFAULT_FASTER_CONFIG.rspackBundler),
150
153
  }), utils_validation_1.Joi.boolean()
151
154
  .required()
152
155
  .custom((bool) => bool ? exports.DEFAULT_FASTER_CONFIG_TRUE : exports.DEFAULT_FASTER_CONFIG))
@@ -5,12 +5,13 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import type { Configuration } from 'webpack';
8
- import type { FasterConfig, Props } from '@docusaurus/types';
8
+ import type { ConfigureWebpackUtils, FasterConfig, Props } from '@docusaurus/types';
9
9
  export declare const clientDir: string;
10
10
  export declare function excludeJS(modulePath: string): boolean;
11
- export declare function createBaseConfig({ props, isServer, minify, faster, }: {
11
+ export declare function createBaseConfig({ props, isServer, minify, faster, configureWebpackUtils, }: {
12
12
  props: Props;
13
13
  isServer: boolean;
14
14
  minify: boolean;
15
15
  faster: FasterConfig;
16
+ configureWebpackUtils: ConfigureWebpackUtils;
16
17
  }): Promise<Configuration>;
@@ -12,11 +12,11 @@ exports.createBaseConfig = createBaseConfig;
12
12
  const tslib_1 = require("tslib");
13
13
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
14
14
  const path_1 = tslib_1.__importDefault(require("path"));
15
- const mini_css_extract_plugin_1 = tslib_1.__importDefault(require("mini-css-extract-plugin"));
16
15
  const utils_1 = require("@docusaurus/utils");
17
16
  const utils_2 = require("./utils");
18
17
  const minification_1 = require("./minification");
19
18
  const aliases_1 = require("./aliases");
19
+ const currentBundler_1 = require("./currentBundler");
20
20
  const CSS_REGEX = /\.css$/i;
21
21
  const CSS_MODULE_REGEX = /\.module\.css$/i;
22
22
  exports.clientDir = path_1.default.join(__dirname, '..', 'client');
@@ -42,7 +42,7 @@ function excludeJS(modulePath) {
42
42
  !/docusaurus(?:(?!node_modules).)*\.jsx?$/.test(modulePath) &&
43
43
  !LibrariesToTranspileRegex.test(modulePath));
44
44
  }
45
- async function createBaseConfig({ props, isServer, minify, faster, }) {
45
+ async function createBaseConfig({ props, isServer, minify, faster, configureWebpackUtils, }) {
46
46
  const { outDir, siteDir, siteConfig, siteConfigPath, baseUrl, generatedFilesDir, routesPaths, siteMetadata, plugins, } = props;
47
47
  const totalPages = routesPaths.length;
48
48
  const isProd = process.env.NODE_ENV === 'production';
@@ -52,6 +52,9 @@ async function createBaseConfig({ props, isServer, minify, faster, }) {
52
52
  const mode = isProd ? 'production' : 'development';
53
53
  const themeAliases = await (0, aliases_1.loadThemeAliases)({ siteDir, plugins });
54
54
  const createJsLoader = await (0, utils_2.createJsLoaderFactory)({ siteConfig });
55
+ const CSSExtractPlugin = await (0, currentBundler_1.getCSSExtractPlugin)({
56
+ currentBundler: configureWebpackUtils.currentBundler,
57
+ });
55
58
  return {
56
59
  mode,
57
60
  name,
@@ -185,7 +188,7 @@ async function createBaseConfig({ props, isServer, minify, faster, }) {
185
188
  {
186
189
  test: CSS_REGEX,
187
190
  exclude: CSS_MODULE_REGEX,
188
- use: (0, utils_2.getStyleLoaders)(isServer, {
191
+ use: configureWebpackUtils.getStyleLoaders(isServer, {
189
192
  importLoaders: 1,
190
193
  sourceMap: !isProd,
191
194
  }),
@@ -194,7 +197,7 @@ async function createBaseConfig({ props, isServer, minify, faster, }) {
194
197
  // using the extension .module.css
195
198
  {
196
199
  test: CSS_MODULE_REGEX,
197
- use: (0, utils_2.getStyleLoaders)(isServer, {
200
+ use: configureWebpackUtils.getStyleLoaders(isServer, {
198
201
  modules: {
199
202
  // Using the same CSS Module class pattern in dev/prod on purpose
200
203
  // See https://github.com/facebook/docusaurus/pull/10423
@@ -208,7 +211,7 @@ async function createBaseConfig({ props, isServer, minify, faster, }) {
208
211
  ],
209
212
  },
210
213
  plugins: [
211
- new mini_css_extract_plugin_1.default({
214
+ new CSSExtractPlugin({
212
215
  filename: isProd
213
216
  ? 'assets/css/[name].[contenthash:8].css'
214
217
  : '[name].css',
@@ -4,20 +4,22 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { FasterConfig, Props } from '@docusaurus/types';
7
+ import type { ConfigureWebpackUtils, FasterConfig, Props } from '@docusaurus/types';
8
8
  import type { Configuration } from 'webpack';
9
- export declare function createStartClientConfig({ props, minify, poll, faster, }: {
9
+ export declare function createStartClientConfig({ props, minify, poll, faster, configureWebpackUtils, }: {
10
10
  props: Props;
11
11
  minify: boolean;
12
12
  poll: number | boolean | undefined;
13
13
  faster: FasterConfig;
14
+ configureWebpackUtils: ConfigureWebpackUtils;
14
15
  }): Promise<{
15
16
  clientConfig: Configuration;
16
17
  }>;
17
- export declare function createBuildClientConfig({ props, minify, faster, bundleAnalyzer, }: {
18
+ export declare function createBuildClientConfig({ props, minify, faster, configureWebpackUtils, bundleAnalyzer, }: {
18
19
  props: Props;
19
20
  minify: boolean;
20
21
  faster: FasterConfig;
22
+ configureWebpackUtils: ConfigureWebpackUtils;
21
23
  bundleAnalyzer: boolean;
22
24
  }): Promise<{
23
25
  config: Configuration;
@@ -21,12 +21,13 @@ const ChunkAssetPlugin_1 = tslib_1.__importDefault(require("./plugins/ChunkAsset
21
21
  const CleanWebpackPlugin_1 = tslib_1.__importDefault(require("./plugins/CleanWebpackPlugin"));
22
22
  const ForceTerminatePlugin_1 = tslib_1.__importDefault(require("./plugins/ForceTerminatePlugin"));
23
23
  const StaticDirectoriesCopyPlugin_1 = require("./plugins/StaticDirectoriesCopyPlugin");
24
- async function createBaseClientConfig({ props, hydrate, minify, faster, }) {
24
+ async function createBaseClientConfig({ props, hydrate, minify, faster, configureWebpackUtils, }) {
25
25
  const baseConfig = await (0, base_1.createBaseConfig)({
26
26
  props,
27
27
  isServer: false,
28
28
  minify,
29
29
  faster,
30
+ configureWebpackUtils,
30
31
  });
31
32
  return (0, webpack_merge_1.default)(baseConfig, {
32
33
  // Useless, disabled on purpose (errors on existing sites with no
@@ -47,18 +48,22 @@ async function createBaseClientConfig({ props, hydrate, minify, faster, }) {
47
48
  new webpackbar_1.default({
48
49
  name: 'Client',
49
50
  }),
50
- await (0, StaticDirectoriesCopyPlugin_1.createStaticDirectoriesCopyPlugin)({ props }),
51
+ await (0, StaticDirectoriesCopyPlugin_1.createStaticDirectoriesCopyPlugin)({
52
+ props,
53
+ currentBundler: configureWebpackUtils.currentBundler,
54
+ }),
51
55
  ].filter(Boolean),
52
56
  });
53
57
  }
54
58
  // client config when running "docusaurus start"
55
- async function createStartClientConfig({ props, minify, poll, faster, }) {
59
+ async function createStartClientConfig({ props, minify, poll, faster, configureWebpackUtils, }) {
56
60
  const { siteConfig, headTags, preBodyTags, postBodyTags } = props;
57
61
  const clientConfig = (0, webpack_merge_1.default)(await createBaseClientConfig({
58
62
  props,
59
63
  minify,
60
64
  hydrate: false,
61
65
  faster,
66
+ configureWebpackUtils,
62
67
  }), {
63
68
  watchOptions: {
64
69
  ignored: /node_modules\/(?!@docusaurus)/,
@@ -85,7 +90,7 @@ async function createStartClientConfig({ props, minify, poll, faster, }) {
85
90
  return { clientConfig };
86
91
  }
87
92
  // client config when running "docusaurus build"
88
- async function createBuildClientConfig({ props, minify, faster, bundleAnalyzer, }) {
93
+ async function createBuildClientConfig({ props, minify, faster, configureWebpackUtils, bundleAnalyzer, }) {
89
94
  // Apply user webpack config.
90
95
  const { generatedFilesDir, siteConfig } = props;
91
96
  const router = siteConfig.future.experimental_router;
@@ -93,7 +98,13 @@ async function createBuildClientConfig({ props, minify, faster, bundleAnalyzer,
93
98
  // This is because it will always be a client-rendered React app
94
99
  const hydrate = router !== 'hash';
95
100
  const clientManifestPath = path_1.default.join(generatedFilesDir, 'client-manifest.json');
96
- const config = (0, webpack_merge_1.default)(await createBaseClientConfig({ props, minify, faster, hydrate }), {
101
+ const config = (0, webpack_merge_1.default)(await createBaseClientConfig({
102
+ props,
103
+ minify,
104
+ faster,
105
+ configureWebpackUtils,
106
+ hydrate,
107
+ }), {
97
108
  plugins: [
98
109
  new ForceTerminatePlugin_1.default(),
99
110
  // Remove/clean build folders before building bundles.
@@ -5,6 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import { createJsLoaderFactory } from './utils';
8
+ import { getCurrentBundler } from './currentBundler';
8
9
  import type { Configuration } from 'webpack';
9
10
  import type { Plugin, ConfigureWebpackUtils, LoadedPlugin } from '@docusaurus/types';
10
11
  /**
@@ -12,7 +13,7 @@ import type { Plugin, ConfigureWebpackUtils, LoadedPlugin } from '@docusaurus/ty
12
13
  * @param config the Docusaurus config
13
14
  */
14
15
  export declare function createConfigureWebpackUtils({ siteConfig, }: {
15
- siteConfig: Parameters<typeof createJsLoaderFactory>[0]['siteConfig'];
16
+ siteConfig: Parameters<typeof createJsLoaderFactory>[0]['siteConfig'] & Parameters<typeof getCurrentBundler>[0]['siteConfig'];
16
17
  }): Promise<ConfigureWebpackUtils>;
17
18
  /**
18
19
  * Helper function to modify webpack config
@@ -23,17 +24,17 @@ export declare function createConfigureWebpackUtils({ siteConfig, }: {
23
24
  * @param content content loaded by the plugin
24
25
  * @returns final/ modified webpack config
25
26
  */
26
- export declare function applyConfigureWebpack({ configureWebpack, config, isServer, utils, content, }: {
27
+ export declare function applyConfigureWebpack({ configureWebpack, config, isServer, configureWebpackUtils, content, }: {
27
28
  configureWebpack: NonNullable<Plugin['configureWebpack']>;
28
29
  config: Configuration;
29
30
  isServer: boolean;
30
- utils: ConfigureWebpackUtils;
31
+ configureWebpackUtils: ConfigureWebpackUtils;
31
32
  content: unknown;
32
33
  }): Configuration;
33
34
  export declare function applyConfigurePostCss(configurePostCss: NonNullable<Plugin['configurePostCss']>, config: Configuration): Configuration;
34
- export declare function executePluginsConfigureWebpack({ plugins, config: configInput, isServer, utils, }: {
35
+ export declare function executePluginsConfigureWebpack({ plugins, config: configInput, isServer, configureWebpackUtils, }: {
35
36
  plugins: LoadedPlugin[];
36
37
  config: Configuration;
37
38
  isServer: boolean;
38
- utils: ConfigureWebpackUtils;
39
+ configureWebpackUtils: ConfigureWebpackUtils;
39
40
  }): Configuration;
@@ -12,14 +12,19 @@ exports.applyConfigurePostCss = applyConfigurePostCss;
12
12
  exports.executePluginsConfigureWebpack = executePluginsConfigureWebpack;
13
13
  const webpack_merge_1 = require("webpack-merge");
14
14
  const utils_1 = require("./utils");
15
+ const currentBundler_1 = require("./currentBundler");
15
16
  /**
16
17
  * Creates convenient utils to inject into the configureWebpack() lifecycle
17
18
  * @param config the Docusaurus config
18
19
  */
19
20
  async function createConfigureWebpackUtils({ siteConfig, }) {
21
+ const currentBundler = await (0, currentBundler_1.getCurrentBundler)({ siteConfig });
22
+ const getStyleLoaders = await (0, utils_1.createStyleLoadersFactory)({ currentBundler });
23
+ const getJSLoader = await (0, utils_1.createJsLoaderFactory)({ siteConfig });
20
24
  return {
21
- getStyleLoaders: utils_1.getStyleLoaders,
22
- getJSLoader: await (0, utils_1.createJsLoaderFactory)({ siteConfig }),
25
+ currentBundler,
26
+ getStyleLoaders,
27
+ getJSLoader,
23
28
  };
24
29
  }
25
30
  /**
@@ -31,9 +36,9 @@ async function createConfigureWebpackUtils({ siteConfig, }) {
31
36
  * @param content content loaded by the plugin
32
37
  * @returns final/ modified webpack config
33
38
  */
34
- function applyConfigureWebpack({ configureWebpack, config, isServer, utils, content, }) {
39
+ function applyConfigureWebpack({ configureWebpack, config, isServer, configureWebpackUtils, content, }) {
35
40
  if (typeof configureWebpack === 'function') {
36
- const { mergeStrategy, ...res } = configureWebpack(config, isServer, utils, content) ?? {};
41
+ const { mergeStrategy, ...res } = configureWebpack(config, isServer, configureWebpackUtils, content) ?? {};
37
42
  const customizeRules = mergeStrategy ?? {};
38
43
  return (0, webpack_merge_1.mergeWithCustomize)({
39
44
  customizeArray: (0, webpack_merge_1.customizeArray)(customizeRules),
@@ -80,7 +85,7 @@ function executePluginsConfigurePostCss({ plugins, config, }) {
80
85
  return resultConfig;
81
86
  }
82
87
  // Plugin Lifecycle - configureWebpack()
83
- function executePluginsConfigureWebpack({ plugins, config: configInput, isServer, utils, }) {
88
+ function executePluginsConfigureWebpack({ plugins, config: configInput, isServer, configureWebpackUtils, }) {
84
89
  let config = configInput;
85
90
  // Step1 - Configure Webpack
86
91
  plugins.forEach((plugin) => {
@@ -90,7 +95,7 @@ function executePluginsConfigureWebpack({ plugins, config: configInput, isServer
90
95
  configureWebpack: configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
91
96
  config,
92
97
  isServer,
93
- utils,
98
+ configureWebpackUtils,
94
99
  content: plugin.content,
95
100
  });
96
101
  }
@@ -0,0 +1,24 @@
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 MiniCssExtractPlugin from 'mini-css-extract-plugin';
8
+ import CopyWebpackPlugin from 'copy-webpack-plugin';
9
+ import type { CurrentBundler, DocusaurusConfig } from '@docusaurus/types';
10
+ type SiteConfigSlice = {
11
+ future: {
12
+ experimental_faster: Pick<DocusaurusConfig['future']['experimental_faster'], 'rspackBundler'>;
13
+ };
14
+ };
15
+ export declare function getCurrentBundler({ siteConfig, }: {
16
+ siteConfig: SiteConfigSlice;
17
+ }): Promise<CurrentBundler>;
18
+ export declare function getCSSExtractPlugin({ currentBundler, }: {
19
+ currentBundler: CurrentBundler;
20
+ }): Promise<typeof MiniCssExtractPlugin>;
21
+ export declare function getCopyPlugin({ currentBundler, }: {
22
+ currentBundler: CurrentBundler;
23
+ }): Promise<typeof CopyWebpackPlugin>;
24
+ export {};
@@ -0,0 +1,42 @@
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.getCurrentBundler = getCurrentBundler;
10
+ exports.getCSSExtractPlugin = getCSSExtractPlugin;
11
+ exports.getCopyPlugin = getCopyPlugin;
12
+ const tslib_1 = require("tslib");
13
+ const webpack_1 = tslib_1.__importDefault(require("webpack"));
14
+ const mini_css_extract_plugin_1 = tslib_1.__importDefault(require("mini-css-extract-plugin"));
15
+ const copy_webpack_plugin_1 = tslib_1.__importDefault(require("copy-webpack-plugin"));
16
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
17
+ function isRspack(siteConfig) {
18
+ return siteConfig.future.experimental_faster.rspackBundler;
19
+ }
20
+ async function getCurrentBundler({ siteConfig, }) {
21
+ if (isRspack(siteConfig)) {
22
+ // TODO add support for Rspack
23
+ logger_1.default.error('Rspack bundler is not supported yet, will use Webpack instead');
24
+ }
25
+ return {
26
+ name: 'webpack',
27
+ instance: webpack_1.default,
28
+ };
29
+ }
30
+ async function getCSSExtractPlugin({ currentBundler, }) {
31
+ if (currentBundler.name === 'rspack') {
32
+ throw new Error('Rspack bundler is not supported yet');
33
+ }
34
+ return mini_css_extract_plugin_1.default;
35
+ }
36
+ async function getCopyPlugin({ currentBundler, }) {
37
+ if (currentBundler.name === 'rspack') {
38
+ throw new Error('Rspack bundler is not supported yet');
39
+ }
40
+ // https://github.com/webpack-contrib/copy-webpack-plugin
41
+ return copy_webpack_plugin_1.default;
42
+ }
@@ -5,17 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import { type Compiler } from 'webpack';
8
- /**
9
- * We modify webpack runtime to add an extra function called
10
- * "__webpack_require__.gca" that will allow us to get the corresponding chunk
11
- * asset for a webpack chunk. Pass it the chunkName or chunkId you want to load.
12
- * For example: if you have a chunk named "my-chunk-name" that will map to
13
- * "/publicPath/0a84b5e7.c8e35c7a.js" as its corresponding output path
14
- * __webpack_require__.gca("my-chunk-name") will return
15
- * "/publicPath/0a84b5e7.c8e35c7a.js"
16
- *
17
- * "gca" stands for "get chunk asset"
18
- */
19
8
  export default class ChunkAssetPlugin {
20
9
  apply(compiler: Compiler): void;
21
10
  }
@@ -8,44 +8,57 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  const tslib_1 = require("tslib");
10
10
  const webpack_1 = tslib_1.__importDefault(require("webpack"));
11
- const pluginName = 'chunk-asset-plugin';
12
- /**
13
- * We modify webpack runtime to add an extra function called
14
- * "__webpack_require__.gca" that will allow us to get the corresponding chunk
15
- * asset for a webpack chunk. Pass it the chunkName or chunkId you want to load.
16
- * For example: if you have a chunk named "my-chunk-name" that will map to
17
- * "/publicPath/0a84b5e7.c8e35c7a.js" as its corresponding output path
18
- * __webpack_require__.gca("my-chunk-name") will return
19
- * "/publicPath/0a84b5e7.c8e35c7a.js"
20
- *
21
- * "gca" stands for "get chunk asset"
11
+ // Adds a custom Docusaurus Webpack runtime function `__webpack_require__.gca`
12
+ // gca = Get Chunk Asset, it converts a chunkName to a JS asset URL
13
+ // It is called in Core client/docusaurus.ts for chunk preloading/prefetching
14
+ // Example: gca("814f3328") = "/baseUrl/assets/js/814f3328.03fcc178.js"
15
+ // See also: https://github.com/facebook/docusaurus/pull/10485
16
+ // The name of the custom Docusaurus Webpack runtime function
17
+ const DocusaurusGetChunkAssetFn = '__webpack_require__.gca';
18
+ const PluginName = 'Docusaurus-ChunkAssetPlugin';
19
+ function generateGetChunkAssetRuntimeCode(chunk) {
20
+ const chunkIdToName = chunk.getChunkMaps(false).name;
21
+ const chunkNameToId = Object.fromEntries(Object.entries(chunkIdToName).map(([chunkId, chunkName]) => [
22
+ chunkName,
23
+ chunkId,
24
+ ]));
25
+ const {
26
+ // publicPath = __webpack_require__.p
27
+ // Example: "/" or "/baseUrl/"
28
+ // https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/PublicPathRuntimeModule.js
29
+ publicPath,
30
+ // getChunkScriptFilename = __webpack_require__.u
31
+ // Example: getChunkScriptFilename("814f3328") = "814f3328.03fcc178.js"
32
+ // https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/GetChunkFilenameRuntimeModule.js
33
+ getChunkScriptFilename, } = webpack_1.default.RuntimeGlobals;
34
+ const code = `// Docusaurus function to get chunk asset
35
+ ${DocusaurusGetChunkAssetFn} = function(chunkId) { chunkId = ${JSON.stringify(chunkNameToId)}[chunkId]||chunkId; return ${publicPath} + ${getChunkScriptFilename}(chunkId); };`;
36
+ return webpack_1.default.Template.asString(code);
37
+ }
38
+ /*
39
+ Note: we previously used `MainTemplate.hooks.requireExtensions.tap()`
40
+ But it will be removed in Webpack 6 and is not supported by Rspack
41
+ So instead we use equivalent code inspired by:
42
+ - https://github.com/webpack/webpack/blob/v5.94.0/lib/RuntimePlugin.js#L462
43
+ - https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/CompatRuntimeModule.js
22
44
  */
23
45
  class ChunkAssetPlugin {
24
46
  apply(compiler) {
25
- compiler.hooks.thisCompilation.tap(pluginName, ({ mainTemplate }) => {
26
- mainTemplate.hooks.requireExtensions.tap(pluginName, (source, chunk) => {
27
- const chunkIdToName = chunk.getChunkMaps(false).name;
28
- const chunkNameToId = Object.fromEntries(Object.entries(chunkIdToName).map(([chunkId, chunkName]) => [
29
- chunkName,
30
- chunkId,
31
- ]));
32
- const buf = [source];
33
- buf.push('// function to get chunk asset');
34
- buf.push(
35
- // If chunkName is passed, we convert it to chunk asset url
36
- // .p => public path url ("/" or "/baseUrl/")
37
- // .u(chunkId) =>
38
- // chunk asset url ("assets/js/x63b64xd.contentHash.js")
39
- // not sure where this is documented, but this link was helpful:
40
- // https://programmer.help/blogs/5d68849083e1a.html
41
- //
42
- // Note: __webpack_require__.gca() is called in docusaurus.ts for
43
- // prefetching
44
- // Note: we previously used jsonpScriptSrc (Webpack 4)
45
- `__webpack_require__.gca = function(chunkId) { chunkId = ${JSON.stringify(chunkNameToId)}[chunkId]||chunkId; return __webpack_require__.p + __webpack_require__.u(chunkId); };`);
46
- return webpack_1.default.Template.asString(buf);
47
+ compiler.hooks.thisCompilation.tap(PluginName, (compilation) => {
48
+ compilation.hooks.additionalTreeRuntimeRequirements.tap(PluginName, (chunk) => {
49
+ compilation.addRuntimeModule(chunk, new ChunkAssetRuntimeModule());
47
50
  });
48
51
  });
49
52
  }
50
53
  }
51
54
  exports.default = ChunkAssetPlugin;
55
+ // Inspired by https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/CompatRuntimeModule.js
56
+ class ChunkAssetRuntimeModule extends webpack_1.default.RuntimeModule {
57
+ constructor() {
58
+ super('ChunkAssetRuntimeModule', webpack_1.default.RuntimeModule.STAGE_ATTACH);
59
+ this.fullHash = true;
60
+ }
61
+ generate() {
62
+ return generateGetChunkAssetRuntimeCode(this.chunk);
63
+ }
64
+ }
@@ -4,8 +4,9 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import CopyWebpackPlugin from 'copy-webpack-plugin';
8
- import type { Props } from '@docusaurus/types';
9
- export declare function createStaticDirectoriesCopyPlugin({ props, }: {
7
+ import type { CurrentBundler, Props } from '@docusaurus/types';
8
+ import type { WebpackPluginInstance } from 'webpack';
9
+ export declare function createStaticDirectoriesCopyPlugin({ props, currentBundler, }: {
10
10
  props: Props;
11
- }): Promise<CopyWebpackPlugin | undefined>;
11
+ currentBundler: CurrentBundler;
12
+ }): Promise<WebpackPluginInstance | undefined>;
@@ -10,8 +10,11 @@ exports.createStaticDirectoriesCopyPlugin = createStaticDirectoriesCopyPlugin;
10
10
  const tslib_1 = require("tslib");
11
11
  const path_1 = tslib_1.__importDefault(require("path"));
12
12
  const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
- const copy_webpack_plugin_1 = tslib_1.__importDefault(require("copy-webpack-plugin"));
14
- async function createStaticDirectoriesCopyPlugin({ props, }) {
13
+ const currentBundler_1 = require("../currentBundler");
14
+ async function createStaticDirectoriesCopyPlugin({ props, currentBundler, }) {
15
+ const CopyPlugin = await (0, currentBundler_1.getCopyPlugin)({
16
+ currentBundler,
17
+ });
15
18
  const { outDir, siteDir, siteConfig: { staticDirectories: staticDirectoriesOption }, } = props;
16
19
  // The staticDirectories option can contain empty directories, or non-existent
17
20
  // directories (e.g. user deleted `static`). Instead of issuing an error, we
@@ -28,7 +31,7 @@ async function createStaticDirectoriesCopyPlugin({ props, }) {
28
31
  if (staticDirectories.length === 0) {
29
32
  return undefined;
30
33
  }
31
- return new copy_webpack_plugin_1.default({
34
+ return new CopyPlugin({
32
35
  patterns: staticDirectories.map((dir) => ({
33
36
  from: dir,
34
37
  to: outDir,
@@ -4,10 +4,11 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import type { Props } from '@docusaurus/types';
7
+ import type { ConfigureWebpackUtils, Props } from '@docusaurus/types';
8
8
  import type { Configuration } from 'webpack';
9
- export default function createServerConfig(params: {
9
+ export default function createServerConfig({ props, configureWebpackUtils, }: {
10
10
  props: Props;
11
+ configureWebpackUtils: ConfigureWebpackUtils;
11
12
  }): Promise<{
12
13
  config: Configuration;
13
14
  serverBundlePath: string;
@@ -13,13 +13,13 @@ const webpack_merge_1 = tslib_1.__importDefault(require("webpack-merge"));
13
13
  const utils_1 = require("@docusaurus/utils");
14
14
  const webpackbar_1 = tslib_1.__importDefault(require("webpackbar"));
15
15
  const base_1 = require("./base");
16
- async function createServerConfig(params) {
17
- const { props } = params;
16
+ async function createServerConfig({ props, configureWebpackUtils, }) {
18
17
  const baseConfig = await (0, base_1.createBaseConfig)({
19
18
  props,
20
19
  isServer: true,
21
20
  minify: false,
22
21
  faster: props.siteConfig.future.experimental_faster,
22
+ configureWebpackUtils,
23
23
  });
24
24
  const outputFilename = 'server.bundle.js';
25
25
  const outputDir = path_1.default.join(props.outDir, '__server');
@@ -33,8 +33,6 @@ async function createServerConfig(params) {
33
33
  path: outputDir,
34
34
  filename: outputFilename,
35
35
  libraryTarget: 'commonjs2',
36
- // Workaround for Webpack 4 Bug (https://github.com/webpack/webpack/issues/6522)
37
- globalObject: 'this',
38
36
  },
39
37
  plugins: [
40
38
  // Show compilation progress bar.
@@ -4,14 +4,14 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import webpack, { type Configuration, type RuleSetRule } from 'webpack';
8
- import type { ConfigureWebpackUtils, DocusaurusConfig } from '@docusaurus/types';
7
+ import webpack, { type Configuration } from 'webpack';
8
+ import type { ConfigureWebpackUtils, CurrentBundler, DocusaurusConfig } from '@docusaurus/types';
9
9
  import type { TransformOptions } from '@babel/core';
10
10
  export declare function formatStatsErrorMessage(statsJson: ReturnType<webpack.Stats['toJson']> | undefined): string | undefined;
11
11
  export declare function printStatsWarnings(statsJson: ReturnType<webpack.Stats['toJson']> | undefined): void;
12
- export declare function getStyleLoaders(isServer: boolean, cssOptionsArg?: {
13
- [key: string]: unknown;
14
- }): RuleSetRule[];
12
+ export declare function createStyleLoadersFactory({ currentBundler, }: {
13
+ currentBundler: CurrentBundler;
14
+ }): Promise<ConfigureWebpackUtils['getStyleLoaders']>;
15
15
  export declare function getCustomBabelConfigFilePath(siteDir: string): Promise<string | undefined>;
16
16
  export declare function getBabelOptions({ isServer, babelOptions, }?: {
17
17
  isServer?: boolean;
@@ -8,7 +8,7 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.formatStatsErrorMessage = formatStatsErrorMessage;
10
10
  exports.printStatsWarnings = printStatsWarnings;
11
- exports.getStyleLoaders = getStyleLoaders;
11
+ exports.createStyleLoadersFactory = createStyleLoadersFactory;
12
12
  exports.getCustomBabelConfigFilePath = getCustomBabelConfigFilePath;
13
13
  exports.getBabelOptions = getBabelOptions;
14
14
  exports.createJsLoaderFactory = createJsLoaderFactory;
@@ -20,10 +20,10 @@ const path_1 = tslib_1.__importDefault(require("path"));
20
20
  const crypto_1 = tslib_1.__importDefault(require("crypto"));
21
21
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
22
22
  const utils_1 = require("@docusaurus/utils");
23
- const mini_css_extract_plugin_1 = tslib_1.__importDefault(require("mini-css-extract-plugin"));
24
23
  const webpack_1 = tslib_1.__importDefault(require("webpack"));
25
24
  const formatWebpackMessages_1 = tslib_1.__importDefault(require("react-dev-utils/formatWebpackMessages"));
26
25
  const faster_1 = require("../faster");
26
+ const currentBundler_1 = require("./currentBundler");
27
27
  function formatStatsErrorMessage(statsJson) {
28
28
  if (statsJson?.errors?.length) {
29
29
  // TODO formatWebpackMessages does not print stack-traces
@@ -43,60 +43,62 @@ function printStatsWarnings(statsJson) {
43
43
  });
44
44
  }
45
45
  }
46
- // Utility method to get style loaders
47
- function getStyleLoaders(isServer, cssOptionsArg = {}) {
48
- const cssOptions = {
49
- // TODO turn esModule on later, see https://github.com/facebook/docusaurus/pull/6424
50
- esModule: false,
51
- ...cssOptionsArg,
52
- };
53
- // On the server we don't really need to extract/emit CSS
54
- // We only need to transform CSS module imports to a styles object
55
- if (isServer) {
56
- return cssOptions.modules
57
- ? [
58
- {
59
- loader: require.resolve('css-loader'),
60
- options: cssOptions,
46
+ async function createStyleLoadersFactory({ currentBundler, }) {
47
+ const CssExtractPlugin = await (0, currentBundler_1.getCSSExtractPlugin)({ currentBundler });
48
+ return function getStyleLoaders(isServer, cssOptionsArg = {}) {
49
+ const cssOptions = {
50
+ // TODO turn esModule on later, see https://github.com/facebook/docusaurus/pull/6424
51
+ esModule: false,
52
+ ...cssOptionsArg,
53
+ };
54
+ // On the server we don't really need to extract/emit CSS
55
+ // We only need to transform CSS module imports to a styles object
56
+ if (isServer) {
57
+ return cssOptions.modules
58
+ ? [
59
+ {
60
+ loader: require.resolve('css-loader'),
61
+ options: cssOptions,
62
+ },
63
+ ]
64
+ : // Ignore regular CSS files
65
+ [{ loader: require.resolve('null-loader') }];
66
+ }
67
+ return [
68
+ {
69
+ loader: CssExtractPlugin.loader,
70
+ options: {
71
+ esModule: true,
61
72
  },
62
- ]
63
- : // Ignore regular CSS files
64
- [{ loader: require.resolve('null-loader') }];
65
- }
66
- return [
67
- {
68
- loader: mini_css_extract_plugin_1.default.loader,
69
- options: {
70
- esModule: true,
71
73
  },
72
- },
73
- {
74
- loader: require.resolve('css-loader'),
75
- options: cssOptions,
76
- },
77
- // TODO apart for configurePostCss(), do we really need this loader?
78
- // Note: using postcss here looks inefficient/duplicate
79
- // But in practice, it's not a big deal because css-loader also uses postcss
80
- // and is able to reuse the parsed AST from postcss-loader
81
- // See https://github.com/webpack-contrib/css-loader/blob/master/src/index.js#L159
82
- {
83
- // Options for PostCSS as we reference these options twice
84
- // Adds vendor prefixing based on your specified browser support in
85
- // package.json
86
- loader: require.resolve('postcss-loader'),
87
- options: {
88
- postcssOptions: {
89
- // Necessary for external CSS imports to work
90
- // https://github.com/facebook/create-react-app/issues/2677
91
- ident: 'postcss',
92
- plugins: [
93
- // eslint-disable-next-line global-require
94
- require('autoprefixer'),
95
- ],
74
+ {
75
+ loader: require.resolve('css-loader'),
76
+ options: cssOptions,
77
+ },
78
+ // TODO apart for configurePostCss(), do we really need this loader?
79
+ // Note: using postcss here looks inefficient/duplicate
80
+ // But in practice, it's not a big deal because css-loader also uses postcss
81
+ // and is able to reuse the parsed AST from postcss-loader
82
+ // See https://github.com/webpack-contrib/css-loader/blob/master/src/index.js#L159
83
+ {
84
+ // Options for PostCSS as we reference these options twice
85
+ // Adds vendor prefixing based on your specified browser support in
86
+ // package.json
87
+ loader: require.resolve('postcss-loader'),
88
+ options: {
89
+ postcssOptions: {
90
+ // Necessary for external CSS imports to work
91
+ // https://github.com/facebook/create-react-app/issues/2677
92
+ ident: 'postcss',
93
+ plugins: [
94
+ // eslint-disable-next-line global-require
95
+ require('autoprefixer'),
96
+ ],
97
+ },
96
98
  },
97
99
  },
98
- },
99
- ];
100
+ ];
101
+ };
100
102
  }
101
103
  async function getCustomBabelConfigFilePath(siteDir) {
102
104
  const customBabelConfigurationPath = path_1.default.join(siteDir, utils_1.BABEL_CONFIG_FILE_NAME);
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": "0.0.0-6069",
4
+ "version": "0.0.0-6074",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -43,12 +43,12 @@
43
43
  "@babel/runtime": "^7.22.6",
44
44
  "@babel/runtime-corejs3": "^7.22.6",
45
45
  "@babel/traverse": "^7.22.8",
46
- "@docusaurus/cssnano-preset": "0.0.0-6069",
47
- "@docusaurus/logger": "0.0.0-6069",
48
- "@docusaurus/mdx-loader": "0.0.0-6069",
49
- "@docusaurus/utils": "0.0.0-6069",
50
- "@docusaurus/utils-common": "0.0.0-6069",
51
- "@docusaurus/utils-validation": "0.0.0-6069",
46
+ "@docusaurus/cssnano-preset": "0.0.0-6074",
47
+ "@docusaurus/logger": "0.0.0-6074",
48
+ "@docusaurus/mdx-loader": "0.0.0-6074",
49
+ "@docusaurus/utils": "0.0.0-6074",
50
+ "@docusaurus/utils-common": "0.0.0-6074",
51
+ "@docusaurus/utils-validation": "0.0.0-6074",
52
52
  "autoprefixer": "^10.4.14",
53
53
  "babel-loader": "^9.1.3",
54
54
  "babel-plugin-dynamic-import-node": "^2.3.3",
@@ -104,8 +104,8 @@
104
104
  "webpackbar": "^5.0.2"
105
105
  },
106
106
  "devDependencies": {
107
- "@docusaurus/module-type-aliases": "0.0.0-6069",
108
- "@docusaurus/types": "0.0.0-6069",
107
+ "@docusaurus/module-type-aliases": "0.0.0-6074",
108
+ "@docusaurus/types": "0.0.0-6074",
109
109
  "@total-typescript/shoehorn": "^0.1.2",
110
110
  "@types/detect-port": "^1.3.3",
111
111
  "@types/react-dom": "^18.2.7",
@@ -132,5 +132,5 @@
132
132
  "engines": {
133
133
  "node": ">=18.0"
134
134
  },
135
- "gitHead": "7258877748d2a7c1682c5c92dae3a17734a7e59e"
135
+ "gitHead": "cb371a5a3300c9ebb158365a2c82be99f8543129"
136
136
  }