@nuxt/webpack-builder 3.0.0-rc.9 → 3.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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.mjs +65 -58
  3. package/package.json +30 -32
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 - Nuxt Project
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
1
  import pify from 'pify';
2
2
  import webpack from 'webpack';
3
+ import { fromNodeMiddleware, defineEventHandler } from 'h3';
3
4
  import webpackDevMiddleware from 'webpack-dev-middleware';
4
5
  import webpackHotMiddleware from 'webpack-hot-middleware';
5
- import { parseURL, joinURL } from 'ufo';
6
+ import { parseURL, parseQuery, joinURL } from 'ufo';
6
7
  import { useNuxt, logger, requireModule } from '@nuxt/kit';
7
8
  import { pathToFileURL } from 'node:url';
8
9
  import { createUnplugin } from 'unplugin';
@@ -37,19 +38,20 @@ const keyedFunctions = [
37
38
  "useLazyFetch"
38
39
  ];
39
40
  const KEYED_FUNCTIONS_RE = new RegExp(`(${keyedFunctions.join("|")})`);
41
+ const stringTypes = ["Literal", "TemplateLiteral"];
40
42
  const composableKeysPlugin = createUnplugin((options) => {
41
43
  return {
42
44
  name: "nuxt:composable-keys",
43
45
  enforce: "post",
46
+ transformInclude(id) {
47
+ const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
48
+ return !pathname.match(/node_modules\/nuxt3?\//) && pathname.match(/\.(m?[jt]sx?|vue)/) && parseQuery(search).type !== "style" && !parseQuery(search).macro;
49
+ },
44
50
  transform(code, id) {
45
- const { pathname } = parseURL(decodeURIComponent(pathToFileURL(id).href));
46
- if (!pathname.match(/\.(m?[jt]sx?|vue)/)) {
47
- return;
48
- }
49
51
  if (!KEYED_FUNCTIONS_RE.test(code)) {
50
52
  return;
51
53
  }
52
- const { 0: script = code, index: codeIndex = 0 } = code.match(/(?<=<script[^>]*>)[\S\s.]*?(?=<\/script>)/) || [];
54
+ const { 0: script = code, index: codeIndex = 0 } = code.match(/(?<=<script[^>]*>)[\S\s.]*?(?=<\/script>)/) || { index: 0, 0: code };
53
55
  const s = new MagicString(code);
54
56
  let count = 0;
55
57
  const relativeID = isAbsolute(id) ? relative(options.rootDir, id) : id;
@@ -62,13 +64,34 @@ const composableKeysPlugin = createUnplugin((options) => {
62
64
  return;
63
65
  }
64
66
  const node = _node;
65
- if (keyedFunctions.includes(node.callee.name) && node.arguments.length < 4) {
66
- const end = node.end;
67
- s.appendLeft(
68
- codeIndex + end - 1,
69
- (node.arguments.length ? ", " : "") + "'$" + hash(`${relativeID}-${++count}`) + "'"
70
- );
67
+ const name = "name" in node.callee && node.callee.name;
68
+ if (!name || !keyedFunctions.includes(name) || node.arguments.length >= 4) {
69
+ return;
71
70
  }
71
+ switch (name) {
72
+ case "useState":
73
+ if (node.arguments.length >= 2 || stringTypes.includes(node.arguments[0]?.type)) {
74
+ return;
75
+ }
76
+ break;
77
+ case "useFetch":
78
+ case "useLazyFetch":
79
+ if (node.arguments.length >= 3 || stringTypes.includes(node.arguments[1]?.type)) {
80
+ return;
81
+ }
82
+ break;
83
+ case "useAsyncData":
84
+ case "useLazyAsyncData":
85
+ if (node.arguments.length >= 3 || stringTypes.includes(node.arguments[0]?.type) || stringTypes.includes(node.arguments[node.arguments.length - 1]?.type)) {
86
+ return;
87
+ }
88
+ break;
89
+ }
90
+ const endsWithComma = code.slice(codeIndex + node.start, codeIndex + node.end - 1).trim().endsWith(",");
91
+ s.appendLeft(
92
+ codeIndex + node.end - 1,
93
+ (node.arguments.length && !endsWithComma ? ", " : "") + "'$" + hash(`${relativeID}-${++count}`) + "'"
94
+ );
72
95
  }
73
96
  });
74
97
  if (s.hasChanged()) {
@@ -95,7 +118,8 @@ const DynamicBasePlugin = createUnplugin((options = {}) => {
95
118
  return;
96
119
  }
97
120
  const s = new MagicString(code);
98
- s.append(`${options.globalPublicPath} = buildAssetsURL();
121
+ s.append(`
122
+ ${options.globalPublicPath} = buildAssetsURL();
99
123
  `);
100
124
  return {
101
125
  code: s.toString(),
@@ -122,7 +146,7 @@ function registerVirtualModules() {
122
146
  virtualModules.writeModule(filePath, nuxt.vfs[filePath]);
123
147
  }
124
148
  };
125
- nuxt.hook("build:compile", ({ compiler }) => {
149
+ nuxt.hook("webpack:compile", ({ compiler }) => {
126
150
  if (compiler.name === "server") {
127
151
  writeFiles();
128
152
  }
@@ -278,7 +302,7 @@ function basePlugins(ctx) {
278
302
  config.plugins.push(...options.webpack.plugins || []);
279
303
  config.plugins.push(new WarningIgnorePlugin(getWarningIgnoreFilter(ctx)));
280
304
  config.plugins.push(new webpack.DefinePlugin(getEnv(ctx)));
281
- if (ctx.isServer || ctx.isDev && !options.build.quiet && options.webpack.friendlyErrors) {
305
+ if (ctx.isServer || ctx.isDev && options.webpack.friendlyErrors) {
282
306
  config.plugins.push(
283
307
  new FriendlyErrorsWebpackPlugin({
284
308
  clearConsole: false,
@@ -301,21 +325,21 @@ function basePlugins(ctx) {
301
325
  reporter: {
302
326
  change: (_, { shortPath }) => {
303
327
  if (!ctx.isServer) {
304
- nuxt.callHook("bundler:change", shortPath);
328
+ nuxt.callHook("webpack:change", shortPath);
305
329
  }
306
330
  },
307
331
  done: ({ state }) => {
308
332
  if (state.hasErrors) {
309
- nuxt.callHook("bundler:error");
333
+ nuxt.callHook("webpack:error");
310
334
  } else {
311
335
  logger.success(`${state.name} ${state.message}`);
312
336
  }
313
337
  },
314
338
  allDone: () => {
315
- nuxt.callHook("bundler:done");
339
+ nuxt.callHook("webpack:done");
316
340
  },
317
341
  progress({ statesArray }) {
318
- nuxt.callHook("bundler:progress", statesArray);
342
+ nuxt.callHook("webpack:progress", statesArray);
319
343
  }
320
344
  }
321
345
  }));
@@ -397,8 +421,6 @@ function getEnv(ctx) {
397
421
  "process.env.NODE_ENV": JSON.stringify(ctx.config.mode),
398
422
  "process.mode": JSON.stringify(ctx.config.mode),
399
423
  "process.dev": options.dev,
400
- "process.static": options.target === "static",
401
- "process.target": JSON.stringify(options.target),
402
424
  "process.env.VUE_ENV": JSON.stringify(ctx.name),
403
425
  "process.browser": ctx.isClient,
404
426
  "process.client": ctx.isClient,
@@ -408,10 +430,6 @@ function getEnv(ctx) {
408
430
  _env["typeof process"] = JSON.stringify(ctx.isServer ? "object" : "undefined");
409
431
  _env["typeof window"] = _env["typeof document"] = JSON.stringify(!ctx.isServer ? "object" : "undefined");
410
432
  }
411
- Object.entries(options.env).forEach(([key, value]) => {
412
- const isNative = ["boolean", "number"].includes(typeof value);
413
- _env["process.env." + key] = isNative ? value : JSON.stringify(value);
414
- });
415
433
  return _env;
416
434
  }
417
435
 
@@ -859,13 +877,15 @@ function client(ctx) {
859
877
  ]);
860
878
  }
861
879
  function clientDevtool(ctx) {
862
- if (!ctx.isDev) {
880
+ if (!ctx.nuxt.options.sourcemap.client) {
863
881
  ctx.config.devtool = false;
864
882
  return;
865
883
  }
866
- const scriptPolicy = getCspScriptPolicy(ctx);
867
- const noUnsafeEval = scriptPolicy && !scriptPolicy.includes("'unsafe-eval'");
868
- ctx.config.devtool = noUnsafeEval ? "cheap-module-source-map" : "eval-cheap-module-source-map";
884
+ if (!ctx.isDev) {
885
+ ctx.config.devtool = "source-map";
886
+ return;
887
+ }
888
+ ctx.config.devtool = "eval-cheap-module-source-map";
869
889
  }
870
890
  function clientPerformance(ctx) {
871
891
  ctx.config.performance = {
@@ -907,20 +927,13 @@ function clientPlugins(ctx) {
907
927
  analyzerMode: "static",
908
928
  defaultSizes: "gzip",
909
929
  generateStatsFile: true,
910
- openAnalyzer: !options.build.quiet,
930
+ openAnalyzer: true,
911
931
  reportFilename: resolve(statsDir, `${ctx.name}.html`),
912
932
  statsFilename: resolve(statsDir, `${ctx.name}.json`),
913
933
  ...options.webpack.analyze === true ? {} : options.webpack.analyze
914
934
  }));
915
935
  }
916
936
  }
917
- function getCspScriptPolicy(ctx) {
918
- const { csp } = ctx.options.render;
919
- if (typeof csp === "object") {
920
- const { policies = {} } = csp;
921
- return policies["script-src"] || policies["default-src"] || [];
922
- }
923
- }
924
937
 
925
938
  function node(ctx) {
926
939
  const { config } = ctx;
@@ -970,7 +983,7 @@ function server(ctx) {
970
983
  function serverPreset(ctx) {
971
984
  const { config } = ctx;
972
985
  config.output.filename = "server.mjs";
973
- config.devtool = "cheap-module-source-map";
986
+ config.devtool = ctx.nuxt.options.sourcemap.server ? ctx.isDev ? "cheap-module-source-map" : "source-map" : false;
974
987
  config.optimization = {
975
988
  splitChunks: false,
976
989
  minimize: false
@@ -1031,10 +1044,10 @@ async function bundle(nuxt) {
1031
1044
  const mfs = nuxt.options.dev ? createMFS() : null;
1032
1045
  const compilers = webpackConfigs.map((config) => {
1033
1046
  config.plugins.push(DynamicBasePlugin.webpack({
1034
- sourcemap: nuxt.options.sourcemap
1047
+ sourcemap: nuxt.options.sourcemap[config.name]
1035
1048
  }));
1036
1049
  config.plugins.push(composableKeysPlugin.webpack({
1037
- sourcemap: nuxt.options.sourcemap,
1050
+ sourcemap: nuxt.options.sourcemap[config.name],
1038
1051
  rootDir: nuxt.options.rootDir
1039
1052
  }));
1040
1053
  const compiler = webpack(config);
@@ -1058,37 +1071,34 @@ async function bundle(nuxt) {
1058
1071
  async function createDevMiddleware(compiler) {
1059
1072
  const nuxt = useNuxt();
1060
1073
  logger.debug("Creating webpack middleware...");
1061
- const devMiddleware = pify(webpackDevMiddleware(compiler, {
1074
+ const devMiddleware = webpackDevMiddleware(compiler, {
1062
1075
  publicPath: joinURL(nuxt.options.app.baseURL, nuxt.options.app.buildAssetsDir),
1063
1076
  outputFileSystem: compiler.outputFileSystem,
1064
1077
  stats: "none",
1065
1078
  ...nuxt.options.webpack.devMiddleware
1066
- }));
1079
+ });
1067
1080
  nuxt.hook("close", () => pify(devMiddleware.close.bind(devMiddleware))());
1068
1081
  const { client: _client, ...hotMiddlewareOptions } = nuxt.options.webpack.hotMiddleware || {};
1069
- const hotMiddleware = pify(webpackHotMiddleware(compiler, {
1082
+ const hotMiddleware = webpackHotMiddleware(compiler, {
1070
1083
  log: false,
1071
1084
  heartbeat: 1e4,
1072
1085
  path: joinURL(nuxt.options.app.baseURL, "__webpack_hmr", compiler.options.name),
1073
1086
  ...hotMiddlewareOptions
1074
- }));
1075
- await nuxt.callHook("webpack:devMiddleware", devMiddleware);
1076
- await nuxt.callHook("webpack:hotMiddleware", hotMiddleware);
1077
- await nuxt.callHook("server:devMiddleware", async (req, res, next) => {
1078
- for (const mw of [devMiddleware, hotMiddleware]) {
1079
- await mw?.(req, res, next);
1080
- }
1081
- next();
1082
1087
  });
1088
+ const devHandler = fromNodeMiddleware(devMiddleware);
1089
+ const hotHandler = fromNodeMiddleware(hotMiddleware);
1090
+ await nuxt.callHook("server:devHandler", defineEventHandler(async (event) => {
1091
+ await devHandler(event);
1092
+ await hotHandler(event);
1093
+ }));
1083
1094
  return devMiddleware;
1084
1095
  }
1085
1096
  async function compile(compiler) {
1086
1097
  const nuxt = useNuxt();
1087
1098
  const { name } = compiler.options;
1088
- await nuxt.callHook("build:compile", { name, compiler });
1099
+ await nuxt.callHook("webpack:compile", { name, compiler });
1089
1100
  compiler.hooks.done.tap("load-resources", async (stats2) => {
1090
- await nuxt.callHook("build:compiled", { name, compiler, stats: stats2 });
1091
- await nuxt.callHook("build:resources", compiler.outputFileSystem);
1101
+ await nuxt.callHook("webpack:compiled", { name, compiler, stats: stats2 });
1092
1102
  });
1093
1103
  if (nuxt.options.dev) {
1094
1104
  const compilersWatching = [];
@@ -1121,12 +1131,9 @@ async function compile(compiler) {
1121
1131
  const stats = await new Promise((resolve, reject) => compiler.run((err, stats2) => err ? reject(err) : resolve(stats2)));
1122
1132
  if (stats.hasErrors()) {
1123
1133
  const error = new Error("Nuxt build error");
1124
- if (nuxt.options.build.quiet === true) {
1125
- error.stack = stats.toString("errors-only");
1126
- }
1134
+ error.stack = stats.toString("errors-only");
1127
1135
  throw error;
1128
1136
  }
1129
- await nuxt.callHook("build:resources");
1130
1137
  }
1131
1138
 
1132
1139
  export { bundle };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/webpack-builder",
3
- "version": "3.0.0-rc.9",
3
+ "version": "3.0.0",
4
4
  "repository": "nuxt/framework",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -12,18 +12,15 @@
12
12
  "files": [
13
13
  "dist"
14
14
  ],
15
- "scripts": {
16
- "prepack": "unbuild"
17
- },
18
15
  "dependencies": {
19
- "@babel/core": "^7.18.13",
16
+ "@babel/core": "^7.20.2",
20
17
  "@nuxt/friendly-errors-webpack-plugin": "^2.5.2",
21
- "@nuxt/kit": "3.0.0-rc.9",
22
- "autoprefixer": "^10.4.8",
23
- "css-loader": "^6.7.1",
24
- "css-minimizer-webpack-plugin": "^4.0.0",
25
- "cssnano": "^5.1.13",
26
- "esbuild-loader": "^2.19.0",
18
+ "@nuxt/kit": "3.0.0",
19
+ "autoprefixer": "^10.4.13",
20
+ "css-loader": "^6.7.2",
21
+ "css-minimizer-webpack-plugin": "^4.2.2",
22
+ "cssnano": "^5.1.14",
23
+ "esbuild-loader": "^2.20.0",
27
24
  "escape-string-regexp": "^5.0.0",
28
25
  "estree-walker": "^3.0.1",
29
26
  "file-loader": "^6.2.0",
@@ -31,46 +28,47 @@
31
28
  "fs-extra": "^10.1.0",
32
29
  "hash-sum": "^2.0.0",
33
30
  "lodash-es": "^4.17.21",
34
- "magic-string": "^0.26.3",
35
- "memfs": "^3.4.7",
31
+ "magic-string": "^0.26.7",
32
+ "memfs": "^3.4.11",
36
33
  "mini-css-extract-plugin": "^2.6.1",
37
- "mlly": "^0.5.14",
38
- "ohash": "^0.1.5",
39
- "pathe": "^0.3.5",
34
+ "mlly": "^1.0.0",
35
+ "ohash": "^1.0.0",
36
+ "pathe": "^1.0.0",
40
37
  "pify": "^6.1.0",
41
- "postcss": "^8.4.16",
38
+ "postcss": "^8.4.19",
42
39
  "postcss-import": "^15.0.0",
43
40
  "postcss-loader": "^7.0.1",
44
41
  "postcss-url": "^10.1.3",
45
42
  "style-resources-loader": "^1.5.0",
46
43
  "time-fix-plugin": "^2.0.7",
47
- "ufo": "^0.8.5",
48
- "unplugin": "^0.9.2",
44
+ "ufo": "^1.0.0",
45
+ "unplugin": "^1.0.0",
49
46
  "url-loader": "^4.1.1",
50
- "vue-bundle-renderer": "^0.4.2",
51
- "vue-loader": "^17.0.0",
52
- "webpack": "^5.74.0",
53
- "webpack-bundle-analyzer": "^4.6.1",
47
+ "vue-bundle-renderer": "^1.0.0",
48
+ "vue-loader": "^17.0.1",
49
+ "webpack": "^5.75.0",
50
+ "webpack-bundle-analyzer": "^4.7.0",
54
51
  "webpack-dev-middleware": "^5.3.3",
55
- "webpack-hot-middleware": "^2.25.2",
56
- "webpack-virtual-modules": "^0.4.4",
52
+ "webpack-hot-middleware": "^2.25.3",
53
+ "webpack-virtual-modules": "^0.4.6",
57
54
  "webpackbar": "^5.0.2"
58
55
  },
59
56
  "devDependencies": {
60
- "@nuxt/schema": "3.0.0-rc.9",
57
+ "@nuxt/schema": "3.0.0",
61
58
  "@types/lodash-es": "^4.17.6",
62
59
  "@types/pify": "^5.0.1",
63
- "@types/webpack-bundle-analyzer": "^4.4.2",
60
+ "@types/webpack-bundle-analyzer": "^4.6.0",
64
61
  "@types/webpack-dev-middleware": "^5.0.2",
65
62
  "@types/webpack-hot-middleware": "^2.25.6",
66
63
  "@types/webpack-virtual-modules": "^0",
67
64
  "unbuild": "latest",
68
- "vue": "3.2.38"
65
+ "vue": "3.2.45"
69
66
  },
70
67
  "peerDependencies": {
71
- "vue": "^3.2.38"
68
+ "vue": "^3.2.45"
72
69
  },
73
70
  "engines": {
74
- "node": "^14.16.0 || ^16.11.0 || ^17.0.0 || ^18.0.0"
75
- }
76
- }
71
+ "node": "^14.16.0 || ^16.10.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
72
+ },
73
+ "scripts": {}
74
+ }