-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathwebpack.config.js
108 lines (96 loc) · 2.3 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
const glob = require( 'glob' );
const path = require( 'path' );
const CssMinimizerWebpackPlugin = require( 'css-minimizer-webpack-plugin' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const WebpackRemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
const webPackModule = ( production ) => {
return {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [ '@babel/preset-env' ],
},
},
{
test: /\.s?css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: path.join( __dirname, 'assets' ),
},
},
{
loader: 'css-loader',
options: {
sourceMap: ! production,
url: false,
},
},
],
},
],
};
};
const lookup = ( lookupPath, prefix ) => {
const ext = path.extname( lookupPath );
const entries = {};
glob.sync( lookupPath ).map( ( filePath ) => {
if ( filePath.includes( '.min' + ext ) ) {
return filePath;
}
let filename = path.basename( filePath, ext );
if ( 'app' === filename ) {
filename = path.basename( path.dirname( filePath ) );
}
entries[ prefix + '/' + filename ] = path.resolve( filePath );
return filePath;
} );
return entries;
};
const hcaptcha = ( env ) => {
/**
* @param env.production
*/
const production = env.production ? env.production : false;
const cssEntries = lookup( './assets/css/*.css', 'css' );
const jsEntries = lookup( './assets/js/*.js', 'js' );
const appEntries = lookup( './src/js/**/app.js', 'js/apps' );
const entries = {
...cssEntries,
...jsEntries,
...appEntries,
};
return {
devtool: production ? false : 'eval-source-map',
entry: entries,
module: webPackModule( production ),
output: {
path: path.join( __dirname, 'assets' ),
filename: ( pathData ) => {
return pathData.chunk.name.includes( 'apps' )
? '[name].js'
: '[name].min.js';
},
},
plugins: [
new WebpackRemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin( {
filename: '[name].min.css',
} ),
],
optimization: {
minimizer: [
new TerserPlugin( {
extractComments: false,
} ),
new CssMinimizerWebpackPlugin(),
],
},
};
};
module.exports = hcaptcha;