-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
121 lines (116 loc) · 2.68 KB
/
webpack.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
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ChunksWebpackPlugin = require('chunks-webpack-plugin');
/**
* Generator for Webpack configuration according to the target browsers (modern | legacy)
*
* @param {String} browsers Browsers type (modern|legacy)
* @param {Boolean} isProduction Webpack mode (development|production)
* @param {Object} presets Babel presets according to the browsers type (modern|legacy)
*
* @returns {Object} Object with the Webpack base configuration
*/
const generateWebpackConfig = ({ browsers, isProduction, presets }) => {
return {
name: browsers,
watch: !isProduction,
devtool: !isProduction ? 'source-map' : 'none',
entry: {
home: './src/home.js',
news: './src/news.js'
},
output: {
path: path.resolve(__dirname, `./dist/assets/${browsers}`),
filename: '[name].js',
sourceMapFilename: '[file].map'
},
stats: {
modules: false,
entrypoints: false,
excludeAssets: /.map$/,
assetsSort: '!size'
},
module: {
rules: [
{
test: /\.js$/,
include: [path.resolve(__dirname, './src')],
loader: 'babel-loader',
options: {
presets
}
},
{
test: /\.css$/,
include: [path.resolve(__dirname, './src')],
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
resolve: {
extensions: ['.js', '.css']
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[name].css'
}),
new ChunksWebpackPlugin({
outputPath: path.resolve(__dirname, `./dist/templates/${browsers}`),
fileExtension: '.html.twig',
templateStyle: `<link rel="stylesheet" href="{{chunk}}" />`,
templateScript: `<script defer${
browsers === 'modern' ? ' type="module"' : ' nomodule'
} src="{{chunk}}"></script>`
})
],
optimization: {
splitChunks: {
chunks: 'all',
name: true
}
}
};
};
/**
* Export Webpack configuration for modern and legacy browsers
*
* @param {Object} env Node.js environment variables
* @param {Object} argv Options passed to Webpack (argv)
*
* @returns {Array} Array of Webpack configurations
*/
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production';
const configModern = generateWebpackConfig({
browsers: 'modern',
isProduction,
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: true
}
}
]
]
});
const configLegacy = generateWebpackConfig({
browsers: 'legacy',
isProduction,
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: false
},
useBuiltIns: 'usage',
corejs: 3
}
]
]
});
return [configModern, configLegacy];
};