@nuxt/webpack-builder 3.17.4 → 3.17.5

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 (2) hide show
  1. package/dist/index.mjs +132 -18
  2. package/package.json +10 -8
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import webpackDevMiddleware from 'webpack-dev-middleware';
4
4
  import webpackHotMiddleware from 'webpack-hot-middleware';
5
5
  import { defu } from 'defu';
6
6
  import { joinURL } from 'ufo';
7
- import { logger, useNuxt } from '@nuxt/kit';
7
+ import { logger, useNitro, useNuxt } from '@nuxt/kit';
8
8
  import { createUnplugin } from 'unplugin';
9
9
  import MagicString from 'magic-string';
10
10
  import { webpack, WebpackBarPlugin, builder, MiniCssExtractPlugin, TsCheckerPlugin } from '#builder';
@@ -17,6 +17,7 @@ import TimeFixPlugin from 'time-fix-plugin';
17
17
  import FriendlyErrorsWebpackPlugin from '@nuxt/friendly-errors-webpack-plugin';
18
18
  import escapeRegExp from 'escape-string-regexp';
19
19
  import { isTest } from 'std-env';
20
+ import { genObjectFromRawEntries, genString } from 'knitwork';
20
21
  import { EsbuildPlugin } from 'esbuild-loader';
21
22
  import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
22
23
  import createResolver from 'postcss-import-resolver';
@@ -177,6 +178,123 @@ class WarningIgnorePlugin {
177
178
  }
178
179
  }
179
180
 
181
+ const validate = (compiler) => {
182
+ if (compiler.options.target !== "node") {
183
+ logger.warn('webpack config `target` should be "node".');
184
+ }
185
+ if (!compiler.options.externals) {
186
+ logger.info(
187
+ "It is recommended to externalize dependencies in the server build for better build performance."
188
+ );
189
+ }
190
+ };
191
+ const isJSRegExp = /\.[cm]?js(\?[^.]+)?$/;
192
+ const isJS = (file) => isJSRegExp.test(file);
193
+ const extractQueryPartJS = (file) => isJSRegExp.exec(file)?.[1];
194
+ const isCSSRegExp = /\.css(?:\?[^.]+)?$/;
195
+ const isCSS = (file) => isCSSRegExp.test(file);
196
+ const isHotUpdate = (file) => file.includes("hot-update");
197
+
198
+ const DYNAMIC_IMPORT_RE = /import\([^)]*\+\s*__webpack_require__[^+]*\)\.then/;
199
+ const DYNAMIC_IMPORT_REPLACE_RE = /import\([^)]*\+\s*(__webpack_require__[^+]*)\)\.then/g;
200
+ const HELPER_FILENAME = "_dynamic-import-helper.mjs";
201
+ const HELPER_IMPORT = `import { _rollupDynamicImport } from "./${HELPER_FILENAME}";
202
+ `;
203
+ class RollupCompatDynamicImportPlugin {
204
+ apply(compiler) {
205
+ compiler.hooks.compilation.tap("RollupCompatDynamicImportPlugin", (compilation) => {
206
+ compilation.hooks.processAssets.tapAsync(
207
+ {
208
+ name: "RollupCompatDynamicImportPlugin",
209
+ stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE
210
+ },
211
+ (assets, callback) => {
212
+ try {
213
+ const targetFiles = /* @__PURE__ */ new Set();
214
+ for (const chunk of compilation.chunks) {
215
+ if (chunk.canBeInitial() || chunk.hasRuntime()) {
216
+ for (const file of chunk.files || []) {
217
+ targetFiles.add(file);
218
+ }
219
+ }
220
+ }
221
+ for (const [filename, asset] of Object.entries(assets)) {
222
+ if (!isJS(filename)) {
223
+ continue;
224
+ }
225
+ if (!targetFiles.has(filename)) {
226
+ continue;
227
+ }
228
+ const source = asset.source();
229
+ const originalCode = typeof source === "string" ? source : source.toString();
230
+ if (!DYNAMIC_IMPORT_RE.test(originalCode)) {
231
+ continue;
232
+ }
233
+ const transformedCode = this.transformDynamicImports(originalCode);
234
+ if (transformedCode !== originalCode) {
235
+ assets[filename] = new compiler.webpack.sources.RawSource(transformedCode);
236
+ }
237
+ }
238
+ this.generateDynamicImportHelper(compilation);
239
+ callback();
240
+ } catch (error) {
241
+ callback(error);
242
+ }
243
+ }
244
+ );
245
+ });
246
+ }
247
+ transformDynamicImports(source) {
248
+ let transformed = source;
249
+ let needsHelperImport = false;
250
+ transformed = transformed.replace(DYNAMIC_IMPORT_REPLACE_RE, (match, filename) => {
251
+ needsHelperImport = true;
252
+ return `_rollupDynamicImport(${filename}).then`;
253
+ });
254
+ if (needsHelperImport && !transformed.includes(HELPER_IMPORT)) {
255
+ transformed = HELPER_IMPORT + transformed;
256
+ }
257
+ return transformed;
258
+ }
259
+ generateDynamicImportHelper(compilation) {
260
+ const chunkFiles = [];
261
+ for (const chunk of compilation.chunks) {
262
+ if (chunk.hasRuntime()) {
263
+ continue;
264
+ }
265
+ for (const filename of chunk.files) {
266
+ if (filename && isJS(filename)) {
267
+ chunkFiles.push(filename);
268
+ }
269
+ }
270
+ }
271
+ if (chunkFiles.length === 0) {
272
+ return;
273
+ }
274
+ const helperContent = this.generateHelperContent(chunkFiles);
275
+ compilation.emitAsset(HELPER_FILENAME, new compilation.compiler.webpack.sources.RawSource(helperContent));
276
+ }
277
+ generateHelperContent(chunkFiles) {
278
+ return `
279
+ // Rollup-compatible dynamic import helper generated by webpack
280
+ // This helper enables rollup to consume webpack chunks directly
281
+
282
+ const chunkMap = ${genObjectFromRawEntries(chunkFiles.map((filename) => [filename, `() => import(${genString("./" + filename)})`]))}
283
+
284
+ // Dynamic import function that rollup can understand
285
+ export function _rollupDynamicImport(chunkId) {
286
+ const chunk = chunkMap[chunkId]
287
+ if (!chunk) {
288
+ return Promise.reject(new Error(\`Chunk \${chunkId} not found in chunkMap. Available chunks: \${Object.keys(chunkMap).join(', ')}\`))
289
+ }
290
+
291
+ // Use actual dynamic import for the chunk
292
+ return chunk()
293
+ }
294
+ `;
295
+ }
296
+ }
297
+
180
298
  async function base(ctx) {
181
299
  await applyPresets(ctx, [
182
300
  baseAlias,
@@ -265,6 +383,9 @@ function basePlugins(ctx) {
265
383
  }
266
384
  }));
267
385
  }
386
+ if (ctx.isServer && !ctx.isDev) {
387
+ ctx.config.plugins.push(new RollupCompatDynamicImportPlugin());
388
+ }
268
389
  }
269
390
  function baseAlias(ctx) {
270
391
  ctx.alias = {
@@ -578,23 +699,6 @@ async function createPostcssLoadersRule(ctx) {
578
699
  };
579
700
  }
580
701
 
581
- const validate = (compiler) => {
582
- if (compiler.options.target !== "node") {
583
- logger.warn('webpack config `target` should be "node".');
584
- }
585
- if (!compiler.options.externals) {
586
- logger.info(
587
- "It is recommended to externalize dependencies in the server build for better build performance."
588
- );
589
- }
590
- };
591
- const isJSRegExp = /\.[cm]?js(\?[^.]+)?$/;
592
- const isJS = (file) => isJSRegExp.test(file);
593
- const extractQueryPartJS = (file) => isJSRegExp.exec(file)?.[1];
594
- const isCSSRegExp = /\.css(?:\?[^.]+)?$/;
595
- const isCSS = (file) => isCSSRegExp.test(file);
596
- const isHotUpdate = (file) => file.includes("hot-update");
597
-
598
702
  class VueSSRClientPlugin {
599
703
  options;
600
704
  constructor(options) {
@@ -1012,6 +1116,16 @@ const bundle = async (nuxt) => {
1012
1116
  await applyPresets(ctx, preset);
1013
1117
  return ctx.config;
1014
1118
  }));
1119
+ if (!nuxt.options.dev) {
1120
+ const nitro = useNitro();
1121
+ nitro.hooks.hook("rollup:before", (_nitro, config) => {
1122
+ const plugins = config.plugins;
1123
+ const existingPlugin = plugins.findIndex((i) => i && "name" in i && i.name === "dynamic-require");
1124
+ if (existingPlugin >= 0) {
1125
+ plugins.splice(existingPlugin, 1);
1126
+ }
1127
+ });
1128
+ }
1015
1129
  await nuxt.callHook(`${builder}:config`, webpackConfigs);
1016
1130
  const mfs = nuxt.options.dev ? createMFS() : null;
1017
1131
  for (const config of webpackConfigs) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/webpack-builder",
3
- "version": "3.17.4",
3
+ "version": "3.17.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/nuxt.git",
@@ -38,13 +38,14 @@
38
38
  "fork-ts-checker-webpack-plugin": "^9.1.0",
39
39
  "h3": "^1.15.3",
40
40
  "jiti": "^2.4.2",
41
+ "knitwork": "^1.2.0",
41
42
  "magic-string": "^0.30.17",
42
43
  "memfs": "^4.17.2",
43
44
  "mini-css-extract-plugin": "^2.9.2",
44
45
  "ohash": "^2.0.11",
45
46
  "pathe": "^2.0.3",
46
47
  "pify": "^6.1.0",
47
- "postcss": "^8.5.3",
48
+ "postcss": "^8.5.4",
48
49
  "postcss-import": "^16.1.0",
49
50
  "postcss-import-resolver": "^2.0.0",
50
51
  "postcss-loader": "^8.1.1",
@@ -54,25 +55,26 @@
54
55
  "time-fix-plugin": "^2.0.7",
55
56
  "ufo": "^1.6.1",
56
57
  "unenv": "^2.0.0-rc.17",
57
- "unplugin": "^2.3.4",
58
+ "unplugin": "^2.3.5",
58
59
  "url-loader": "^4.1.1",
59
60
  "vue-bundle-renderer": "^2.1.1",
60
61
  "vue-loader": "^17.4.2",
61
- "webpack": "^5.99.8",
62
+ "webpack": "^5.99.9",
62
63
  "webpack-bundle-analyzer": "^4.10.2",
63
64
  "webpack-dev-middleware": "^7.4.2",
64
65
  "webpack-hot-middleware": "^2.26.1",
65
66
  "webpackbar": "^7.0.0",
66
- "@nuxt/kit": "3.17.4"
67
+ "@nuxt/kit": "3.17.5"
67
68
  },
68
69
  "devDependencies": {
69
- "@rspack/core": "1.3.11",
70
+ "@rspack/core": "1.3.13",
70
71
  "@types/pify": "6.1.0",
71
72
  "@types/webpack-bundle-analyzer": "4.7.0",
72
73
  "@types/webpack-hot-middleware": "2.25.9",
74
+ "rollup": "4.41.1",
73
75
  "unbuild": "3.5.0",
74
- "vue": "3.5.14",
75
- "@nuxt/schema": "3.17.4"
76
+ "vue": "3.5.16",
77
+ "@nuxt/schema": "3.17.5"
76
78
  },
77
79
  "peerDependencies": {
78
80
  "vue": "^3.3.4"