@shuvi/toolpack 0.0.1-rc.9 → 1.0.0-rc.10

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 (103) hide show
  1. package/lib/babel/plugins/auto-css-modules.js +1 -1
  2. package/lib/babel/plugins/{loadable-plugin.d.ts → dynamic-plugin.d.ts} +1 -1
  3. package/lib/babel/plugins/{loadable-plugin.js → dynamic-plugin.js} +9 -9
  4. package/lib/babel/plugins/optimize-hook-destructuring.d.ts +1 -1
  5. package/lib/babel/plugins/optimize-hook-destructuring.js +4 -4
  6. package/lib/babel/preset.js +25 -5
  7. package/lib/constants.d.ts +1 -0
  8. package/lib/constants.js +5 -1
  9. package/lib/utils/bundle-require.d.ts +33 -0
  10. package/lib/utils/bundle-require.js +143 -0
  11. package/lib/utils/forkTsCheckerWebpackPlugin.js +2 -1
  12. package/lib/utils/formatWebpackMessages.d.ts +18 -4
  13. package/lib/utils/formatWebpackMessages.js +17 -14
  14. package/lib/webpack/config/base.d.ts +17 -4
  15. package/lib/webpack/config/base.js +144 -99
  16. package/lib/webpack/config/browser.d.ts +3 -4
  17. package/lib/webpack/config/browser.js +132 -56
  18. package/lib/webpack/config/index.js +18 -7
  19. package/lib/webpack/config/node.d.ts +3 -4
  20. package/lib/webpack/config/node.js +21 -28
  21. package/lib/webpack/config/parts/external.d.ts +3 -2
  22. package/lib/webpack/config/parts/external.js +16 -10
  23. package/lib/webpack/config/parts/helpers.d.ts +3 -1
  24. package/lib/webpack/config/parts/helpers.js +14 -1
  25. package/lib/webpack/config/parts/resolve.js +1 -0
  26. package/lib/webpack/config/parts/style.d.ts +5 -2
  27. package/lib/webpack/config/parts/style.js +125 -60
  28. package/lib/webpack/dynamic-dll/bundler/index.d.ts +22 -0
  29. package/lib/webpack/dynamic-dll/bundler/index.js +211 -0
  30. package/lib/webpack/dynamic-dll/bundler/webpack-config.d.ts +16 -0
  31. package/lib/webpack/dynamic-dll/bundler/webpack-config.js +116 -0
  32. package/lib/webpack/dynamic-dll/constants.d.ts +6 -0
  33. package/lib/webpack/dynamic-dll/constants.js +9 -0
  34. package/lib/webpack/dynamic-dll/dep/getCJSExports.d.ts +3 -0
  35. package/lib/webpack/dynamic-dll/dep/getCJSExports.js +58 -0
  36. package/lib/webpack/dynamic-dll/dep/getExposeFromContent.d.ts +1 -0
  37. package/lib/webpack/dynamic-dll/dep/getExposeFromContent.js +80 -0
  38. package/lib/webpack/dynamic-dll/dep/getModuleExports.d.ts +7 -0
  39. package/lib/webpack/dynamic-dll/dep/getModuleExports.js +46 -0
  40. package/lib/webpack/dynamic-dll/dep/index.d.ts +13 -0
  41. package/lib/webpack/dynamic-dll/dep/index.js +43 -0
  42. package/lib/webpack/dynamic-dll/helper/check-not-in-node-modules.d.ts +1 -0
  43. package/lib/webpack/dynamic-dll/helper/check-not-in-node-modules.js +15 -0
  44. package/lib/webpack/dynamic-dll/index.d.ts +35 -0
  45. package/lib/webpack/dynamic-dll/index.js +198 -0
  46. package/lib/webpack/dynamic-dll/metadata.d.ts +13 -0
  47. package/lib/webpack/dynamic-dll/metadata.js +62 -0
  48. package/lib/webpack/dynamic-dll/moduleCollector.d.ts +28 -0
  49. package/lib/webpack/dynamic-dll/moduleCollector.js +64 -0
  50. package/lib/webpack/dynamic-dll/plugin/dynamic-dll-plugin.d.ts +24 -0
  51. package/lib/webpack/dynamic-dll/plugin/dynamic-dll-plugin.js +70 -0
  52. package/lib/webpack/dynamic-dll/utils.d.ts +6 -0
  53. package/lib/webpack/dynamic-dll/utils.js +29 -0
  54. package/lib/webpack/index.d.ts +15 -0
  55. package/lib/webpack/index.js +110 -0
  56. package/lib/webpack/loaders/parcel-css-loader/CssSyntaxError.d.ts +11 -0
  57. package/lib/webpack/loaders/parcel-css-loader/CssSyntaxError.js +40 -0
  58. package/lib/webpack/loaders/parcel-css-loader/index.d.ts +4 -0
  59. package/lib/webpack/loaders/parcel-css-loader/index.js +369 -0
  60. package/lib/webpack/loaders/parcel-css-loader/runtime/api.d.ts +2 -0
  61. package/lib/webpack/loaders/parcel-css-loader/runtime/api.js +85 -0
  62. package/lib/webpack/loaders/parcel-css-loader/runtime/getUrl.d.ts +2 -0
  63. package/lib/webpack/loaders/parcel-css-loader/runtime/getUrl.js +23 -0
  64. package/lib/webpack/loaders/parcel-css-loader/runtime/noSourceMaps.d.ts +2 -0
  65. package/lib/webpack/loaders/parcel-css-loader/runtime/noSourceMaps.js +2 -0
  66. package/lib/webpack/loaders/parcel-css-loader/runtime/sourceMaps.d.ts +2 -0
  67. package/lib/webpack/loaders/parcel-css-loader/runtime/sourceMaps.js +16 -0
  68. package/lib/webpack/loaders/parcel-css-loader/utils.d.ts +28 -0
  69. package/lib/webpack/loaders/parcel-css-loader/utils.js +869 -0
  70. package/lib/webpack/loaders/route-component-loader.js +25 -5
  71. package/lib/webpack/loaders/shuvi-swc-loader/getLoaderSWCOptions.d.ts +94 -0
  72. package/lib/webpack/loaders/shuvi-swc-loader/getLoaderSWCOptions.js +162 -0
  73. package/lib/webpack/loaders/shuvi-swc-loader/index.d.ts +2 -0
  74. package/lib/webpack/loaders/shuvi-swc-loader/index.js +119 -0
  75. package/lib/webpack/plugins/build-manifest-plugin.js +44 -23
  76. package/lib/webpack/plugins/dynamic-public-path-plugin.d.ts +9 -0
  77. package/lib/webpack/plugins/dynamic-public-path-plugin.js +50 -0
  78. package/lib/webpack/plugins/fix-watching-plugin.d.ts +4 -0
  79. package/lib/webpack/plugins/fix-watching-plugin.js +23 -0
  80. package/lib/webpack/plugins/jsconfig-paths-plugin.d.ts +44 -0
  81. package/lib/webpack/plugins/jsconfig-paths-plugin.js +228 -0
  82. package/lib/webpack/plugins/module-replace-plugin/index.js +5 -1
  83. package/lib/webpack/plugins/module-replace-plugin/plugin.d.ts +7 -6
  84. package/lib/webpack/plugins/module-replace-plugin/plugin.js +56 -33
  85. package/lib/webpack/plugins/module-replace-plugin/stub-loader.js +47 -7
  86. package/lib/webpack/plugins/require-cache-hot-reloader-plugin.js +9 -8
  87. package/lib/webpack/plugins/support-ts-extension-resolver-plugin.d.ts +4 -0
  88. package/lib/webpack/plugins/support-ts-extension-resolver-plugin.js +23 -0
  89. package/lib/webpack/types.d.ts +40 -0
  90. package/lib/webpack/types.js +2 -0
  91. package/package.json +46 -40
  92. package/lib/utils/emptyComponent.d.ts +0 -1
  93. package/lib/utils/emptyComponent.js +0 -7
  94. package/lib/utils/errorOverlayMiddleware.d.ts +0 -1
  95. package/lib/utils/errorOverlayMiddleware.js +0 -21
  96. package/lib/utils/hotDevClient/eventsource.d.ts +0 -1
  97. package/lib/utils/hotDevClient/eventsource.js +0 -63
  98. package/lib/utils/hotDevClient/index.d.ts +0 -4
  99. package/lib/utils/hotDevClient/index.js +0 -289
  100. package/lib/utils/verifyTypeScriptSetup.d.ts +0 -5
  101. package/lib/utils/verifyTypeScriptSetup.js +0 -229
  102. package/lib/webpack/loaders/export-global-loader.d.ts +0 -7
  103. package/lib/webpack/loaders/export-global-loader.js +0 -26
@@ -1,30 +1,53 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
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
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.baseWebpackChain = exports.WebpackChain = void 0;
6
30
  const webpack_chain_1 = __importDefault(require("webpack-chain"));
7
31
  exports.WebpackChain = webpack_chain_1.default;
8
32
  const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
33
+ const css_minimizer_webpack_plugin_1 = __importDefault(require("css-minimizer-webpack-plugin"));
9
34
  const webpack_1 = __importDefault(require("webpack"));
10
- const path_1 = __importDefault(require("path"));
35
+ const path = __importStar(require("path"));
11
36
  const constants_1 = require("@shuvi/shared/lib/constants");
12
- const detectTypescript_1 = require("@shuvi/utils/lib/detectTypescript");
13
- const escapeRegExp_1 = require("@shuvi/utils/lib/escapeRegExp");
14
37
  const build_manifest_plugin_1 = __importDefault(require("../plugins/build-manifest-plugin"));
15
- const module_replace_plugin_1 = __importDefault(require("../plugins/module-replace-plugin"));
16
- const chunk_names_plugin_1 = __importDefault(require("../plugins/chunk-names-plugin"));
17
- const require_cache_hot_reloader_plugin_1 = __importDefault(require("../plugins/require-cache-hot-reloader-plugin"));
38
+ const fix_watching_plugin_1 = __importDefault(require("../plugins/fix-watching-plugin"));
18
39
  const constants_2 = require("../../constants");
19
- const dumbRouteComponent = require.resolve('../../utils/emptyComponent');
20
- const resolveLocalLoader = (name) => path_1.default.join(__dirname, `../loaders/${name}`);
40
+ const crypto = __importStar(require("crypto"));
41
+ const jsconfig_paths_plugin_1 = __importDefault(require("../plugins/jsconfig-paths-plugin"));
42
+ const support_ts_extension_resolver_plugin_1 = __importDefault(require("../plugins/support-ts-extension-resolver-plugin"));
43
+ const helpers_1 = require("./parts/helpers");
44
+ const resolveLocalLoader = (name) => path.join(__dirname, `../loaders/${name}`);
21
45
  const terserOptions = {
22
46
  parse: {
23
- ecma: 8
47
+ ecma: 2017 // es8 === 2017
24
48
  },
25
49
  compress: {
26
50
  ecma: 5,
27
- warnings: false,
28
51
  // The following two options are known to break valid JavaScript code
29
52
  comparisons: false,
30
53
  inline: 2 // https://github.com/zeit/next.js/issues/7178#issuecomment-493048965
@@ -38,42 +61,71 @@ const terserOptions = {
38
61
  ascii_only: true
39
62
  }
40
63
  };
41
- function baseWebpackChain({ dev, projectRoot, srcDirs, mediaFilename, buildManifestFilename, publicPath = '/', env = {} }) {
42
- const { typeScriptPath, tsConfigPath, useTypeScript } = detectTypescript_1.getTypeScriptInfo(projectRoot);
64
+ function baseWebpackChain({ dev, outputDir, parcelCss, experimental, compiler, projectRoot, include, typescript, name, buildManifestFilename, publicPath = '/', env = {}, cacheDir }) {
65
+ var _a;
43
66
  const config = new webpack_chain_1.default();
44
67
  config.mode(dev ? 'development' : 'production');
45
68
  config.bail(!dev);
46
69
  config.performance.hints(false);
47
70
  config.context(projectRoot);
71
+ config.output.path(outputDir);
72
+ config.output.merge({
73
+ publicPath,
74
+ filename: `${dev ? '[name].js' : '[name].[contenthash:8]'}.js`,
75
+ // This saves chunks with the name given via `import()`
76
+ chunkFilename: `static/chunks/${dev ? '[name]' : '[name].[contenthash:8]'}.js`,
77
+ hotUpdateChunkFilename: 'static/webpack/[id].[fullhash].hot-update.js',
78
+ hotUpdateMainFilename: 'static/webpack/[fullhash].hot-update.json',
79
+ strictModuleExceptionHandling: true,
80
+ // crossOriginLoading: crossOrigin,
81
+ webassemblyModuleFilename: 'static/wasm/[modulehash:8].wasm',
82
+ hashFunction: 'xxhash64',
83
+ hashDigestLength: 16
84
+ });
48
85
  config.optimization.merge({
49
86
  emitOnErrors: !dev,
50
87
  checkWasmTypes: false,
51
88
  nodeEnv: false,
52
- splitChunks: false,
53
89
  runtimeChunk: undefined,
54
- minimize: !dev
90
+ minimize: !dev,
91
+ realContentHash: false
55
92
  });
56
- if (!dev) {
93
+ if (dev) {
94
+ config.optimization.usedExports(false);
95
+ config.optimization.splitChunks({
96
+ chunks: helpers_1.splitChunksFilter,
97
+ cacheGroups: {
98
+ defaultVendors: false,
99
+ default: false,
100
+ vendors: {
101
+ name: 'vendors',
102
+ filename: constants_2.CommonChunkFilename,
103
+ test: /[\\/]node_modules[\\/]/,
104
+ // Don't let webpack eliminate this chunk (prevents this chunk from
105
+ // becoming a part of the commons chunk)
106
+ enforce: true
107
+ }
108
+ }
109
+ });
110
+ }
111
+ else {
112
+ // @ts-ignore
57
113
  config.optimization.minimizer('terser').use(terser_webpack_plugin_1.default, [
58
114
  {
59
115
  extractComments: false,
60
116
  parallel: true,
61
- cache: true,
62
- sourceMap: false,
63
117
  terserOptions
64
118
  }
65
119
  ]);
120
+ config.optimization.minimizer('cssMinimizer').use(css_minimizer_webpack_plugin_1.default, [
121
+ {
122
+ // @ts-ignore
123
+ minify: parcelCss
124
+ ? css_minimizer_webpack_plugin_1.default.parcelCssMinify
125
+ : css_minimizer_webpack_plugin_1.default.cssnanoMinify
126
+ }
127
+ ]);
66
128
  }
67
- config.output.merge({
68
- publicPath,
69
- hotUpdateChunkFilename: 'static/webpack/[id].[fullhash].hot-update.js',
70
- hotUpdateMainFilename: 'static/webpack/[fullhash].hot-update.json',
71
- // This saves chunks with the name given via `import()`
72
- chunkFilename: `static/chunks/${dev ? '[name]' : '[name].[contenthash:8]'}.js`,
73
- strictModuleExceptionHandling: true,
74
- // crossOriginLoading: crossOrigin,
75
- webassemblyModuleFilename: 'static/wasm/[modulehash:8].wasm'
76
- });
77
129
  // Support for NODE_PATH
78
130
  const nodePathList = (process.env.NODE_PATH || '')
79
131
  .split(process.platform === 'win32' ? ';' : ':')
@@ -86,7 +138,11 @@ function baseWebpackChain({ dev, projectRoot, srcDirs, mediaFilename, buildManif
86
138
  alias: {}
87
139
  });
88
140
  config.resolveLoader.merge({
89
- alias: ['shuvi-babel-loader', 'route-component-loader'].reduce((alias, loader) => {
141
+ alias: [
142
+ 'shuvi-swc-loader',
143
+ 'route-component-loader',
144
+ 'parcel-css-loader'
145
+ ].reduce((alias, loader) => {
90
146
  alias[`@shuvi/${loader}`] = resolveLocalLoader(loader);
91
147
  return alias;
92
148
  }, {})
@@ -101,108 +157,97 @@ function baseWebpackChain({ dev, projectRoot, srcDirs, mediaFilename, buildManif
101
157
  .resolve.set('fullySpecified', false);
102
158
  mainRule
103
159
  .oneOf('js')
104
- .test(/\.(tsx|ts|js|mjs|jsx)$/)
105
- .include.merge([...srcDirs, ...constants_2.AppSourceRegexs])
160
+ .test(/\.(tsx|ts|js|cjs|mjs|jsx)$/)
161
+ .include.merge([...include, ...constants_2.AppSourceRegexs])
106
162
  .end()
107
- .exclude.add((path) => {
108
- if (constants_2.AppSourceRegexs.some(r => r.test(path))) {
109
- return false;
110
- }
111
- if (srcDirs.some(src => path.includes(src))) {
112
- return false;
113
- }
114
- return /node_modules/.test(path);
115
- })
116
- .end()
117
- .use('shuvi-babel-loader')
118
- .loader('@shuvi/shuvi-babel-loader')
163
+ .use('shuvi-swc-loader')
164
+ .loader('@shuvi/shuvi-swc-loader')
119
165
  .options({
120
- isNode: false,
121
- cacheDirectory: true
166
+ isServer: false,
167
+ experimental,
168
+ compiler,
169
+ supportedBrowsers: false,
170
+ swcCacheDir: path.join(cacheDir, 'swc')
122
171
  });
172
+ config.resolve.alias.set('@swc/helpers', path.dirname(require.resolve(`@swc/helpers/package.json`)));
123
173
  mainRule
124
174
  .oneOf('media')
125
- .exclude.merge([/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/])
175
+ .exclude.merge([/\.(tsx|ts|js|cjs|mjs|jsx)$/, /\.html$/, /\.json$/])
126
176
  .end()
127
177
  .use('file-loader')
128
178
  .loader(require.resolve('file-loader'))
129
179
  .options({
130
- name: mediaFilename
180
+ name: 'static/media/[name].[hash:8].[ext]'
131
181
  });
132
- config.plugin('chunk-names').use(chunk_names_plugin_1.default);
133
182
  config.plugin('private/ignore-plugin').use(webpack_1.default.IgnorePlugin, [
134
183
  {
135
184
  resourceRegExp: /^\.\/locale$/,
136
185
  contextRegExp: /moment$/
137
186
  }
138
187
  ]);
188
+ const shuviPublicEnv = Object.keys(process.env).reduce((prev, key) => {
189
+ if (key.startsWith(constants_1.PUBLIC_ENV_PREFIX)) {
190
+ prev[`process.env.${key}`] = JSON.stringify(process.env[key]);
191
+ }
192
+ return prev;
193
+ }, {});
194
+ const shuviConfigEnv = Object.keys(env).reduce((acc, key) => {
195
+ if (/^(?:NODE_.+)|^(?:__.+)$/i.test(key)) {
196
+ throw new Error(`The key "${key}" under "env" is not allowed.`);
197
+ }
198
+ return Object.assign(Object.assign({}, acc), { [`process.env.${key}`]: JSON.stringify(env[key]) });
199
+ }, {});
139
200
  config.plugin('define').use(webpack_1.default.DefinePlugin, [
140
- Object.assign(Object.assign(Object.assign({}, Object.keys(process.env).reduce((prev, key) => {
141
- if (key.startsWith(constants_1.PUBLIC_ENV_PREFIX)) {
142
- prev[`process.env.${key}`] = JSON.stringify(process.env[key]);
143
- }
144
- return prev;
145
- }, {})), Object.keys(env).reduce((acc, key) => {
146
- if (/^(?:NODE_.+)|^(?:__.+)$/i.test(key)) {
147
- throw new Error(`The key "${key}" under "env" is not allowed.`);
148
- }
149
- return Object.assign(Object.assign({}, acc), { [`process.env.${key}`]: JSON.stringify(env[key]) });
150
- }, {})), { 'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production') })
201
+ Object.assign(Object.assign(Object.assign({}, shuviPublicEnv), shuviConfigEnv), { 'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production') })
151
202
  ]);
152
203
  config
153
204
  .plugin('private/build-manifest')
154
205
  .use(build_manifest_plugin_1.default, [
155
206
  { filename: buildManifestFilename, chunkRequest: dev }
156
207
  ]);
157
- if (useTypeScript) {
158
- config
159
- .plugin('private/fork-ts-checker-webpack-plugin')
160
- .use(require.resolve('fork-ts-checker-webpack-plugin'), [
161
- {
162
- typescript: {
163
- configFile: tsConfigPath,
164
- typeScriptPath,
165
- diagnosticOptions: {
166
- syntactic: true
167
- }
168
- },
169
- async: dev,
170
- logger: {
171
- infrastructure: 'silent',
172
- issues: 'silent',
173
- devServer: false
174
- },
175
- formatter: 'codeframe'
176
- }
208
+ const getCacheConfig = () => {
209
+ const projectHash = crypto
210
+ .createHash('md5')
211
+ .update(projectRoot)
212
+ .digest('hex');
213
+ const stringifiedEnvs = Object.entries(Object.assign(Object.assign({}, shuviConfigEnv), shuviPublicEnv)).reduce((prev, [key, value]) => {
214
+ return `${prev}|${key}=${value}`;
215
+ }, '');
216
+ const PACKAGE_JSON = path.resolve(__dirname, '../../../package.json');
217
+ const SHUVI_VERSION = require(PACKAGE_JSON).version;
218
+ return {
219
+ cacheDirectory: path.join(cacheDir, 'webpack', projectHash),
220
+ type: 'filesystem',
221
+ name: `${name.replace(/\//, '-')}-${config.get('mode')}`,
222
+ version: `${SHUVI_VERSION}|${stringifiedEnvs}`
223
+ };
224
+ };
225
+ config.cache(typeof process.env.SHUVI_DEV_DISABLE_CACHE !== 'undefined'
226
+ ? false
227
+ : getCacheConfig());
228
+ if (((_a = typescript === null || typescript === void 0 ? void 0 : typescript.tsCompilerOptions) === null || _a === void 0 ? void 0 : _a.paths) && (typescript === null || typescript === void 0 ? void 0 : typescript.resolvedBaseUrl)) {
229
+ config.resolve
230
+ .plugin('jsconfig-paths-plugin')
231
+ .use(jsconfig_paths_plugin_1.default, [
232
+ typescript.tsCompilerOptions.paths,
233
+ typescript.resolvedBaseUrl
177
234
  ]);
178
235
  }
236
+ if (!(typescript === null || typescript === void 0 ? void 0 : typescript.useTypeScript)) {
237
+ config.resolve
238
+ .plugin('support-ts-extension-resolver-plugin')
239
+ .use(support_ts_extension_resolver_plugin_1.default);
240
+ }
179
241
  if (dev) {
180
- config.cache({
181
- type: 'memory'
182
- });
183
- // For future webpack-dev-server purpose
242
+ // For webpack-dev-middleware usage
184
243
  config.watchOptions({
185
- ignored: ['**/.git/**', '**/node_modules/**']
244
+ aggregateTimeout: 5,
245
+ ignored: ['**/.git/**']
186
246
  });
187
247
  config.set('infrastructureLogging', {
188
248
  level: 'none'
189
249
  });
190
- config.plugin('private/module-replace-plugin').use(module_replace_plugin_1.default, [
191
- {
192
- modules: [
193
- {
194
- test: RegExp(escapeRegExp_1.escapeRegExp(`?${constants_1.ROUTE_RESOURCE_QUERYSTRING}`)),
195
- module: dumbRouteComponent
196
- }
197
- ]
198
- }
199
- ]);
200
- // Even though require.cache is server only we have to clear assets from both compilations
201
- // This is because the client compilation generates the build manifest that's used on the server side
202
- config
203
- .plugin('private/require-cache-hot-reloader')
204
- .use(require_cache_hot_reloader_plugin_1.default);
205
- config.optimization.usedExports(false);
250
+ config.plugin('private/fix-watching-plugin').use(fix_watching_plugin_1.default);
206
251
  }
207
252
  else {
208
253
  config
@@ -1,8 +1,7 @@
1
- import WebpackChain from 'webpack-chain';
2
- import { BaseOptions } from './base';
3
- import { IWebpackHelpers } from '@shuvi/types/src/bundler';
1
+ import { IWebpackHelpers } from '../types';
2
+ import { WebpackChain, BaseOptions } from './base';
4
3
  export interface BrowserOptions extends BaseOptions {
5
4
  webpackHelpers: IWebpackHelpers;
6
5
  analyze?: boolean;
7
6
  }
8
- export declare function createBrowserWebpackChain({ webpackHelpers, ...baseOptions }: BrowserOptions): WebpackChain;
7
+ export declare function createBrowserWebpackChain(options: BrowserOptions): WebpackChain;
@@ -1,63 +1,145 @@
1
1
  "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
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;
12
24
  };
13
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
27
  };
16
28
  Object.defineProperty(exports, "__esModule", { value: true });
17
- const crypto_1 = __importDefault(require("crypto"));
29
+ exports.createBrowserWebpackChain = void 0;
30
+ const crypto = __importStar(require("crypto"));
18
31
  const webpack_1 = __importDefault(require("webpack"));
32
+ const path = __importStar(require("path"));
19
33
  const webpack_bundle_analyzer_1 = require("webpack-bundle-analyzer");
20
- const detectTypescript_1 = require("@shuvi/utils/lib/detectTypescript");
21
- const prefer_resolver_plugin_1 = __importDefault(require("../plugins/prefer-resolver-plugin"));
34
+ const resolve_1 = require("@shuvi/utils/lib/resolve");
35
+ // import PreferResolverPlugin from '../plugins/prefer-resolver-plugin';
36
+ const constants_1 = require("../../constants");
37
+ const dynamic_public_path_plugin_1 = __importDefault(require("../plugins/dynamic-public-path-plugin"));
22
38
  const base_1 = require("./base");
23
39
  const style_1 = require("./parts/style");
40
+ const helpers_1 = require("./parts/helpers");
24
41
  const BIG_LIBRARY_THRESHOLD = 160000; // byte
25
- function createBrowserWebpackChain(_a) {
26
- var { webpackHelpers } = _a, baseOptions = __rest(_a, ["webpackHelpers"]);
27
- const { dev, publicPath, analyze } = baseOptions;
28
- const chain = base_1.baseWebpackChain(baseOptions);
29
- const { useTypeScript } = detectTypescript_1.getTypeScriptInfo(baseOptions.projectRoot);
42
+ const SHUVI_PKGS_REGEX = /[\\/]node_modules[\\/]@shuvi[\\/]/;
43
+ const FRAMEWORK_REACT_MODULES = [
44
+ {
45
+ test: /[/\\]node_modules[/\\]react[/\\]/
46
+ },
47
+ {
48
+ test: /[/\\]node_modules[/\\]react-dom[/\\]/
49
+ },
50
+ {
51
+ test: /[/\\]node_modules[/\\]scheduler[/\\]/,
52
+ issuers: [/[/\\]node_modules[/\\]react-dom[/\\]/]
53
+ }
54
+ ];
55
+ function createBrowserWebpackChain(options) {
56
+ const { cacheDir, typescript, dev, publicPath, analyze } = options;
57
+ const chain = (0, base_1.baseWebpackChain)(options);
58
+ const useTypeScript = !!(typescript === null || typescript === void 0 ? void 0 : typescript.useTypeScript);
30
59
  chain.target('web');
31
60
  chain.devtool(dev ? 'cheap-module-source-map' : false);
32
61
  chain.resolve.extensions.merge([
33
- ...(useTypeScript ? ['.tsx', '.ts'] : []),
62
+ ...(useTypeScript ? ['.ts', '.tsx'] : []),
34
63
  '.mjs',
35
64
  '.js',
36
65
  '.jsx',
37
66
  '.json',
38
67
  '.wasm'
39
68
  ]);
40
- if (baseOptions.target) {
41
- chain.resolve
42
- .plugin('private/prefer-resolver-plugin')
43
- .use(prefer_resolver_plugin_1.default, [{ suffix: baseOptions.target }]);
69
+ if (useTypeScript) {
70
+ chain
71
+ .plugin('private/fork-ts-checker-webpack-plugin')
72
+ .use(require.resolve('fork-ts-checker-webpack-plugin'), [
73
+ {
74
+ typescript: {
75
+ configFile: typescript.tsConfigPath,
76
+ mode: 'write-references',
77
+ typeScriptPath: typescript.typeScriptPath,
78
+ diagnosticOptions: {
79
+ syntactic: true
80
+ },
81
+ configOverwrite: {
82
+ compilerOptions: {
83
+ incremental: true,
84
+ tsBuildInfoFile: path.resolve(cacheDir, 'tsbuildinfo')
85
+ }
86
+ }
87
+ },
88
+ async: dev,
89
+ logger: {
90
+ infrastructure: 'silent',
91
+ issues: 'silent',
92
+ devServer: false
93
+ },
94
+ formatter: 'codeframe'
95
+ }
96
+ ]);
44
97
  }
98
+ // if (options.target) {
99
+ // chain.resolve
100
+ // .plugin('private/prefer-resolver-plugin')
101
+ // .use(PreferResolverPlugin, [{ suffix: options.target }]);
102
+ // }
45
103
  if (dev) {
46
104
  chain.plugin('private/hmr-plugin').use(webpack_1.default.HotModuleReplacementPlugin);
47
105
  }
48
106
  else {
49
107
  chain.optimization.splitChunks({
50
- chunks: 'all',
108
+ chunks: helpers_1.splitChunksFilter,
51
109
  cacheGroups: {
52
110
  default: false,
53
111
  defaultVendors: false,
54
112
  framework: {
55
113
  chunks: 'all',
56
114
  name: 'framework',
57
- // This regex ignores nested copies of framework libraries so they're
58
- // bundled with their issuer.
59
- // https://github.com/zeit/next.js/pull/9012
60
- test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|@shuvi\/router|@shuvi\/router-react|scheduler|prop-types|use-subscription|history)[\\/]/,
115
+ filename: constants_1.CommonChunkFilename,
116
+ test(module) {
117
+ const resource = module.nameForCondition();
118
+ if (!resource) {
119
+ return false;
120
+ }
121
+ if (SHUVI_PKGS_REGEX.test(resource)) {
122
+ return true;
123
+ }
124
+ return FRAMEWORK_REACT_MODULES.some(frameworkModule => {
125
+ if (!frameworkModule.test.test(resource)) {
126
+ return false;
127
+ }
128
+ // Check issuer to ignore nested copies of framework libraries so they're
129
+ // bundled with their issuer.
130
+ // https://github.com/zeit/next.js/pull/9012
131
+ if (frameworkModule.issuers) {
132
+ for (const issuer of frameworkModule.issuers) {
133
+ const issuerResource = module.issuer.nameForCondition();
134
+ if (!issuer.test(issuerResource)) {
135
+ return false;
136
+ }
137
+ }
138
+ }
139
+ return true;
140
+ });
141
+ },
142
+ // test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|@shuvi[\\/]router|@shuvi[\\/]router-react|scheduler|prop-types|use-sync-external-store|history)[\\/]/,
61
143
  priority: 40,
62
144
  // Don't let webpack eliminate this chunk (prevents this chunk from
63
145
  // becoming a part of the commons chunk)
@@ -66,10 +148,10 @@ function createBrowserWebpackChain(_a) {
66
148
  lib: {
67
149
  test(module) {
68
150
  return (module.size() > BIG_LIBRARY_THRESHOLD &&
69
- /node_modules[/\\]/.test(module.identifier()));
151
+ /[/\\]node_modules[/\\]/.test(module.nameForCondition() || ''));
70
152
  },
71
153
  name(module) {
72
- const hash = crypto_1.default.createHash('sha1');
154
+ const hash = crypto.createHash('sha1');
73
155
  if (module.type === `css/mini-extract`) {
74
156
  module.updateHash(hash);
75
157
  }
@@ -77,31 +159,14 @@ function createBrowserWebpackChain(_a) {
77
159
  if (!module.libIdent) {
78
160
  throw new Error(`Encountered unknown module type: ${module.type}. Please open an issue.`);
79
161
  }
80
- hash.update(module.libIdent({ context: baseOptions.projectRoot }));
162
+ hash.update(module.libIdent({ context: options.projectRoot }));
81
163
  }
82
164
  return hash.digest('hex').substring(0, 8);
83
165
  },
166
+ filename: constants_1.CommonChunkFilename,
84
167
  priority: 30,
85
168
  minChunks: 1,
86
169
  reuseExistingChunk: true
87
- },
88
- commons: {
89
- name: 'commons',
90
- minChunks: 2,
91
- priority: 20
92
- },
93
- shared: {
94
- name(module, chunks) {
95
- return crypto_1.default
96
- .createHash('sha1')
97
- .update(chunks.reduce((acc, chunk) => {
98
- return acc + chunk.name;
99
- }, ''))
100
- .digest('hex');
101
- },
102
- priority: 10,
103
- minChunks: 2,
104
- reuseExistingChunk: true
105
170
  }
106
171
  },
107
172
  maxInitialRequests: 25,
@@ -118,12 +183,16 @@ function createBrowserWebpackChain(_a) {
118
183
  ]);
119
184
  }
120
185
  }
121
- chain.resolve.alias
122
- .set('stream', require.resolve('stream-browserify'))
123
- .set('path', require.resolve('path-browserify'))
124
- .set('crypto', require.resolve('crypto-browserify'))
125
- .set('buffer', require.resolve('buffer'))
126
- .set('vm', require.resolve('vm-browserify'));
186
+ // node polyfills
187
+ chain.resolve.set('fallback', {
188
+ buffer: (0, resolve_1.resolve)('buffer', {
189
+ includeCoreModules: false
190
+ }),
191
+ crypto: require.resolve('crypto-browserify'),
192
+ path: require.resolve('path-browserify'),
193
+ stream: require.resolve('stream-browserify'),
194
+ vm: require.resolve('vm-browserify')
195
+ });
127
196
  chain.plugin('node-buffer-polyfill').use(webpack_1.default.ProvidePlugin, [
128
197
  {
129
198
  Buffer: ['buffer', 'Buffer']
@@ -139,9 +208,16 @@ function createBrowserWebpackChain(_a) {
139
208
  // prevent errof of destructing process.env
140
209
  'process.env': JSON.stringify('{}') })
141
210
  ]);
211
+ chain.plugin('dynamic-public-path-plugin').use(dynamic_public_path_plugin_1.default);
142
212
  chain.plugin('private/build-manifest').tap(([options]) => [
143
213
  Object.assign(Object.assign({}, options), { modules: true })
144
214
  ]);
145
- return style_1.withStyle(chain, { extractCss: !dev, publicPath });
215
+ return (0, style_1.withStyle)(chain, {
216
+ extractCss: !dev,
217
+ publicPath,
218
+ parcelCss: options.parcelCss,
219
+ filename: 'static/css/[contenthash:8].css',
220
+ chunkFilename: 'static/css/[contenthash:8].chunk.css'
221
+ });
146
222
  }
147
223
  exports.createBrowserWebpackChain = createBrowserWebpackChain;
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
5
16
  Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./base"));
7
- __export(require("./browser"));
8
- __export(require("./node"));
9
- __export(require("./parts/helpers"));
17
+ __exportStar(require("./base"), exports);
18
+ __exportStar(require("./browser"), exports);
19
+ __exportStar(require("./node"), exports);
20
+ __exportStar(require("./parts/helpers"), exports);
@@ -1,7 +1,6 @@
1
- import WebpackChain from 'webpack-chain';
2
- import { BaseOptions } from './base';
3
- import { IWebpackHelpers } from '@shuvi/types/src/bundler';
1
+ import { WebpackChain, BaseOptions } from './base';
2
+ import { IWebpackHelpers } from '../types';
4
3
  export interface NodeOptions extends BaseOptions {
5
4
  webpackHelpers: IWebpackHelpers;
6
5
  }
7
- export declare function createNodeWebpackChain({ webpackHelpers, ...baseOptions }: NodeOptions): WebpackChain;
6
+ export declare function createNodeWebpackChain(options: NodeOptions): WebpackChain;