@umijs/bundler-webpack 4.0.0-canary.20230227.1 → 4.0.0-canary.20230309.3

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/dist/build.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import webpack from '../compiled/webpack';
2
- import { IOpts as IConfigOpts } from './config/config';
2
+ import type { IOpts as IConfigOpts } from './config/config';
3
3
  import { IConfig } from './types';
4
4
  declare type IOpts = {
5
5
  cwd: string;
package/dist/build.js CHANGED
@@ -35,14 +35,16 @@ module.exports = __toCommonJS(build_exports);
35
35
  var import_utils = require("@umijs/utils");
36
36
  var import_path = require("path");
37
37
  var import_webpack = __toESM(require("../compiled/webpack"));
38
- var import_config = require("./config/config");
39
38
  var import_types = require("./types");
39
+ var configModule = (0, import_utils.importLazy)(
40
+ require.resolve("./config/config")
41
+ );
40
42
  async function build(opts) {
41
43
  const cacheDirectoryPath = (0, import_path.resolve)(
42
44
  opts.rootDir || opts.cwd,
43
45
  opts.config.cacheDirectoryPath || "node_modules/.cache"
44
46
  );
45
- const webpackConfig = await (0, import_config.getConfig)({
47
+ const webpackConfig = await configModule.getConfig({
46
48
  cwd: opts.cwd,
47
49
  rootDir: opts.rootDir,
48
50
  env: import_types.Env.production,
@@ -74,26 +76,21 @@ async function build(opts) {
74
76
  }
75
77
  const compiler = (0, import_webpack.default)(webpackConfig);
76
78
  let closeWatching;
77
- const handler = (err, stats) => {
79
+ const handler = async (err, stats) => {
78
80
  var _a;
79
- (_a = opts.onBuildComplete) == null ? void 0 : _a.call(opts, {
80
- err,
81
+ const validErr = err || ((stats == null ? void 0 : stats.hasErrors()) ? new Error(stats.toString("errors-only")) : null);
82
+ await ((_a = opts.onBuildComplete) == null ? void 0 : _a.call(opts, {
83
+ err: validErr,
81
84
  stats,
82
85
  isFirstCompile,
83
86
  time: stats ? stats.endTime - stats.startTime : null,
84
87
  // pass close function to close watching
85
88
  ...opts.watch ? { close: closeWatching } : {}
86
- });
89
+ }));
87
90
  isFirstCompile = false;
88
- if (err || (stats == null ? void 0 : stats.hasErrors())) {
89
- if (err) {
90
- reject(err);
91
- }
92
- if (stats) {
93
- const errorMsg = stats.toString("errors-only");
94
- esbuildCompressErrorHelper(errorMsg);
95
- reject(new Error(errorMsg));
96
- }
91
+ if (validErr) {
92
+ (stats == null ? void 0 : stats.hasErrors()) && esbuildCompressErrorHelper(validErr.toString());
93
+ reject(validErr);
97
94
  } else {
98
95
  resolve2(stats);
99
96
  }
@@ -62,14 +62,14 @@ async function addCSSRules(opts) {
62
62
  const nestRulesConfig = [
63
63
  userConfig.autoCSSModules && {
64
64
  rule: rule.test(test).oneOf("css-modules").resourceQuery(/modules/),
65
- isCSSModules: true
65
+ isAutoCSSModuleRule: true
66
66
  },
67
67
  {
68
68
  rule: rule.test(test).oneOf("css").sideEffects(true),
69
- isCSSModules: false
69
+ isAutoCSSModuleRule: false
70
70
  }
71
71
  ].filter(Boolean);
72
- for (const { rule: rule2, isCSSModules } of nestRulesConfig) {
72
+ for (const { rule: rule2, isAutoCSSModuleRule } of nestRulesConfig) {
73
73
  if (userConfig.styleLoader) {
74
74
  rule2.use("style-loader").loader(
75
75
  require.resolve("@umijs/bundler-webpack/compiled/style-loader")
@@ -83,6 +83,23 @@ async function addCSSRules(opts) {
83
83
  esModule: true
84
84
  });
85
85
  }
86
+ const getLocalIdent = userConfig.ssr && getLocalIdentForSSR;
87
+ const localIdentName = "[local]___[hash:base64:5]";
88
+ let cssLoaderModulesConfig;
89
+ if (isAutoCSSModuleRule) {
90
+ cssLoaderModulesConfig = {
91
+ localIdentName,
92
+ ...userConfig.cssLoaderModules,
93
+ getLocalIdent
94
+ };
95
+ } else if (userConfig.normalCSSLoaderModules) {
96
+ cssLoaderModulesConfig = {
97
+ localIdentName,
98
+ auto: true,
99
+ ...userConfig.normalCSSLoaderModules,
100
+ getLocalIdent
101
+ };
102
+ }
86
103
  rule2.use("css-loader").loader(require.resolve("css-loader")).options({
87
104
  importLoaders: 1,
88
105
  esModule: true,
@@ -94,25 +111,7 @@ async function addCSSRules(opts) {
94
111
  }
95
112
  },
96
113
  import: true,
97
- ...isCSSModules ? {
98
- modules: {
99
- localIdentName: "[local]___[hash:base64:5]",
100
- ...userConfig.cssLoaderModules,
101
- // If SSR is enabled, we need to handling the css modules name hashing
102
- // and save the class names mapping into opts.cssModulesMapping
103
- // so the esbuild can use it to generate the correct name for the server side
104
- getLocalIdent: userConfig.ssr && ((context, localIdentName, localName, opt) => {
105
- const classIdent = ((0, import_utils.winPath)(context.resourcePath).replace(
106
- (0, import_utils.winPath)(ensureLastSlash(opt.context)),
107
- ""
108
- ) + "@" + localName).trim();
109
- let hash = Buffer.from(classIdent).toString("base64").replace(/=/g, "");
110
- hash = hash.substring(hash.length - 5);
111
- const result = localIdentName.replace(/\[local]/g, localName).replace(/\[hash[^\[]*?]/g, hash);
112
- return result;
113
- })
114
- }
115
- } : {},
114
+ modules: cssLoaderModulesConfig,
116
115
  ...userConfig.cssLoader
117
116
  });
118
117
  rule2.use("postcss-loader").loader(
@@ -143,6 +142,16 @@ async function addCSSRules(opts) {
143
142
  function ensureLastSlash(path) {
144
143
  return path.endsWith("/") ? path : path + "/";
145
144
  }
145
+ function getLocalIdentForSSR(context, localIdentName, localName, opt) {
146
+ const classIdent = ((0, import_utils.winPath)(context.resourcePath).replace(
147
+ (0, import_utils.winPath)(ensureLastSlash(opt.context)),
148
+ ""
149
+ ) + "@" + localName).trim();
150
+ let hash = Buffer.from(classIdent).toString("base64").replace(/=/g, "");
151
+ hash = hash.substring(hash.length - 5);
152
+ const result = localIdentName.replace(/\[local]/g, localName).replace(/\[hash[^\[]*?]/g, hash);
153
+ return result;
154
+ }
146
155
  // Annotate the CommonJS export names for ESM import in node:
147
156
  0 && (module.exports = {
148
157
  addCSSRules
package/dist/dev.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { MFSU } from '@umijs/mfsu';
3
3
  import type { Worker } from 'worker_threads';
4
4
  import webpack from '../compiled/webpack';
5
- import { IOpts as IConfigOpts } from './config/config';
5
+ import type { IOpts as IConfigOpts } from './config/config';
6
6
  import { IConfig } from './types';
7
7
  declare type IOpts = {
8
8
  afterMiddlewares?: any[];
package/dist/dev.js CHANGED
@@ -39,10 +39,12 @@ var import_utils = require("@umijs/utils");
39
39
  var import_fs = require("fs");
40
40
  var import_path = require("path");
41
41
  var import_webpack = __toESM(require("../compiled/webpack"));
42
- var import_config = require("./config/config");
43
42
  var import_constants = require("./constants");
44
43
  var import_server = require("./server/server");
45
44
  var import_types = require("./types");
45
+ var configModule = (0, import_utils.importLazy)(
46
+ require.resolve("./config/config")
47
+ );
46
48
  function ensureSerializableValue(obj) {
47
49
  return JSON.parse(
48
50
  JSON.stringify(
@@ -118,7 +120,7 @@ async function setup(opts) {
118
120
  cwd: opts.cwd
119
121
  });
120
122
  }
121
- const webpackConfig = await (0, import_config.getConfig)({
123
+ const webpackConfig = await configModule.getConfig({
122
124
  cwd: opts.cwd,
123
125
  rootDir: opts.rootDir,
124
126
  env: import_types.Env.development,
@@ -149,7 +151,7 @@ async function setup(opts) {
149
151
  } : void 0,
150
152
  pkg: opts.pkg
151
153
  });
152
- const depConfig = await (0, import_config.getConfig)({
154
+ const depConfig = await configModule.getConfig({
153
155
  cwd: opts.cwd,
154
156
  rootDir: opts.rootDir,
155
157
  env: import_types.Env.development,
@@ -3,5 +3,6 @@ export declare class EsbuildMinifyFix {
3
3
  private name;
4
4
  constructor();
5
5
  apply(compiler: Compiler): void;
6
+ private isIIFE;
6
7
  minifyFix(compilation: Compilation, assets: Record<string, sources.Source>): Promise<void>;
7
8
  }
@@ -24,6 +24,7 @@ __export(EsbuildMinifyFix_exports, {
24
24
  module.exports = __toCommonJS(EsbuildMinifyFix_exports);
25
25
  var import_utils = require("@umijs/utils");
26
26
  var import_webpack = require("../../compiled/webpack");
27
+ var JS_FILE_REG = /\.(js|mjs|cjs)$/;
27
28
  var EsbuildMinifyFix = class {
28
29
  constructor() {
29
30
  this.name = `EsbuildMinifyFix`;
@@ -40,9 +41,19 @@ var EsbuildMinifyFix = class {
40
41
  );
41
42
  });
42
43
  }
44
+ isIIFE(source) {
45
+ source = source.trim();
46
+ if (source.startsWith('(function(){"use strict";')) {
47
+ return true;
48
+ }
49
+ if (source.startsWith("(function(){") && (source.endsWith("})()") || source.endsWith("})();"))) {
50
+ return true;
51
+ }
52
+ return false;
53
+ }
43
54
  async minifyFix(compilation, assets) {
44
55
  const matchObject = import_webpack.ModuleFilenameHelpers.matchObject.bind(void 0, {
45
- include: [/\.(js|mjs|cjs)$/]
56
+ include: [JS_FILE_REG]
46
57
  });
47
58
  const assetsForMinify = await Promise.all(
48
59
  Object.keys(assets).filter((name) => {
@@ -56,6 +67,9 @@ var EsbuildMinifyFix = class {
56
67
  if (info == null ? void 0 : info.EsbuildMinifyFix) {
57
68
  return false;
58
69
  }
70
+ if (name.endsWith(".worker.js")) {
71
+ return false;
72
+ }
59
73
  return true;
60
74
  }).map(async (name) => {
61
75
  const { info, source } = compilation.getAsset(name);
@@ -69,19 +83,37 @@ var EsbuildMinifyFix = class {
69
83
  for (const asset of assetsForMinify) {
70
84
  const { name, inputSource } = asset;
71
85
  const { source, map } = inputSource.sourceAndMap();
72
- let code = source;
73
- if (Buffer.isBuffer(code)) {
74
- code = code.toString();
75
- }
76
- if (!code.startsWith('"use strict";(self.') && !code.startsWith('(function(){"use strict";') && !code.startsWith("(self.webpack")) {
77
- const bundle = new import_utils.MagicString(code);
78
- bundle.indent().prepend("!(function () {\n").append("}());");
79
- code = bundle.toString();
86
+ const originCode = source.toString();
87
+ let newCode = originCode;
88
+ if (!newCode.startsWith('"use strict";(self.') && !newCode.startsWith("(self.webpack") && !this.isIIFE(newCode)) {
89
+ const bundle = new import_utils.MagicString(newCode);
90
+ bundle.prepend("!(function(){").append("}());");
91
+ newCode = bundle.toString();
80
92
  const output = {};
81
93
  if (map) {
82
- output.source = new SourceMapSource(code, name, map, source, true);
94
+ const bundleMap = bundle.generateMap({
95
+ source: name,
96
+ file: `${name}.map`,
97
+ includeContent: true,
98
+ hires: true
99
+ });
100
+ const originMapAsString = JSON.stringify(map);
101
+ const mergedMap = (0, import_utils.remapping)(JSON.stringify(bundleMap), (file) => {
102
+ if (file === name) {
103
+ return originMapAsString;
104
+ }
105
+ return null;
106
+ });
107
+ output.source = new SourceMapSource(
108
+ newCode,
109
+ name,
110
+ mergedMap,
111
+ originCode,
112
+ map,
113
+ true
114
+ );
83
115
  } else {
84
- output.source = new RawSource(code);
116
+ output.source = new RawSource(newCode);
85
117
  }
86
118
  compilation.updateAsset(name, output.source, {
87
119
  ...asset.info,
package/dist/schema.js CHANGED
@@ -135,6 +135,7 @@ function getSchemas() {
135
135
  }),
136
136
  Joi.boolean()
137
137
  ),
138
+ normalCSSLoaderModules: (Joi) => Joi.object(),
138
139
  outputPath: (Joi) => Joi.string(),
139
140
  postcssLoader: (Joi) => Joi.object(),
140
141
  proxy: (Joi) => Joi.alternatives().try(Joi.object(), Joi.array()),
@@ -8,6 +8,6 @@ import { Server as HttpsServer } from 'https';
8
8
  import WebSocket from '../../compiled/ws';
9
9
  export declare function createWebSocketServer(server: HttpServer | HttpsServer | Http2Server | Server): {
10
10
  send(message: string): void;
11
- wss: WebSocket.Server<WebSocket.WebSocket>;
11
+ wss: WebSocket.Server;
12
12
  close(): void;
13
13
  };
package/dist/types.d.ts CHANGED
@@ -90,6 +90,9 @@ export interface IConfig {
90
90
  lessLoader?: {
91
91
  [key: string]: any;
92
92
  };
93
+ normalCSSLoaderModules?: {
94
+ [key: string]: any;
95
+ };
93
96
  outputPath?: string;
94
97
  postcssLoader?: {
95
98
  [key: string]: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/bundler-webpack",
3
- "version": "4.0.0-canary.20230227.1",
3
+ "version": "4.0.0-canary.20230309.3",
4
4
  "description": "@umijs/bundler-webpack",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/bundler-webpack#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -33,11 +33,11 @@
33
33
  "@svgr/plugin-jsx": "^6.5.1",
34
34
  "@svgr/plugin-svgo": "^6.5.1",
35
35
  "@types/hapi__joi": "17.1.9",
36
- "@umijs/babel-preset-umi": "4.0.0-canary.20230227.1",
37
- "@umijs/bundler-utils": "4.0.0-canary.20230227.1",
36
+ "@umijs/babel-preset-umi": "4.0.0-canary.20230309.3",
37
+ "@umijs/bundler-utils": "4.0.0-canary.20230309.3",
38
38
  "@umijs/case-sensitive-paths-webpack-plugin": "^1.0.1",
39
- "@umijs/mfsu": "4.0.0-canary.20230227.1",
40
- "@umijs/utils": "4.0.0-canary.20230227.1",
39
+ "@umijs/mfsu": "4.0.0-canary.20230309.3",
40
+ "@umijs/utils": "4.0.0-canary.20230309.3",
41
41
  "cors": "^2.8.5",
42
42
  "css-loader": "6.7.1",
43
43
  "es5-imcompatible-versions": "^0.1.78",