@umijs/bundler-webpack 4.0.0-canary.20220615.2 → 4.0.0-canary.20220628.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.
package/dist/build.js CHANGED
@@ -32,7 +32,9 @@ async function build(opts) {
32
32
  });
33
33
  let isFirstCompile = true;
34
34
  return new Promise((resolve, reject) => {
35
- utils_1.rimraf.sync(webpackConfig.output.path);
35
+ if (opts.clean) {
36
+ utils_1.rimraf.sync(webpackConfig.output.path);
37
+ }
36
38
  const compiler = (0, webpack_1.default)(webpackConfig);
37
39
  compiler.run((err, stats) => {
38
40
  var _a;
@@ -14,9 +14,6 @@ async function addAssetRules(opts) {
14
14
  dataUrlCondition: {
15
15
  maxSize: inlineLimit,
16
16
  },
17
- })
18
- .generator({
19
- filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
20
17
  });
21
18
  rule
22
19
  .oneOf('image')
@@ -26,9 +23,6 @@ async function addAssetRules(opts) {
26
23
  dataUrlCondition: {
27
24
  maxSize: inlineLimit,
28
25
  },
29
- })
30
- .generator({
31
- filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
32
26
  });
33
27
  const fallback = rule
34
28
  .oneOf('fallback')
@@ -40,11 +34,6 @@ async function addAssetRules(opts) {
40
34
  if (userConfig.mdx) {
41
35
  fallback.add(/\.mdx?$/);
42
36
  }
43
- fallback
44
- .end()
45
- .type('asset/resource')
46
- .generator({
47
- filename: `${opts.staticPathPrefix}[name].[hash:8].[ext]`,
48
- });
37
+ fallback.end().type('asset/resource');
49
38
  }
50
39
  exports.addAssetRules = addAssetRules;
@@ -28,6 +28,7 @@ const miniCSSExtractPlugin_1 = require("./miniCSSExtractPlugin");
28
28
  const nodePolyfill_1 = require("./nodePolyfill");
29
29
  const progressPlugin_1 = require("./progressPlugin");
30
30
  const speedMeasureWebpackPlugin_1 = require("./speedMeasureWebpackPlugin");
31
+ const ssrPlugin_1 = __importDefault(require("./ssrPlugin"));
31
32
  const svgRules_1 = require("./svgRules");
32
33
  async function getConfig(opts) {
33
34
  const { userConfig } = opts;
@@ -70,14 +71,15 @@ async function getConfig(opts) {
70
71
  : userConfig.devtool || constants_1.DEFAULT_DEVTOOL
71
72
  : userConfig.devtool);
72
73
  // output
73
- const absOutputPath = (0, path_1.join)(opts.cwd, userConfig.outputPath || constants_1.DEFAULT_OUTPUT_PATH);
74
+ const absOutputPath = (0, path_1.resolve)(opts.cwd, userConfig.outputPath || constants_1.DEFAULT_OUTPUT_PATH);
74
75
  const disableCompress = process.env.COMPRESS === 'none';
75
76
  config.output
76
77
  .path(absOutputPath)
77
78
  .filename(useHash ? `[name].[contenthash:8].js` : `[name].js`)
78
79
  .chunkFilename(useHash ? `[name].[contenthash:8].async.js` : `[name].async.js`)
79
80
  .publicPath(userConfig.publicPath || 'auto')
80
- .pathinfo(isDev || disableCompress);
81
+ .pathinfo(isDev || disableCompress)
82
+ .set('assetModuleFilename', `${applyOpts.staticPathPrefix}[name].[hash:8][ext]`);
81
83
  // resolve
82
84
  // prettier-ignore
83
85
  config.resolve
@@ -138,6 +140,8 @@ async function getConfig(opts) {
138
140
  if (isDev && opts.hmr) {
139
141
  config.plugin('hmr').use(webpack_1.default.HotModuleReplacementPlugin);
140
142
  }
143
+ // ssr
144
+ await (0, ssrPlugin_1.default)(applyOpts);
141
145
  // compress
142
146
  await (0, compressPlugin_1.addCompressPlugin)(applyOpts);
143
147
  // purgecss
@@ -4,23 +4,20 @@ exports.addCopyPlugin = void 0;
4
4
  const fs_1 = require("fs");
5
5
  const path_1 = require("path");
6
6
  async function addCopyPlugin(opts) {
7
+ var _a;
7
8
  const { config, userConfig, cwd } = opts;
8
9
  const copyPatterns = [
9
10
  (0, fs_1.existsSync)((0, path_1.join)(cwd, 'public')) && {
10
11
  from: (0, path_1.join)(cwd, 'public'),
11
12
  },
12
13
  ...(userConfig.copy
13
- ? userConfig.copy.map((item) => {
14
- if (typeof item === 'string') {
15
- return {
16
- from: (0, path_1.join)(cwd, item),
17
- to: item,
18
- };
14
+ ? (_a = userConfig.copy) === null || _a === void 0 ? void 0 : _a.map((pattern) => {
15
+ if (typeof pattern === 'string') {
16
+ return (0, path_1.resolve)(cwd, pattern);
19
17
  }
20
18
  return {
21
- // 相对于 process.cwd,所以这里需要使用绝对路径
22
- from: (0, path_1.join)(cwd, item.from),
23
- to: item.to,
19
+ from: (0, path_1.resolve)(cwd, pattern.from),
20
+ to: (0, path_1.resolve)(cwd, pattern.to),
24
21
  };
25
22
  })
26
23
  : []),
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addCSSRules = void 0;
4
+ const utils_1 = require("@umijs/utils");
4
5
  async function addCSSRules(opts) {
5
6
  const { config, userConfig } = opts;
6
7
  const rulesConfig = [
@@ -71,6 +72,23 @@ async function addCSSRules(opts) {
71
72
  modules: {
72
73
  localIdentName: '[local]___[hash:base64:5]',
73
74
  ...userConfig.cssLoaderModules,
75
+ // If SSR is enabled, we need to handling the css modules name hashing
76
+ // and save the class names mapping into opts.cssModulesMapping
77
+ // so the esbuild can use it to generate the correct name for the server side
78
+ getLocalIdent: userConfig.ssr &&
79
+ ((context, localIdentName, localName, opt) => {
80
+ const classIdent = ((0, utils_1.winPath)(context.resourcePath).replace((0, utils_1.winPath)(ensureLastSlash(opt.context)), '') +
81
+ '@' +
82
+ localName).trim();
83
+ let hash = Buffer.from(classIdent)
84
+ .toString('base64')
85
+ .replace(/=/g, '');
86
+ hash = hash.substring(hash.length - 5);
87
+ const result = localIdentName
88
+ .replace(/\[local]/g, localName)
89
+ .replace(/\[hash[^\[]*?]/g, hash);
90
+ return result;
91
+ }),
74
92
  },
75
93
  }
76
94
  : {}),
@@ -106,3 +124,6 @@ async function addCSSRules(opts) {
106
124
  }
107
125
  }
108
126
  exports.addCSSRules = addCSSRules;
127
+ function ensureLastSlash(path) {
128
+ return path.endsWith('/') ? path : path + '/';
129
+ }
@@ -1,5 +1,5 @@
1
1
  import Config from '@umijs/bundler-webpack/compiled/webpack-5-chain';
2
- import { Env, IConfig } from '../types';
2
+ import type { Env, IConfig } from '../types';
3
3
  interface IOpts {
4
4
  name?: string;
5
5
  config: Config;
@@ -13,7 +13,10 @@ async function addMiniCSSExtractPlugin(opts) {
13
13
  config.plugin('mini-css-extract-plugin').use(mini_css_extract_plugin_1.default, [
14
14
  {
15
15
  filename: `[name]${hash}.css`,
16
- chunkFilename: `[name]${hash}.chunk.css`,
16
+ chunkFilename: opts.userConfig.ssr
17
+ ? // TODO: FIXME
18
+ `umi${hash}.css`
19
+ : `[name]${hash}.chunk.css`,
17
20
  ignoreOrder: true,
18
21
  },
19
22
  ]);
@@ -0,0 +1,11 @@
1
+ import Config from '../../compiled/webpack-5-chain';
2
+ import { Env, IConfig } from '../types';
3
+ interface IOpts {
4
+ name?: string;
5
+ config: Config;
6
+ userConfig: IConfig;
7
+ cwd: string;
8
+ env: Env;
9
+ }
10
+ export default function addSSRPlugin(opts: IOpts): void;
11
+ export {};
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const webpack_1 = require("@umijs/bundler-webpack/compiled/webpack");
4
+ const utils_1 = require("@umijs/utils");
5
+ const fs_1 = require("fs");
6
+ const path_1 = require("path");
7
+ const constants_1 = require("../constants");
8
+ const PLUGIN_NAME = 'SSR_PLUGIN';
9
+ class SSRPlugin {
10
+ constructor(opts) {
11
+ this.opts = opts;
12
+ this.manifest = new Map();
13
+ }
14
+ apply(compiler) {
15
+ // ref: https://github.com/webdeveric/webpack-assets-manifest
16
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
17
+ compilation.hooks.processAssets.tap(PLUGIN_NAME, () => {
18
+ const publicPath = compiler.options.output.publicPath || '/';
19
+ const assets = compilation.getAssets().filter((asset) => {
20
+ if (asset.info.hotModuleReplacement) {
21
+ return false;
22
+ }
23
+ return true;
24
+ });
25
+ assets.forEach((asset) => {
26
+ if (asset.info.sourceFilename) {
27
+ this.manifest.set(asset.info.sourceFilename, publicPath + asset.name);
28
+ }
29
+ });
30
+ const stats = compilation.getStats().toJson({
31
+ all: false,
32
+ assets: true,
33
+ cachedAssets: true,
34
+ cachedModules: true,
35
+ });
36
+ const { assetsByChunkName } = stats;
37
+ Object.keys(assetsByChunkName).forEach((chunkName) => {
38
+ assetsByChunkName[chunkName].forEach((filename) => {
39
+ const ext = (0, path_1.extname)(filename.split(/[?#]/)[0]);
40
+ if (!filename.includes('.hot-update.')) {
41
+ this.manifest.set(chunkName + ext, publicPath + filename);
42
+ }
43
+ });
44
+ });
45
+ const assetsSource = JSON.stringify({
46
+ assets: Object.fromEntries(this.manifest),
47
+ }, null, 2);
48
+ if (process.env.NODE_ENV === 'production' ||
49
+ this.opts.userConfig.writeToDisk) {
50
+ compilation.emitAsset('build-manifest.json', new webpack_1.sources.RawSource(assetsSource, false));
51
+ }
52
+ else {
53
+ const outputPath = compiler.options.output.path;
54
+ utils_1.fsExtra.mkdirpSync(outputPath);
55
+ (0, fs_1.writeFileSync)((0, path_1.join)(outputPath, 'build-manifest.json'), assetsSource, 'utf-8');
56
+ }
57
+ });
58
+ });
59
+ }
60
+ }
61
+ function addSSRPlugin(opts) {
62
+ if (opts.userConfig.ssr && opts.name !== constants_1.MFSU_NAME) {
63
+ opts.config.plugin('ssr-plugin').use(SSRPlugin, [opts]);
64
+ }
65
+ }
66
+ exports.default = addSSRPlugin;
@@ -35,20 +35,14 @@ async function addSVGRules(opts) {
35
35
  .loader(require.resolve('@umijs/bundler-webpack/compiled/url-loader'))
36
36
  .end();
37
37
  }
38
- if (svgo === false) {
38
+ if (svgo !== false) {
39
39
  const svgRule = config.module.rule('svg');
40
40
  svgRule
41
41
  .test(/\.svg$/)
42
- .use('url-loader')
43
- .loader(require.resolve('@umijs/bundler-webpack/compiled/url-loader'));
44
- return;
42
+ .use('svgo-loader')
43
+ .loader(require.resolve('@umijs/bundler-webpack/compiled/svgo-loader'))
44
+ .options({ configFile: false, ...svgo })
45
+ .end();
45
46
  }
46
- const svgRule = config.module.rule('svg');
47
- svgRule
48
- .test(/\.svg$/)
49
- .use('svgo-loader')
50
- .loader(require.resolve('@umijs/bundler-webpack/compiled/svgo-loader'))
51
- .options({ configFile: false, ...svgo })
52
- .end();
53
47
  }
54
48
  exports.addSVGRules = addSVGRules;
package/dist/dev.js CHANGED
@@ -75,7 +75,7 @@ async function dev(opts) {
75
75
  extraEsbuildLoaderHandler: (mfsu === null || mfsu === void 0 ? void 0 : mfsu.getEsbuildLoaderHandler()) || [],
76
76
  chainWebpack: opts.chainWebpack,
77
77
  modifyWebpackConfig: opts.modifyWebpackConfig,
78
- hmr: true,
78
+ hmr: process.env.HMR !== 'none',
79
79
  analyze: process.env.ANALYZE,
80
80
  cache: opts.cache,
81
81
  });
package/dist/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
+ import * as parcelCSS from '@parcel/css';
1
2
  import type webpack from '../compiled/webpack';
3
+ import './requireHook';
2
4
  export type { RequestHandler } from '@umijs/bundler-utils/compiled/express';
3
5
  export type { Compiler, Stats } from '../compiled/webpack';
4
6
  export * from './build';
5
7
  export * from './config/config';
6
8
  export * from './dev';
7
9
  export * from './schema';
10
+ export { parcelCSS };
8
11
  export { webpack };
package/dist/index.js CHANGED
@@ -10,10 +10,26 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
13
25
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
27
  };
16
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.parcelCSS = void 0;
30
+ const parcelCSS = __importStar(require("@parcel/css"));
31
+ exports.parcelCSS = parcelCSS;
32
+ require("./requireHook");
17
33
  __exportStar(require("./build"), exports);
18
34
  __exportStar(require("./config/config"), exports);
19
35
  __exportStar(require("./dev"), exports);
@@ -12,8 +12,9 @@ class UmiProgressPlugin extends webpack_1.ProgressPlugin {
12
12
  };
13
13
  }
14
14
  apply(compiler) {
15
+ const prefix = this.options.name ? `[${this.options.name}]` : '[Webpack]';
15
16
  compiler.hooks.invalid.tap(PLUGIN_NAME, () => {
16
- utils_1.logger.wait('Compiling...');
17
+ utils_1.logger.wait(`${prefix} Compiling...`);
17
18
  });
18
19
  compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {
19
20
  const { errors, warnings } = stats.toJson({
@@ -32,8 +33,7 @@ class UmiProgressPlugin extends webpack_1.ProgressPlugin {
32
33
  });
33
34
  }
34
35
  else {
35
- const prefix = this.options.name ? `${this.options.name} ` : '';
36
- utils_1.logger.event(`${prefix}Compiled successfully in ${stats.endTime - stats.startTime} ms (${stats.compilation.modules.size} modules)`);
36
+ utils_1.logger.event(`${prefix} Compiled in ${stats.endTime - stats.startTime} ms (${stats.compilation.modules.size} modules)`);
37
37
  }
38
38
  });
39
39
  }
package/dist/schema.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- /// <reference types="hapi__joi" />
2
- import type { Root } from '@hapi/joi';
1
+ import type { Root } from '@umijs/utils/compiled/@hapi/joi';
3
2
  export declare function getSchemas(): Record<string, (Joi: Root) => any>;
@@ -138,10 +138,11 @@ async function createServer(opts) {
138
138
  app.use(key, (0, http_proxy_middleware_1.createProxyMiddleware)(key, {
139
139
  ...proxy[key],
140
140
  // Add x-real-url in response header
141
- onProxyRes(proxyRes, req) {
142
- var _a;
141
+ onProxyRes(proxyRes, req, res) {
142
+ var _a, _b;
143
143
  proxyRes.headers['x-real-url'] =
144
144
  ((_a = new URL(req.url || '', target)) === null || _a === void 0 ? void 0 : _a.href) || '';
145
+ (_b = proxyConfig.onProxyRes) === null || _b === void 0 ? void 0 : _b.call(proxyConfig, proxyRes, req, res);
145
146
  },
146
147
  }));
147
148
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@umijs/bundler-webpack",
3
- "version": "4.0.0-canary.20220615.2",
3
+ "version": "4.0.0-canary.20220628.1",
4
4
  "description": "@umijs/bundler-webpack",
5
- "homepage": "https://github.com/umijs/umi-next/tree/master/packages/bundler-webpack#readme",
6
- "bugs": "https://github.com/umijs/umi-next/issues",
5
+ "homepage": "https://github.com/umijs/umi/tree/master/packages/bundler-webpack#readme",
6
+ "bugs": "https://github.com/umijs/umi/issues",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://github.com/umijs/umi-next"
9
+ "url": "https://github.com/umijs/umi"
10
10
  },
11
11
  "license": "MIT",
12
12
  "main": "dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "build": "pnpm tsc",
25
25
  "build:client": "pnpm tsc --project ./tsconfig.client.json",
26
26
  "build:deps": "umi-scripts bundleDeps",
27
- "dev": "pnpm build -- --watch",
27
+ "dev": "pnpm build --watch",
28
28
  "generate:webpackPackages": "zx ./scripts/generateWebpackPackages.mjs",
29
29
  "test": "umi-scripts jest-turbo"
30
30
  },
@@ -35,10 +35,10 @@
35
35
  "@svgr/plugin-jsx": "^6.2.1",
36
36
  "@svgr/plugin-svgo": "^6.2.0",
37
37
  "@types/hapi__joi": "17.1.8",
38
- "@umijs/babel-preset-umi": "4.0.0-canary.20220615.2",
39
- "@umijs/bundler-utils": "4.0.0-canary.20220615.2",
40
- "@umijs/mfsu": "4.0.0-canary.20220615.2",
41
- "@umijs/utils": "4.0.0-canary.20220615.2",
38
+ "@umijs/babel-preset-umi": "4.0.0-canary.20220628.1",
39
+ "@umijs/bundler-utils": "4.0.0-canary.20220628.1",
40
+ "@umijs/mfsu": "4.0.0-canary.20220628.1",
41
+ "@umijs/utils": "4.0.0-canary.20220628.1",
42
42
  "cors": "^2.8.5",
43
43
  "css-loader": "6.7.1",
44
44
  "es5-imcompatible-versions": "^0.1.73",