-
Notifications
You must be signed in to change notification settings - Fork 44
/
webpack.config.js
156 lines (141 loc) · 2.96 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
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
155
156
/* global process __dirname */
const DEV = 'production' !== process.env.NODE_ENV;
/**
* Plugins
*/
const path = require( 'path' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const OptimizeCssAssetsPlugin = require( 'optimize-css-assets-webpack-plugin' );
const cssnano = require( 'cssnano' );
const CleanWebpackPlugin = require( 'clean-webpack-plugin' );
const TerserPlugin = require('terser-webpack-plugin');
const StyleLintPlugin = require( 'stylelint-webpack-plugin' );
const FriendlyErrorsPlugin = require( 'friendly-errors-webpack-plugin' );
// JS Directory path.
const JSDir = path.resolve( __dirname, 'src/js' );
const IMG_DIR = path.resolve( __dirname, 'src/img' );
const FONTS_DIR = path.resolve( __dirname, 'src/fonts' );
const DIST_DIR = path.resolve( __dirname, 'dist' );
const entry = {
admin: JSDir + '/admin.js',
public: JSDir + '/public.js',
};
const output = {
path: DIST_DIR,
filename: 'js/[name].js'
};
/**
* Note: argv.mode will return 'development' or 'production'.
*/
const plugins = ( argv ) => [
new CleanWebpackPlugin( [ DIST_DIR ] ),
new MiniCssExtractPlugin( {
filename: 'css/[name].css'
} ),
new StyleLintPlugin( {
'extends': 'stylelint-config-wordpress/scss'
} ),
new FriendlyErrorsPlugin( {
clearConsole: false
} )
];
const rules = [
{
enforce: 'pre',
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: 'eslint-loader'
},
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
'targets': {
'browsers': [
'last 2 Chrome versions',
'last 2 Firefox versions',
'last 2 Safari versions',
'last 2 iOS versions',
'last 1 Android version',
'last 1 ChromeAndroid version',
'ie 11'
]
}
}
]
]
}
},
include: [
JSDir,
path.resolve( __dirname, 'node_modules/foundation-sites' )
]
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
{
test: /\.(png|jpg|svg|jpeg|gif|ico)$/,
exclude: [ FONTS_DIR, /node_modules/ ],
use: {
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
publicPath: '../'
}
}
},
{
test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
exclude: [ IMG_DIR, /node_modules/ ],
use: {
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
publicPath: '../'
}
}
}
];
const optimization = [
new OptimizeCssAssetsPlugin( {
cssProcessor: cssnano
} ),
new TerserPlugin( {
cache: false,
parallel: true,
sourceMap: false,
extractComments: false,
terserOptions: {
mangle: {
reserved: ["__"]
}
}
} )
];
module.exports = ( env, argv ) => ( {
entry: entry,
output: output,
plugins: plugins( argv ),
devtool: 'source-map',
module: {
'rules': rules
},
optimization: {
minimizer: optimization
},
externals: {
jquery: 'jQuery'
}
} );