This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
141 lines (130 loc) · 3.57 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
'use strict';
const { relative } = require('path');
const {
EnvironmentPlugin,
HashedModuleIdsPlugin,
NamedModulesPlugin,
optimize: { ModuleConcatenationPlugin },
} = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const { join, trimSlashes } = require('pathifist');
const getModules = require('../utils/modules');
module.exports = function getConfig(config, name) {
const getAssetPath = (...arg) => trimSlashes(join(config.assetPath, ...arg));
const isProduction = process.env.NODE_ENV === 'production';
const jsLoaderConfig = {
test: [/\.m?js$/],
exclude: [/node_modules\/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 },
include: [],
exclude: [],
},
],
],
plugins: [require.resolve('@babel/plugin-syntax-dynamic-import')],
sourceType: 'unambiguous',
},
};
const urlLoaderConfig = {
test: [/\.(png|gif|jpe?g|webp)$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: getAssetPath('[name]-[hash:16].[ext]'),
},
};
const fileLoaderConfig = {
exclude: [/\.(?:m?js|html|json)$/],
loader: require.resolve('file-loader'),
options: {
name: getAssetPath('[name]-[hash:16].[ext]'),
},
};
const allLoaderConfigs = [jsLoaderConfig, urlLoaderConfig, fileLoaderConfig];
return {
// invalid for webpack, needed with untool
loaderConfigs: {
jsLoaderConfig,
urlLoaderConfig,
fileLoaderConfig,
allLoaderConfigs,
},
name,
mode: isProduction ? 'production' : 'development',
bail: isProduction,
context: config.rootDir,
entry: require.resolve('../shims/browser'),
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),
},
resolve: {
modules: getModules(config.rootDir),
alias: {
'@untool/entrypoint': config.rootDir,
},
extensions: ['.mjs', '.js'],
mainFields: [
'esnext:browser',
'jsnext:browser',
'browser',
'module',
'esnext',
'jsnext',
'esnext:main',
'jsnext:main',
'main',
],
},
module: {
rules: [{ oneOf: allLoaderConfigs }],
},
externals: [],
optimization: {
splitChunks: {
chunks: 'all',
name: false,
},
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
compress: {
inline: 1, // https://github.com/mishoo/UglifyJS2/issues/2842
},
output: { comments: false },
},
}),
],
},
plugins: [
new (isProduction ? HashedModuleIdsPlugin : NamedModulesPlugin)(),
new ModuleConcatenationPlugin(),
new EnvironmentPlugin({ NODE_ENV: 'development' }),
],
performance: {
hints: 'warning',
maxEntrypointSize: 262144,
},
devtool: 'hidden-source-map',
};
};