-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathwebpack-base-config.js
407 lines (381 loc) · 10.4 KB
/
webpack-base-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const webpack = require('webpack');
const path = require('path');
const { resolve, dirname } = require('path');
const { readFileSync, existsSync } = require('fs');
const { isInstalledVersionPreactXOrAbove } = require('./utils');
const autoprefixer = require('autoprefixer');
const browserslist = require('browserslist');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const ReplacePlugin = require('webpack-plugin-replace');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const createBabelConfig = require('../babel-config');
const loadPostcssConfig = require('postcss-load-config');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
function readJson(file) {
try {
return JSON.parse(readFileSync(file, 'utf8'));
} catch (e) {}
}
function findAllNodeModules(startDir) {
let dir = path.resolve(startDir);
let dirs = [];
const { root } = path.parse(startDir);
// eslint-disable-next-line no-constant-condition
while (true) {
const joined = path.join(dir, 'node_modules');
if (existsSync(joined)) {
dirs.push(joined);
}
if (dir === root) {
return dirs;
}
dir = path.dirname(dir);
}
}
function resolveTsconfig(cwd, isProd) {
if (existsSync(resolve(cwd, `tsconfig.${isProd ? 'prod' : 'dev'}.json`))) {
return resolve(cwd, `tsconfig.${isProd ? 'prod' : 'dev'}.json`);
} else if (existsSync(resolve(cwd, 'tsconfig.json'))) {
return resolve(cwd, 'tsconfig.json');
}
}
function getSassConfiguration(...includePaths) {
const config = {
sourceMap: true,
sassOptions: {
includePaths,
},
};
Object.defineProperty(config, 'includePaths', { value: includePaths });
return config;
}
/**
* @returns {import('webpack').Configuration}
*/
module.exports = function createBaseConfig(env) {
const { cwd, isProd, isWatch, src, source } = env;
const babelConfigFile = env.babelConfig || '.babelrc';
const IS_SOURCE_PREACT_X_OR_ABOVE = isInstalledVersionPreactXOrAbove(cwd);
// Apply base-level `env` values
env.dest = resolve(cwd, env.dest || 'build');
env.manifest = readJson(source('manifest.json')) || {};
env.pkg = readJson(resolve(cwd, 'package.json')) || {};
let babelrc = readJson(resolve(cwd, babelConfigFile)) || {};
// use browserslist config environment, config default, or default browsers
// default browsers are > 0.25% global market share or Internet Explorer >= 9
const browserslistDefaults = ['> 0.25%', 'IE >= 9'];
const browserlistConfig = Object(browserslist.findConfig(cwd));
const browsers =
(isProd ? browserlistConfig.production : browserlistConfig.development) ||
browserlistConfig.defaults ||
browserslistDefaults;
let userNodeModules = findAllNodeModules(cwd);
let cliNodeModules = findAllNodeModules(__dirname);
let nodeModules = [...new Set([...userNodeModules, ...cliNodeModules])];
let compat = 'preact-compat';
try {
compat = dirname(
require.resolve('preact/compat/package.json', { paths: [cwd] })
);
} catch (e) {
try {
compat = dirname(
require.resolve('preact-compat/package.json', { paths: [cwd] })
);
} catch (e) {}
}
let tsconfig = resolveTsconfig(cwd, isProd);
let postcssPlugins;
try {
postcssPlugins = loadPostcssConfig.sync(cwd).plugins;
} catch (error) {
postcssPlugins = [autoprefixer({ overrideBrowserslist: browsers })];
}
function tryResolveOptionalLoader(name) {
try {
return require.resolve(name);
} catch (e) {
return name;
}
}
return {
context: src,
resolve: {
modules: ['node_modules', ...nodeModules],
extensions: [
'.mjs',
'.js',
'.jsx',
'.ts',
'.tsx',
'.json',
'.less',
'.pcss',
'.scss',
'.sass',
'.styl',
'.css',
'.wasm',
],
alias: {
style: source('style'),
'preact-cli-entrypoint': source('index'),
url: dirname(require.resolve('native-url/package.json')),
// preact-compat aliases for supporting React dependencies:
react: compat,
'react-dom': compat,
'preact-compat': compat,
'react-addons-css-transition-group': 'preact-css-transition-group',
'preact-cli/async-component': IS_SOURCE_PREACT_X_OR_ABOVE
? require.resolve('@preact/async-loader/async')
: require.resolve('@preact/async-loader/async-legacy'),
},
plugins: [
// TODO: Remove when upgrading to webpack 5
PnpWebpackPlugin,
],
},
resolveLoader: {
modules: [...nodeModules],
alias: {
'proxy-loader': require.resolve('./proxy-loader'),
},
},
module: {
rules: [
{
// ES2015
enforce: 'pre',
test: /\.m?[jt]sx?$/,
resolve: { mainFields: ['module', 'jsnext:main', 'browser', 'main'] },
type: 'javascript/auto',
use: [
{
loader: require.resolve('babel-loader'),
options: Object.assign(
{ babelrc: false },
createBabelConfig(env, { browsers }),
babelrc // intentionally overwrite our settings
),
},
require.resolve('source-map-loader'),
],
},
{
// LESS
enforce: 'pre',
test: /\.less$/,
use: [
{
loader: require.resolve('./proxy-loader'),
options: {
cwd,
loader: tryResolveOptionalLoader('less-loader'),
options: {
sourceMap: true,
lessOptions: {
paths: [...nodeModules],
},
},
},
},
],
},
{
// SASS
enforce: 'pre',
test: /\.s[ac]ss$/,
use: [
{
loader: require.resolve('./proxy-loader'),
options: {
cwd,
loader: tryResolveOptionalLoader('sass-loader'),
options: getSassConfiguration(...nodeModules),
},
},
],
},
{
// STYLUS
enforce: 'pre',
test: /\.styl$/,
use: [
{
loader: require.resolve('./proxy-loader'),
options: {
cwd,
loader: tryResolveOptionalLoader('stylus-loader'),
options: {
sourceMap: true,
paths: nodeModules,
},
},
},
],
},
{
// User styles
test: /\.(p?css|less|s[ac]ss|styl)$/,
include: [source('components'), source('routes')],
use: [
isWatch
? require.resolve('style-loader')
: MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
modules: {
localIdentName: '[local]__[hash:base64:5]',
},
importLoaders: 1,
sourceMap: true,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
sourceMap: true,
postcssOptions: {
plugins: postcssPlugins,
},
},
},
],
},
{
// External / `node_module` styles
test: /\.(p?css|less|s[ac]ss|styl)$/,
exclude: [source('components'), source('routes')],
use: [
isWatch
? require.resolve('style-loader')
: MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
sourceMap: true,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
sourceMap: true,
postcssOptions: {
plugins: postcssPlugins,
},
},
},
],
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true,
},
{
test: /\.(xml|html|txt|md)$/,
loader: require.resolve('raw-loader'),
},
{
test: /\.(svg|woff2?|ttf|eot|jpe?g|png|webp|avif|gif|mp4|mov|ogg|webm)(\?.*)?$/i,
loader: isProd
? require.resolve('file-loader')
: require.resolve('url-loader'),
},
],
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin(
Object.keys(process.env)
.filter(key => /^PREACT_APP_/.test(key))
.reduce(
(env, key) => {
env[`process.env.${key}`] = JSON.stringify(process.env[key]);
return env;
},
{
'process.env.NODE_ENV': JSON.stringify(
isProd ? 'production' : 'development'
),
}
)
),
new webpack.ProvidePlugin({
h: ['preact', 'h'],
Fragment: ['preact', 'Fragment'],
}),
// Fix for https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
new FixStyleOnlyEntriesPlugin(),
// Extract CSS
new MiniCssExtractPlugin({
filename: isProd ? '[name].[contenthash:5].css' : '[name].css',
chunkFilename: isProd
? '[name].chunk.[contenthash:5].css'
: '[name].chunk.css',
}),
ProgressBarPlugin({
format:
'\u001b[97m\u001b[44m Build \u001b[49m\u001b[39m [:bar] \u001b[32m\u001b[1m:percent\u001b[22m\u001b[39m (:elapseds) \u001b[2m:msg\u001b[22m',
renderThrottle: 100,
summary: false,
clear: true,
}),
new WebpackManifestPlugin({
fileName: 'asset-manifest.json',
assetHookStage: webpack.Compiler.PROCESS_ASSETS_STAGE_ANALYSE,
// TODO: Remove this next breaking change and use the full filepath from this manifest
// when referring to built assets, i.e.:
// https://github.com/preactjs/preact-cli/blob/master/packages/cli/src/resources/head-end.ejs#L1
// This is just to avoid any potentially breaking changes for right now.
publicPath: '',
}),
...(tsconfig
? [
new ForkTsCheckerWebpackPlugin({
checkSyntacticErrors: true,
async: !isProd,
tsconfig: tsconfig,
silent: !isWatch,
}),
]
: []),
...(isProd
? [
new webpack.HashedModuleIdsPlugin(),
new webpack.LoaderOptionsPlugin({ minimize: true }),
new webpack.optimize.ModuleConcatenationPlugin(),
// strip out babel-helper invariant checks
new ReplacePlugin({
include: /babel-helper$/,
patterns: [
{
regex: /throw\s+(new\s+)?(Type|Reference)?Error\s*\(/g,
value: s => `return;${Array(s.length - 7).join(' ')}(`,
},
],
}),
]
: []),
],
optimization: {
splitChunks: {
minChunks: 3,
},
},
mode: isProd ? 'production' : 'development',
devtool: isWatch ? 'cheap-module-eval-source-map' : 'source-map',
node: {
console: false,
process: false,
Buffer: false,
__filename: false,
__dirname: false,
setImmediate: false,
},
};
};