-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbuild.js
154 lines (148 loc) · 4.07 KB
/
build.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
const { dirname, relative } = require('path');
const { EnvironmentPlugin, DefinePlugin } = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const { join, trimSlashes } = require('pathifist');
const getModules = require('../utils/modules');
module.exports = function getConfig(config, name, buildDependencies) {
const getAssetPath = (...arg) => trimSlashes(join(config.assetPath, ...arg));
const isProduction = process.env.NODE_ENV === 'production';
const jsLoaderConfig = {
test: [/\.m?js$/],
// eslint-disable-next-line no-useless-escape
exclude: [/node_modules[\/\\](webpack[\/\\]buildin|core-js)/],
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
compact: isProduction,
cacheDirectory: true,
cacheIdentifier: `${process.env.NODE_ENV || 'development'}:${name}`,
presets: [
[
require.resolve('@babel/preset-env'),
{
modules: false,
useBuiltIns: 'usage',
targets: { browsers: config.browsers },
corejs: 3,
include: [],
exclude: [],
},
],
],
plugins: [],
sourceType: 'unambiguous',
},
};
const fileLoaderConfig = {
exclude: [/\.(?:m?js|html|json)$/],
type: 'asset/resource',
generator: {
filename: getAssetPath('[name]-[contenthash:16][ext]'),
},
};
const urlLoaderConfig = {
test: [/\.(png|gif|jpe?g|webp)$/],
oneOf: [
{
resourceQuery: /noinline/,
...fileLoaderConfig,
},
{
type: 'asset',
generator: {
filename: getAssetPath('[name]-[contenthash:16][ext]'),
},
parser: {
dataUrlCondition: {
maxSize: 10000,
},
},
},
],
};
const allLoaderConfigs = [jsLoaderConfig, urlLoaderConfig, fileLoaderConfig];
return {
// invalid for webpack, needed for hops mixings
loaderConfigs: {
jsLoaderConfig,
urlLoaderConfig,
fileLoaderConfig,
allLoaderConfigs,
},
name,
target: ['web', 'es5'],
mode: isProduction ? 'production' : 'development',
bail: isProduction,
context: config.rootDir,
entry: require.resolve('../shims/build'),
output: {
path: config.buildDir,
publicPath: '/',
pathinfo: true,
filename: getAssetPath(`${config.name}-[chunkhash:12].js`),
chunkFilename: getAssetPath(`${config.name}-[id]-[chunkhash:12].js`),
devtoolModuleFilenameTemplate: (info) =>
relative(config.rootDir, info.absoluteResourcePath),
},
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename].concat(buildDependencies),
},
},
snapshot: {
buildDependencies: {
hash: true,
timestamp: false,
},
},
resolve: {
modules: getModules(config.rootDir),
alias: {
'hops/entrypoint': config.rootDir,
'regenerator-runtime': dirname(
require.resolve('regenerator-runtime/package.json')
),
'core-js': dirname(require.resolve('core-js/package.json')),
},
extensions: ['.mjs', '.js'],
mainFields: [
'esnext:browser',
'jsnext:browser',
'browser',
'module',
'esnext',
'jsnext',
'esnext:main',
'jsnext:main',
'main',
],
},
module: {
rules: [{ oneOf: allLoaderConfigs }],
},
externals: [],
optimization: {
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
output: { comments: false },
},
}),
],
},
plugins: [
// Needed for bootstrap/lib/utils#environmentalize, which falls
// back to `process.env` if there's no global variable `_env`
new DefinePlugin({ 'process.env': JSON.stringify({}) }),
new EnvironmentPlugin({ NODE_ENV: 'development' }),
],
performance: {
hints: false,
maxEntrypointSize: 262144,
maxAssetSize: 262144,
},
devtool: 'hidden-source-map',
};
};