@nuxt/webpack-builder 3.17.3 → 4.0.0-0

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.
package/builder.mjs CHANGED
@@ -2,3 +2,4 @@ export const builder = 'webpack'
2
2
  export { default as webpack } from 'webpack'
3
3
  export { default as MiniCssExtractPlugin } from 'mini-css-extract-plugin'
4
4
  export { default as WebpackBarPlugin } from 'webpackbar'
5
+ export { default as TsCheckerPlugin } from 'fork-ts-checker-webpack-plugin'
package/dist/index.mjs CHANGED
@@ -1,18 +1,17 @@
1
1
  import pify from 'pify';
2
+ import { join, resolve, 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
- import { webpack, WebpackBarPlugin, builder, MiniCssExtractPlugin } from '#builder';
11
- import { join, resolve, normalize, dirname, isAbsolute } from 'pathe';
11
+ import { webpack, WebpackBarPlugin, builder, MiniCssExtractPlugin, TsCheckerPlugin } from '#builder';
12
12
  import { createFsFromVolume, Volume } from 'memfs';
13
13
  import querystring from 'node:querystring';
14
14
  import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
15
- import ForkTSCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
16
15
  import { defineEnv } from 'unenv';
17
16
  import TimeFixPlugin from 'time-fix-plugin';
18
17
  import FriendlyErrorsWebpackPlugin from '@nuxt/friendly-errors-webpack-plugin';
@@ -26,6 +25,8 @@ import VueLoaderPlugin from 'vue-loader/dist/pluginWebpack5.js';
26
25
  import { mkdir, writeFile } from 'node:fs/promises';
27
26
  import { normalizeWebpackManifest } from 'vue-bundle-renderer';
28
27
  import { hash } from 'ohash';
28
+ import { glob } from 'tinyglobby';
29
+ import { genSafeVariableName } from 'knitwork';
29
30
 
30
31
  const defaults = {
31
32
  globalPublicPath: "__webpack_public_path__",
@@ -274,7 +275,7 @@ function baseAlias(ctx) {
274
275
  ...ctx.alias
275
276
  };
276
277
  if (ctx.isClient) {
277
- ctx.alias["#internal/nitro"] = resolve(ctx.nuxt.options.buildDir, "nitro.client.mjs");
278
+ ctx.alias["nitro/runtime"] = resolve(ctx.nuxt.options.buildDir, "nitro.client.mjs");
278
279
  }
279
280
  }
280
281
  function baseResolve(ctx) {
@@ -889,7 +890,7 @@ function clientPlugins(ctx) {
889
890
  }
890
891
  if (!ctx.nuxt.options.ssr) {
891
892
  if (!ctx.nuxt.options.test && (ctx.nuxt.options.typescript.typeCheck === true || ctx.nuxt.options.typescript.typeCheck === "build" && !ctx.nuxt.options.dev)) {
892
- ctx.config.plugins.push(new ForkTSCheckerWebpackPlugin({
893
+ ctx.config.plugins.push(new TsCheckerPlugin({
893
894
  logger
894
895
  }));
895
896
  }
@@ -967,12 +968,12 @@ function serverStandalone(ctx) {
967
968
  ...ctx.options.build.transpile
968
969
  ];
969
970
  const external = [
970
- "#internal/nitro",
971
+ "nitro/runtime",
971
972
  "#shared",
972
973
  resolve(ctx.nuxt.options.rootDir, ctx.nuxt.options.dir.shared)
973
974
  ];
974
975
  if (!ctx.nuxt.options.dev) {
975
- external.push("#internal/nuxt/paths", "#app-manifest");
976
+ external.push("#internal/nuxt/paths", "#internal/nuxt/app-config", "#app-manifest");
976
977
  }
977
978
  if (!Array.isArray(ctx.config.externals)) {
978
979
  return;
@@ -999,12 +1000,97 @@ function serverPlugins(ctx) {
999
1000
  }));
1000
1001
  }
1001
1002
  if (!ctx.nuxt.options.test && (ctx.nuxt.options.typescript.typeCheck === true || ctx.nuxt.options.typescript.typeCheck === "build" && !ctx.nuxt.options.dev)) {
1002
- ctx.config.plugins.push(new ForkTSCheckerWebpackPlugin({
1003
+ ctx.config.plugins.push(new TsCheckerPlugin({
1003
1004
  logger
1004
1005
  }));
1005
1006
  }
1006
1007
  }
1007
1008
 
1009
+ const PLUGIN_NAME = "dynamic-require";
1010
+ const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.mjs`;
1011
+ const DYNAMIC_REQUIRE_RE = /import\((?:.*\+\s*)?"\.\/" ?\+(.*)\).then/g;
1012
+ const BACKWARD_SLASH_RE = /\\/g;
1013
+ function dynamicRequire({ dir, ignore, inline }) {
1014
+ return {
1015
+ name: PLUGIN_NAME,
1016
+ transform(code, _id) {
1017
+ return {
1018
+ code: code.replace(
1019
+ DYNAMIC_REQUIRE_RE,
1020
+ `import('${HELPER_DYNAMIC}').then(r => r.default || r).then(dynamicRequire => dynamicRequire($1)).then`
1021
+ ),
1022
+ map: null
1023
+ };
1024
+ },
1025
+ resolveId(id) {
1026
+ return id === HELPER_DYNAMIC ? id : null;
1027
+ },
1028
+ // TODO: Async chunk loading over network!
1029
+ // renderDynamicImport () {
1030
+ // return {
1031
+ // left: 'fetch(', right: ')'
1032
+ // }
1033
+ // },
1034
+ async load(_id) {
1035
+ if (_id !== HELPER_DYNAMIC) {
1036
+ return null;
1037
+ }
1038
+ let files = [];
1039
+ try {
1040
+ const wpManifest = resolve(dir, "./server.manifest.json");
1041
+ files = await importModule(wpManifest).then((r) => Object.keys(r.files).filter((file) => !ignore.includes(file)));
1042
+ } catch {
1043
+ files = await glob("**/*.{cjs,mjs,js}", {
1044
+ cwd: dir,
1045
+ absolute: false,
1046
+ ignore
1047
+ });
1048
+ }
1049
+ const chunks = (await Promise.all(
1050
+ files.map(async (id) => ({
1051
+ id,
1052
+ src: resolve(dir, id).replace(BACKWARD_SLASH_RE, "/"),
1053
+ name: genSafeVariableName(id),
1054
+ meta: await getWebpackChunkMeta(resolve(dir, id))
1055
+ }))
1056
+ )).filter((chunk) => chunk.meta);
1057
+ return inline ? TMPL_INLINE({ chunks }) : TMPL_LAZY({ chunks });
1058
+ }
1059
+ };
1060
+ }
1061
+ async function getWebpackChunkMeta(src) {
1062
+ const chunk = await importModule(src) || {};
1063
+ const { __webpack_id__, __webpack_ids__, __webpack_modules__, id = __webpack_id__, ids = __webpack_ids__, modules = __webpack_modules__ } = chunk;
1064
+ if (!id && !ids) {
1065
+ return null;
1066
+ }
1067
+ return {
1068
+ id,
1069
+ ids,
1070
+ moduleIds: Object.keys(modules || {})
1071
+ };
1072
+ }
1073
+ function TMPL_INLINE({ chunks }) {
1074
+ return `${chunks.map((i) => `import * as ${i.name} from '${i.src}'`).join("\n")}
1075
+ const dynamicChunks = {
1076
+ ${chunks.map((i) => ` ['${i.id}']: ${i.name}`).join(",\n")}
1077
+ };
1078
+
1079
+ export default function dynamicRequire(id) {
1080
+ return Promise.resolve(dynamicChunks[id]);
1081
+ };`;
1082
+ }
1083
+ function TMPL_LAZY({ chunks }) {
1084
+ return `
1085
+ const dynamicChunks = {
1086
+ ${chunks.map((i) => ` ['${i.id}']: () => import('${i.src}')`).join(",\n")}
1087
+ };
1088
+
1089
+ export default function dynamicRequire(id) {
1090
+ return dynamicChunks[id]();
1091
+ };`;
1092
+ }
1093
+
1008
1094
  const bundle = async (nuxt) => {
1009
1095
  const webpackConfigs = await Promise.all([client, ...nuxt.options.ssr ? [server] : []].map(async (preset) => {
1010
1096
  const ctx = createWebpackConfigContext(nuxt);
@@ -1012,6 +1098,24 @@ const bundle = async (nuxt) => {
1012
1098
  await applyPresets(ctx, preset);
1013
1099
  return ctx.config;
1014
1100
  }));
1101
+ if (!nuxt.options.dev) {
1102
+ const nitro = useNitro();
1103
+ const dynamicRequirePlugin = dynamicRequire({
1104
+ dir: resolve(nuxt.options.buildDir, "dist/server"),
1105
+ inline: nitro.options.node === false || nitro.options.inlineDynamicImports,
1106
+ ignore: [
1107
+ "client.manifest.mjs",
1108
+ "server.js",
1109
+ "server.cjs",
1110
+ "server.mjs",
1111
+ "server.manifest.mjs"
1112
+ ]
1113
+ });
1114
+ const prerenderRollupPlugins = nitro.options._config.rollupConfig.plugins;
1115
+ const rollupPlugins = nitro.options.rollupConfig.plugins;
1116
+ prerenderRollupPlugins.push(dynamicRequirePlugin);
1117
+ rollupPlugins.push(dynamicRequirePlugin);
1118
+ }
1015
1119
  await nuxt.callHook(`${builder}:config`, webpackConfigs);
1016
1120
  const mfs = nuxt.options.dev ? createMFS() : null;
1017
1121
  for (const config of webpackConfigs) {
@@ -1032,7 +1136,7 @@ const bundle = async (nuxt) => {
1032
1136
  });
1033
1137
  nuxt.hook("close", async () => {
1034
1138
  for (const compiler of compilers) {
1035
- await new Promise((resolve) => compiler.close(resolve));
1139
+ await new Promise((resolve2) => compiler.close(resolve2));
1036
1140
  }
1037
1141
  });
1038
1142
  if (nuxt.options.dev) {
@@ -1086,21 +1190,21 @@ function wdmToH3Handler(devMiddleware, corsOptions) {
1086
1190
  devMiddleware: devMiddleware.context
1087
1191
  };
1088
1192
  const { req, res } = event.node;
1089
- const body = await new Promise((resolve, reject) => {
1193
+ const body = await new Promise((resolve2, reject) => {
1090
1194
  res.stream = (stream) => {
1091
- resolve(stream);
1195
+ resolve2(stream);
1092
1196
  };
1093
1197
  res.send = (data) => {
1094
- resolve(data);
1198
+ resolve2(data);
1095
1199
  };
1096
1200
  res.finish = (data) => {
1097
- resolve(data);
1201
+ resolve2(data);
1098
1202
  };
1099
1203
  devMiddleware(req, res, (err) => {
1100
1204
  if (err) {
1101
1205
  reject(err);
1102
1206
  } else {
1103
- resolve(void 0);
1207
+ resolve2(void 0);
1104
1208
  }
1105
1209
  });
1106
1210
  });
@@ -1119,9 +1223,9 @@ async function compile(compiler) {
1119
1223
  await Promise.all(compilersWatching.map((watching) => pify(watching.close.bind(watching))()));
1120
1224
  });
1121
1225
  if (compiler.options.name === "client") {
1122
- return new Promise((resolve, reject) => {
1226
+ return new Promise((resolve2, reject) => {
1123
1227
  compiler.hooks.done.tap("nuxt-dev", () => {
1124
- resolve(null);
1228
+ resolve2(null);
1125
1229
  });
1126
1230
  compiler.hooks.failed.tap("nuxt-errorlog", (err) => {
1127
1231
  reject(err);
@@ -1133,17 +1237,17 @@ async function compile(compiler) {
1133
1237
  });
1134
1238
  });
1135
1239
  }
1136
- return new Promise((resolve, reject) => {
1240
+ return new Promise((resolve2, reject) => {
1137
1241
  const watching = compiler.watch(nuxt.options.watchers.webpack, (err) => {
1138
1242
  if (err) {
1139
1243
  return reject(err);
1140
1244
  }
1141
- resolve(null);
1245
+ resolve2(null);
1142
1246
  });
1143
1247
  compilersWatching.push(watching);
1144
1248
  });
1145
1249
  }
1146
- const stats = await new Promise((resolve, reject) => compiler.run((err, stats2) => err ? reject(err) : resolve(stats2)));
1250
+ const stats = await new Promise((resolve2, reject) => compiler.run((err, stats2) => err ? reject(err) : resolve2(stats2)));
1147
1251
  if (stats.hasErrors()) {
1148
1252
  const error = new Error("Nuxt build error");
1149
1253
  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.3",
3
+ "version": "4.0.0-0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/nuxt.git",
@@ -38,6 +38,7 @@
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.1",
43
44
  "mini-css-extract-plugin": "^2.9.2",
@@ -52,27 +53,28 @@
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.13",
55
57
  "ufo": "^1.6.1",
56
58
  "unenv": "^2.0.0-rc.15",
57
59
  "unplugin": "^2.3.3",
58
60
  "url-loader": "^4.1.1",
59
61
  "vue-bundle-renderer": "^2.1.1",
60
62
  "vue-loader": "^17.4.2",
61
- "webpack": "^5.99.5",
63
+ "webpack": "^5.99.8",
62
64
  "webpack-bundle-analyzer": "^4.10.2",
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.3"
68
+ "@nuxt/kit": "4.0.0-0"
67
69
  },
68
70
  "devDependencies": {
69
71
  "@rspack/core": "1.3.9",
70
- "@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.40.2",
73
75
  "unbuild": "3.5.0",
74
76
  "vue": "3.5.13",
75
- "@nuxt/schema": "3.17.3"
77
+ "@nuxt/schema": "4.0.0-0"
76
78
  },
77
79
  "peerDependencies": {
78
80
  "vue": "^3.3.4"