@umijs/bundler-webpack 4.0.0-canary.20220517.1 → 4.0.0-canary.20220525.2

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.
@@ -7,8 +7,6 @@ exports.addCompressPlugin = void 0;
7
7
  // @ts-ignore
8
8
  const css_minimizer_webpack_plugin_1 = __importDefault(require("@umijs/bundler-webpack/compiled/css-minimizer-webpack-plugin"));
9
9
  const terser_webpack_plugin_1 = __importDefault(require("../../compiled/terser-webpack-plugin"));
10
- const ESBuildCSSMinifyPlugin_1 = __importDefault(require("../plugins/ESBuildCSSMinifyPlugin"));
11
- const ParcelCSSMinifyPlugin_1 = require("../plugins/ParcelCSSMinifyPlugin");
12
10
  const types_1 = require("../types");
13
11
  const getEsBuildTarget_1 = require("../utils/getEsBuildTarget");
14
12
  async function addCompressPlugin(opts) {
@@ -22,24 +20,38 @@ async function addCompressPlugin(opts) {
22
20
  return;
23
21
  }
24
22
  config.optimization.minimize(true);
23
+ // esbuild transform only allow `string[]` as target
24
+ const esbuildTarget = (0, getEsBuildTarget_1.getEsBuildTarget)({
25
+ targets: userConfig.targets || {},
26
+ });
25
27
  let minify;
26
28
  let terserOptions;
27
29
  if (jsMinifier === types_1.JSMinifier.esbuild) {
28
30
  minify = terser_webpack_plugin_1.default.esbuildMinify;
29
31
  terserOptions = {
30
- target: (0, getEsBuildTarget_1.getEsBuildTarget)({
31
- targets: userConfig.targets || {},
32
- }),
32
+ target: esbuildTarget,
33
+ // remove all comments
34
+ legalComments: 'none',
33
35
  };
34
36
  }
35
37
  else if (jsMinifier === types_1.JSMinifier.terser) {
36
38
  minify = terser_webpack_plugin_1.default.terserMinify;
39
+ terserOptions = {
40
+ format: {
41
+ comments: false,
42
+ },
43
+ };
37
44
  }
38
45
  else if (jsMinifier === types_1.JSMinifier.swc) {
39
46
  minify = terser_webpack_plugin_1.default.swcMinify;
40
47
  }
41
48
  else if (jsMinifier === types_1.JSMinifier.uglifyJs) {
42
49
  minify = terser_webpack_plugin_1.default.uglifyJsMinify;
50
+ terserOptions = {
51
+ output: {
52
+ comments: false,
53
+ },
54
+ };
43
55
  }
44
56
  else if (jsMinifier !== types_1.JSMinifier.none) {
45
57
  throw new Error(`Unsupported jsMinifier ${userConfig.jsMinifier}.`);
@@ -51,33 +63,40 @@ async function addCompressPlugin(opts) {
51
63
  if (jsMinifier !== types_1.JSMinifier.none) {
52
64
  config.optimization.minimizer(`js-${jsMinifier}`).use(terser_webpack_plugin_1.default, [
53
65
  {
66
+ extractComments: false,
54
67
  minify,
55
68
  terserOptions,
56
69
  },
57
70
  ]);
58
71
  }
72
+ let cssMinify;
73
+ let minimizerOptions;
59
74
  if (cssMinifier === types_1.CSSMinifier.esbuild) {
60
- config.optimization
61
- .minimizer(`css-${cssMinifier}`)
62
- .use(ESBuildCSSMinifyPlugin_1.default, [userConfig.cssMinifierOptions]);
75
+ cssMinify = css_minimizer_webpack_plugin_1.default.esbuildMinify;
76
+ minimizerOptions = {
77
+ target: esbuildTarget,
78
+ };
63
79
  }
64
80
  else if (cssMinifier === types_1.CSSMinifier.cssnano) {
65
- config.optimization
66
- .minimizer(`css-${cssMinifier}`)
67
- .use(css_minimizer_webpack_plugin_1.default, [
68
- {
69
- minimizerOptions: userConfig.cssMinifierOptions,
70
- parallel: true,
71
- },
72
- ]);
81
+ cssMinify = css_minimizer_webpack_plugin_1.default.cssnanoMinify;
73
82
  }
74
83
  else if (cssMinifier === types_1.CSSMinifier.parcelCSS) {
75
- config.optimization
76
- .minimizer(`css-${cssMinifier}`)
77
- .use(ParcelCSSMinifyPlugin_1.ParcelCSSMinifyPlugin);
84
+ cssMinify = css_minimizer_webpack_plugin_1.default.parcelCssMinify;
78
85
  }
79
86
  else if (cssMinifier !== types_1.CSSMinifier.none) {
80
87
  throw new Error(`Unsupported cssMinifier ${userConfig.cssMinifier}.`);
81
88
  }
89
+ minimizerOptions = {
90
+ ...minimizerOptions,
91
+ ...userConfig.cssMinifierOptions,
92
+ };
93
+ config.optimization
94
+ .minimizer(`css-${cssMinifier}`)
95
+ .use(css_minimizer_webpack_plugin_1.default, [
96
+ {
97
+ minify: cssMinify,
98
+ minimizerOptions,
99
+ },
100
+ ]);
82
101
  }
83
102
  exports.addCompressPlugin = addCompressPlugin;
@@ -9,6 +9,7 @@ const express_1 = __importDefault(require("@umijs/bundler-utils/compiled/express
9
9
  const http_proxy_middleware_1 = require("@umijs/bundler-webpack/compiled/http-proxy-middleware");
10
10
  const webpack_1 = __importDefault(require("@umijs/bundler-webpack/compiled/webpack"));
11
11
  const utils_1 = require("@umijs/utils");
12
+ const cors_1 = __importDefault(require("cors"));
12
13
  const fs_1 = require("fs");
13
14
  const http_1 = __importDefault(require("http"));
14
15
  const path_1 = require("path");
@@ -19,12 +20,11 @@ async function createServer(opts) {
19
20
  const { proxy } = userConfig;
20
21
  const app = (0, express_1.default)();
21
22
  // cros
22
- app.use((_req, res, next) => {
23
- res.header('Access-Control-Allow-Origin', '*');
24
- res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With');
25
- res.header('Access-Control-Allow-Methods', 'GET, HEAD, PUT, POST, PATCH, DELETE, OPTIONS');
26
- next();
27
- });
23
+ app.use((0, cors_1.default)({
24
+ origin: true,
25
+ methods: ['GET', 'HEAD', 'PUT', 'POST', 'PATCH', 'DELETE', 'OPTIONS'],
26
+ credentials: true,
27
+ }));
28
28
  // compression
29
29
  app.use(require('@umijs/bundler-webpack/compiled/compression')());
30
30
  // TODO: headers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/bundler-webpack",
3
- "version": "4.0.0-canary.20220517.1",
3
+ "version": "4.0.0-canary.20220525.2",
4
4
  "description": "@umijs/bundler-webpack",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/bundler-webpack#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -35,10 +35,11 @@
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.20220517.1",
39
- "@umijs/bundler-utils": "4.0.0-canary.20220517.1",
40
- "@umijs/mfsu": "4.0.0-canary.20220517.1",
41
- "@umijs/utils": "4.0.0-canary.20220517.1",
38
+ "@umijs/babel-preset-umi": "4.0.0-canary.20220525.2",
39
+ "@umijs/bundler-utils": "4.0.0-canary.20220525.2",
40
+ "@umijs/mfsu": "4.0.0-canary.20220525.2",
41
+ "@umijs/utils": "4.0.0-canary.20220525.2",
42
+ "cors": "^2.8.5",
42
43
  "css-loader": "6.7.1",
43
44
  "es5-imcompatible-versions": "^0.1.73",
44
45
  "jest-worker": "27.5.1",
@@ -49,6 +50,7 @@
49
50
  },
50
51
  "devDependencies": {
51
52
  "@swc/core": "1.2.165",
53
+ "@types/cors": "^2.8.12",
52
54
  "@types/webpack-sources": "3.2.0",
53
55
  "@types/ws": "8.5.3",
54
56
  "autoprefixer": "10.4.4",
@@ -56,7 +58,7 @@
56
58
  "compression": "1.7.4",
57
59
  "connect-history-api-fallback": "1.6.0",
58
60
  "copy-webpack-plugin": "10.2.4",
59
- "css-minimizer-webpack-plugin": "3.4.1",
61
+ "css-minimizer-webpack-plugin": "4.0.0",
60
62
  "cssnano": "5.1.7",
61
63
  "fork-ts-checker-webpack-plugin": "7.2.4",
62
64
  "http-proxy-middleware": "2.0.4",
@@ -1,11 +0,0 @@
1
- import { Compilation, Compiler } from '@umijs/bundler-webpack/compiled/webpack';
2
- interface IOpts {
3
- sourcemap?: any;
4
- }
5
- declare class ESBuildCSSMinifyPlugin {
6
- options: IOpts;
7
- constructor(options?: IOpts);
8
- apply(compiler: Compiler): void;
9
- transformAssets(compilation: Compilation): Promise<void>;
10
- }
11
- export default ESBuildCSSMinifyPlugin;
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const esbuild_1 = require("@umijs/bundler-utils/compiled/esbuild");
4
- const webpack_sources_1 = require("@umijs/bundler-webpack/compiled/webpack-sources");
5
- const version = require('../../package.json');
6
- const PLUGIN_NAME = 'ESBuildCSSMinifyPlugin';
7
- const RE_CSS_FILE = /\.css(\?.*)?$/i;
8
- class ESBuildCSSMinifyPlugin {
9
- constructor(options = {}) {
10
- this.options = options;
11
- }
12
- apply(compiler) {
13
- compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
14
- compilation.hooks.chunkHash.tap(PLUGIN_NAME, (_, hash) => {
15
- hash.update(JSON.stringify({
16
- version,
17
- options: this.options,
18
- }));
19
- });
20
- compilation.hooks.processAssets.tapPromise({
21
- name: PLUGIN_NAME,
22
- stage: 400,
23
- additionalAssets: true,
24
- }, async () => {
25
- await this.transformAssets(compilation);
26
- });
27
- });
28
- }
29
- async transformAssets(compilation) {
30
- const { options: { devtool }, } = compilation.compiler;
31
- const sourcemap = this.options.sourcemap === undefined ? !!devtool : this.options.sourcemap;
32
- const assets = compilation.getAssets().filter((asset) => {
33
- return !asset.info.minimized && RE_CSS_FILE.test(asset.name);
34
- });
35
- await Promise.all(assets.map(async (asset) => {
36
- const { source, map } = asset.source.sourceAndMap();
37
- const sourceAsString = source.toString();
38
- const result = await (0, esbuild_1.transform)(sourceAsString, {
39
- loader: 'css',
40
- sourcemap,
41
- sourcefile: asset.name,
42
- minify: true,
43
- });
44
- compilation.updateAsset(asset.name,
45
- // @ts-ignore
46
- sourcemap
47
- ? new webpack_sources_1.SourceMapSource(result.code, asset.name, result.map, sourceAsString, map, true)
48
- : new webpack_sources_1.RawSource(result.code), { ...asset.info, minimized: true });
49
- }));
50
- }
51
- }
52
- exports.default = ESBuildCSSMinifyPlugin;
@@ -1,10 +0,0 @@
1
- import type { TransformOptions } from '@parcel/css';
2
- import type { Compiler } from '../../compiled/webpack/types';
3
- declare type MinifyPluginOpts = Omit<TransformOptions, 'filename' | 'code' | 'minify'>;
4
- export declare class ParcelCSSMinifyPlugin {
5
- private readonly options;
6
- constructor(opts?: MinifyPluginOpts);
7
- apply(compiler: Compiler): void;
8
- private transformAssets;
9
- }
10
- export {};
@@ -1,73 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ParcelCSSMinifyPlugin = void 0;
4
- const utils_1 = require("@umijs/utils");
5
- const buffer_1 = require("buffer");
6
- const path_1 = require("path");
7
- const webpack_sources_1 = require("../../compiled/webpack-sources");
8
- const pkgPath = (0, path_1.join)(__dirname, '../../package.json');
9
- const pkg = require(pkgPath);
10
- const PLUGIN_NAME = 'parcel-css-minify-plugin';
11
- const CSS_FILE_REG = /\.css(?:\?.*)?$/i;
12
- class ParcelCSSMinifyPlugin {
13
- constructor(opts = {}) {
14
- this.options = opts;
15
- }
16
- apply(compiler) {
17
- const meta = JSON.stringify({
18
- name: pkg.name,
19
- version: pkg.version,
20
- options: this.options,
21
- });
22
- compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
23
- compilation.hooks.chunkHash.tap(PLUGIN_NAME, (_, hash) => hash.update(meta));
24
- compilation.hooks.processAssets.tapPromise({
25
- name: PLUGIN_NAME,
26
- // @ts-ignore
27
- stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
28
- additionalAssets: true,
29
- }, async () => await this.transformAssets(compilation));
30
- compilation.hooks.statsPrinter.tap(PLUGIN_NAME, (statsPrinter) => {
31
- statsPrinter.hooks.print
32
- .for('asset.info.minimized')
33
- // @ts-ignore
34
- .tap(PLUGIN_NAME, (minimized, { green, formatFlag }) => {
35
- // @ts-ignore
36
- return minimized ? green(formatFlag('minimized')) : undefined;
37
- });
38
- });
39
- });
40
- }
41
- async transformAssets(compilation) {
42
- const { options: { devtool }, } = compilation.compiler;
43
- const sourcemap = this.options.sourceMap === undefined
44
- ? (devtool && devtool.includes('source-map'))
45
- : this.options.sourceMap;
46
- const assets = compilation.getAssets().filter((asset) => {
47
- return !asset.info.minimized && CSS_FILE_REG.test(asset.name);
48
- });
49
- await Promise.all(assets.map(async (asset) => {
50
- const { source, map } = asset.source.sourceAndMap();
51
- const sourceAsString = source.toString();
52
- const code = typeof source === 'string' ? buffer_1.Buffer.from(source) : source;
53
- const { transform } = (0, utils_1.importLazy)('@parcel/css');
54
- const result = await transform({
55
- filename: asset.name,
56
- code,
57
- minify: true,
58
- sourceMap: sourcemap,
59
- ...this.options,
60
- });
61
- const codeString = result.code.toString();
62
- compilation.updateAsset(asset.name,
63
- // @ts-ignore
64
- sourcemap
65
- ? new webpack_sources_1.SourceMapSource(codeString, asset.name, JSON.parse(result.map.toString()), sourceAsString, map, true)
66
- : new webpack_sources_1.RawSource(codeString), {
67
- ...asset.info,
68
- minimized: true,
69
- });
70
- }));
71
- }
72
- }
73
- exports.ParcelCSSMinifyPlugin = ParcelCSSMinifyPlugin;