-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
143 lines (138 loc) · 3.49 KB
/
Copy pathwebpack.config.js
File metadata and controls
143 lines (138 loc) · 3.49 KB
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
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production';
const isAnalyze = process.env.ANALYZE === 'true';
// Clean dist folder once before all builds
if (isProduction && !global.webpackCleanDone) {
const { execSync } = require('child_process');
execSync('rm -rf dist', { stdio: 'inherit' });
global.webpackCleanDone = true;
}
const commonConfig = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
optimization: {
minimize: isProduction,
minimizer: [
new TerserPlugin({
terserOptions: {
parse: {
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
comparisons: false,
inline: 2,
drop_console: isProduction,
drop_debugger: isProduction,
pure_funcs: isProduction ? ['console.log', 'console.info', 'console.debug', 'console.warn'] : [],
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
ascii_only: true,
},
},
extractComments: false,
})
],
// Tree shaking optimizations
usedExports: true,
sideEffects: false,
},
performance: {
// Increase limits for library bundles
hints: isProduction ? 'warning' : false,
maxEntrypointSize: 512000, // 500 KiB
maxAssetSize: 512000, // 500 KiB
}
};
const configs = [
// CommonJS build
{
...commonConfig,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'templates.js',
library: {
type: 'commonjs2'
}
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'src/template.js', to: 'template.js' }
]
}),
...(isAnalyze ? [new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: 'bundle-report-cjs.html',
openAnalyzer: false
})] : [])
]
},
// ESM build
{
...commonConfig,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'templates.esm.js',
library: {
type: 'module'
}
},
experiments: {
outputModule: true
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'src/template.js', to: 'template.js' }
]
})
]
},
// UMD build for browser usage
{
...commonConfig,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'templates.umd.js',
library: {
name: 'FyndStorefrontTemplates',
type: 'umd'
},
globalObject: 'this'
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'src/template.js', to: 'template.js' }
]
})
]
}
];
return configs;
};