-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
121 lines (115 loc) · 2.78 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
/**
* External dependencies
*/
const TerserPlugin = require( 'terser-webpack-plugin' );
const MiniCssExtractPlugin = require( "mini-css-extract-plugin" );
const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' ).BundleAnalyzerPlugin;
const path = require( 'path' );
const files = [
'customizer',
'customizer-preview',
'customizer-search',
'dark-mode',
'settings',
];
function camelize( str ) {
const arr = str.split( '-' );
return arr.slice(1).reduce( ( acc, curr ) => {
return acc + curr.charAt(0).toUpperCase() + curr.slice(1).toLowerCase();
}, arr[0] );
}
function kebabize( str ) {
return str.replace( /([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2' ).toLowerCase();
}
const entries = files.reduce( ( acc, curr ) => {
acc[ camelize( curr ) ] = `./src/_js/${ curr }/index.js`;
return acc;
}, {} );
module.exports = {
mode: 'production',
entry: entries,
output: {
path: path.join( __dirname, "dist/js" ),
filename: pathData => {
return `${ kebabize( pathData.chunk.name ) }.js`;
},
library: {
name: [ 'sm', '[name]' ],
type: 'window'
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
modules: false
}
],
'@babel/preset-react',
],
}
},
sideEffects: false
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
// Translates CSS into CommonJS
{
loader: "css-loader",
options: {
url: false,
},
},
// Compiles Sass to CSS
"sass-loader",
],
sideEffects: true
},
{
test: /\.svg$/,
loader: 'svg-sprite-loader'
}
],
},
externals: {
jquery: 'jQuery',
lodash: 'lodash',
react: 'React',
'chroma-js': 'chroma',
'react-dom': 'ReactDOM',
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin( {
include: /\.js$/,
extractComments: {
condition: true,
filename: ( fileData ) => {
// The "fileData" argument contains object with "filename", "basename", "query" and "hash"
return `${ fileData.filename }.LICENSE.txt${ fileData.query }`;
},
},
} )
],
},
'plugins': [
new BundleAnalyzerPlugin,
new MiniCssExtractPlugin( {
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css",
} ),
]
};