@nuxt/webpack-builder 3.17.4 → 4.0.0-alpha.1

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 +131 -13
  2. package/package.json +10 -8
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import pify from 'pify';
2
+ import { join, resolve, basename, normalize, dirname, isAbsolute } from 'pathe';
2
3
  import { fromNodeMiddleware, defineEventHandler, handleCors, getRequestHeader, createError, setHeader } from 'h3';
3
4
  import webpackDevMiddleware from 'webpack-dev-middleware';
4
5
  import webpackHotMiddleware from 'webpack-hot-middleware';
5
6
  import { defu } from 'defu';
6
7
  import { joinURL } from 'ufo';
7
- import { logger, useNuxt } from '@nuxt/kit';
8
+ import { logger, importModule, useNitro, useNuxt } from '@nuxt/kit';
8
9
  import { createUnplugin } from 'unplugin';
9
10
  import MagicString from 'magic-string';
10
11
  import { webpack, WebpackBarPlugin, builder, MiniCssExtractPlugin, TsCheckerPlugin } from '#builder';
11
- import { join, resolve, normalize, dirname, isAbsolute } from 'pathe';
12
12
  import { createFsFromVolume, Volume } from 'memfs';
13
13
  import querystring from 'node:querystring';
14
14
  import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
@@ -25,6 +25,8 @@ import VueLoaderPlugin from 'vue-loader/dist/pluginWebpack5.js';
25
25
  import { mkdir, writeFile } from 'node:fs/promises';
26
26
  import { normalizeWebpackManifest } from 'vue-bundle-renderer';
27
27
  import { hash } from 'ohash';
28
+ import { glob } from 'tinyglobby';
29
+ import { genSafeVariableName } from 'knitwork';
28
30
 
29
31
  const defaults = {
30
32
  globalPublicPath: "__webpack_public_path__",
@@ -269,11 +271,14 @@ function basePlugins(ctx) {
269
271
  function baseAlias(ctx) {
270
272
  ctx.alias = {
271
273
  "#app": ctx.options.appDir,
274
+ [basename(ctx.nuxt.options.dir.assets)]: resolve(ctx.nuxt.options.srcDir, ctx.nuxt.options.dir.assets),
272
275
  ...ctx.options.alias,
273
276
  ...ctx.alias
274
277
  };
275
278
  if (ctx.isClient) {
279
+ ctx.alias["nitro/runtime"] = resolve(ctx.nuxt.options.buildDir, "nitro.client.mjs");
276
280
  ctx.alias["#internal/nitro"] = resolve(ctx.nuxt.options.buildDir, "nitro.client.mjs");
281
+ ctx.alias["nitropack/runtime"] = resolve(ctx.nuxt.options.buildDir, "nitro.client.mjs");
277
282
  }
278
283
  }
279
284
  function baseResolve(ctx) {
@@ -966,12 +971,16 @@ function serverStandalone(ctx) {
966
971
  ...ctx.options.build.transpile
967
972
  ];
968
973
  const external = /* @__PURE__ */ new Set([
974
+ "nitro/runtime",
975
+ // TODO: remove in v5
969
976
  "#internal/nitro",
977
+ "nitropack/runtime",
970
978
  "#shared",
971
979
  resolve(ctx.nuxt.options.rootDir, ctx.nuxt.options.dir.shared)
972
980
  ]);
973
981
  if (!ctx.nuxt.options.dev) {
974
982
  external.add("#internal/nuxt/paths");
983
+ external.add("#internal/nuxt/app-config");
975
984
  external.add("#app-manifest");
976
985
  }
977
986
  if (!Array.isArray(ctx.config.externals)) {
@@ -1005,6 +1014,92 @@ function serverPlugins(ctx) {
1005
1014
  }
1006
1015
  }
1007
1016
 
1017
+ const PLUGIN_NAME = "dynamic-require";
1018
+ const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.mjs`;
1019
+ const DYNAMIC_REQUIRE_RE = /import\((?:.*\+\s*)?"\.\/" ?\+(.*)\).then/g;
1020
+ const BACKWARD_SLASH_RE = /\\/g;
1021
+ function dynamicRequire({ dir, ignore, inline }) {
1022
+ const filesToIgnore = new Set(ignore);
1023
+ return {
1024
+ name: PLUGIN_NAME,
1025
+ transform(code, _id) {
1026
+ return {
1027
+ code: code.replace(
1028
+ DYNAMIC_REQUIRE_RE,
1029
+ `import('${HELPER_DYNAMIC}').then(r => r.default || r).then(dynamicRequire => dynamicRequire($1)).then`
1030
+ ),
1031
+ map: null
1032
+ };
1033
+ },
1034
+ resolveId(id) {
1035
+ return id === HELPER_DYNAMIC ? id : null;
1036
+ },
1037
+ // TODO: Async chunk loading over network!
1038
+ // renderDynamicImport () {
1039
+ // return {
1040
+ // left: 'fetch(', right: ')'
1041
+ // }
1042
+ // },
1043
+ async load(_id) {
1044
+ if (_id !== HELPER_DYNAMIC) {
1045
+ return null;
1046
+ }
1047
+ let files = [];
1048
+ try {
1049
+ const wpManifest = resolve(dir, "./server.manifest.json");
1050
+ files = await importModule(wpManifest).then((r) => Object.keys(r.files).filter((file) => !filesToIgnore.has(file)));
1051
+ } catch {
1052
+ files = await glob("**/*.{cjs,mjs,js}", {
1053
+ cwd: dir,
1054
+ absolute: false,
1055
+ ignore
1056
+ });
1057
+ }
1058
+ const chunks = (await Promise.all(
1059
+ files.map(async (id) => ({
1060
+ id,
1061
+ src: resolve(dir, id).replace(BACKWARD_SLASH_RE, "/"),
1062
+ name: genSafeVariableName(id),
1063
+ meta: await getWebpackChunkMeta(resolve(dir, id))
1064
+ }))
1065
+ )).filter((chunk) => chunk.meta);
1066
+ return inline ? TMPL_INLINE({ chunks }) : TMPL_LAZY({ chunks });
1067
+ }
1068
+ };
1069
+ }
1070
+ async function getWebpackChunkMeta(src) {
1071
+ const chunk = await importModule(src) || {};
1072
+ const { __webpack_id__, __webpack_ids__, __webpack_modules__, id = __webpack_id__, ids = __webpack_ids__, modules = __webpack_modules__ } = chunk;
1073
+ if (!id && !ids) {
1074
+ return null;
1075
+ }
1076
+ return {
1077
+ id,
1078
+ ids,
1079
+ moduleIds: Object.keys(modules || {})
1080
+ };
1081
+ }
1082
+ function TMPL_INLINE({ chunks }) {
1083
+ return `${chunks.map((i) => `import * as ${i.name} from '${i.src}'`).join("\n")}
1084
+ const dynamicChunks = {
1085
+ ${chunks.map((i) => ` ['${i.id}']: ${i.name}`).join(",\n")}
1086
+ };
1087
+
1088
+ export default function dynamicRequire(id) {
1089
+ return Promise.resolve(dynamicChunks[id]);
1090
+ };`;
1091
+ }
1092
+ function TMPL_LAZY({ chunks }) {
1093
+ return `
1094
+ const dynamicChunks = {
1095
+ ${chunks.map((i) => ` ['${i.id}']: () => import('${i.src}')`).join(",\n")}
1096
+ };
1097
+
1098
+ export default function dynamicRequire(id) {
1099
+ return dynamicChunks[id]();
1100
+ };`;
1101
+ }
1102
+
1008
1103
  const bundle = async (nuxt) => {
1009
1104
  const webpackConfigs = await Promise.all([client, ...nuxt.options.ssr ? [server] : []].map(async (preset) => {
1010
1105
  const ctx = createWebpackConfigContext(nuxt);
@@ -1012,6 +1107,29 @@ const bundle = async (nuxt) => {
1012
1107
  await applyPresets(ctx, preset);
1013
1108
  return ctx.config;
1014
1109
  }));
1110
+ if (!nuxt.options.dev) {
1111
+ const nitro = useNitro();
1112
+ const dynamicRequirePlugin = dynamicRequire({
1113
+ dir: resolve(nuxt.options.buildDir, "dist/server"),
1114
+ inline: nitro.options.node === false || nitro.options.inlineDynamicImports,
1115
+ ignore: [
1116
+ "client.manifest.mjs",
1117
+ "server.js",
1118
+ "server.cjs",
1119
+ "server.mjs",
1120
+ "server.manifest.mjs"
1121
+ ]
1122
+ });
1123
+ const prerenderRollupPlugins = nitro.options._config.rollupConfig.plugins;
1124
+ const rollupPlugins = nitro.options.rollupConfig.plugins;
1125
+ for (const plugins of [prerenderRollupPlugins, rollupPlugins]) {
1126
+ const existingPlugin = plugins.findIndex((i) => i && "name" in i && i.name === "dynamic-require");
1127
+ if (existingPlugin >= 0) {
1128
+ plugins.splice(existingPlugin, 1);
1129
+ }
1130
+ plugins.push(dynamicRequirePlugin);
1131
+ }
1132
+ }
1015
1133
  await nuxt.callHook(`${builder}:config`, webpackConfigs);
1016
1134
  const mfs = nuxt.options.dev ? createMFS() : null;
1017
1135
  for (const config of webpackConfigs) {
@@ -1032,7 +1150,7 @@ const bundle = async (nuxt) => {
1032
1150
  });
1033
1151
  nuxt.hook("close", async () => {
1034
1152
  for (const compiler of compilers) {
1035
- await new Promise((resolve) => compiler.close(resolve));
1153
+ await new Promise((resolve2) => compiler.close(resolve2));
1036
1154
  }
1037
1155
  });
1038
1156
  if (nuxt.options.dev) {
@@ -1086,21 +1204,21 @@ function wdmToH3Handler(devMiddleware, corsOptions) {
1086
1204
  devMiddleware: devMiddleware.context
1087
1205
  };
1088
1206
  const { req, res } = event.node;
1089
- const body = await new Promise((resolve, reject) => {
1207
+ const body = await new Promise((resolve2, reject) => {
1090
1208
  res.stream = (stream) => {
1091
- resolve(stream);
1209
+ resolve2(stream);
1092
1210
  };
1093
1211
  res.send = (data) => {
1094
- resolve(data);
1212
+ resolve2(data);
1095
1213
  };
1096
1214
  res.finish = (data) => {
1097
- resolve(data);
1215
+ resolve2(data);
1098
1216
  };
1099
1217
  devMiddleware(req, res, (err) => {
1100
1218
  if (err) {
1101
1219
  reject(err);
1102
1220
  } else {
1103
- resolve(void 0);
1221
+ resolve2(void 0);
1104
1222
  }
1105
1223
  });
1106
1224
  });
@@ -1119,9 +1237,9 @@ async function compile(compiler) {
1119
1237
  await Promise.all(compilersWatching.map((watching) => pify(watching.close.bind(watching))()));
1120
1238
  });
1121
1239
  if (compiler.options.name === "client") {
1122
- return new Promise((resolve, reject) => {
1240
+ return new Promise((resolve2, reject) => {
1123
1241
  compiler.hooks.done.tap("nuxt-dev", () => {
1124
- resolve(null);
1242
+ resolve2(null);
1125
1243
  });
1126
1244
  compiler.hooks.failed.tap("nuxt-errorlog", (err) => {
1127
1245
  reject(err);
@@ -1133,17 +1251,17 @@ async function compile(compiler) {
1133
1251
  });
1134
1252
  });
1135
1253
  }
1136
- return new Promise((resolve, reject) => {
1254
+ return new Promise((resolve2, reject) => {
1137
1255
  const watching = compiler.watch(nuxt.options.watchers.webpack, (err) => {
1138
1256
  if (err) {
1139
1257
  return reject(err);
1140
1258
  }
1141
- resolve(null);
1259
+ resolve2(null);
1142
1260
  });
1143
1261
  compilersWatching.push(watching);
1144
1262
  });
1145
1263
  }
1146
- const stats = await new Promise((resolve, reject) => compiler.run((err, stats2) => err ? reject(err) : resolve(stats2)));
1264
+ const stats = await new Promise((resolve2, reject) => compiler.run((err, stats2) => err ? reject(err) : resolve2(stats2)));
1147
1265
  if (stats.hasErrors()) {
1148
1266
  const error = new Error("Nuxt build error");
1149
1267
  error.stack = stats.toString("errors-only");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/webpack-builder",
3
- "version": "3.17.4",
3
+ "version": "4.0.0-alpha.1",
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",
@@ -52,9 +53,10 @@
52
53
  "pug-plain-loader": "^1.1.0",
53
54
  "std-env": "^3.9.0",
54
55
  "time-fix-plugin": "^2.0.7",
56
+ "tinyglobby": "^0.2.14",
55
57
  "ufo": "^1.6.1",
56
58
  "unenv": "^2.0.0-rc.17",
57
- "unplugin": "^2.3.4",
59
+ "unplugin": "^2.3.5",
58
60
  "url-loader": "^4.1.1",
59
61
  "vue-bundle-renderer": "^2.1.1",
60
62
  "vue-loader": "^17.4.2",
@@ -63,16 +65,16 @@
63
65
  "webpack-dev-middleware": "^7.4.2",
64
66
  "webpack-hot-middleware": "^2.26.1",
65
67
  "webpackbar": "^7.0.0",
66
- "@nuxt/kit": "3.17.4"
68
+ "@nuxt/kit": "4.0.0-alpha.1"
67
69
  },
68
70
  "devDependencies": {
69
- "@rspack/core": "1.3.11",
70
- "@types/pify": "6.1.0",
71
+ "@rspack/core": "1.3.13",
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": "4.0.0-alpha.1"
76
78
  },
77
79
  "peerDependencies": {
78
80
  "vue": "^3.3.4"