| 1 |
const DEV = 'production' !== process.env.NODE_ENV; |
| 2 |
|
| 3 |
/** |
| 4 |
* NPM Plugins. |
| 5 |
*/ |
| 6 |
const path = require( 'path' ); |
| 7 |
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' ); |
| 8 |
const CssMinimizerPlugin = require( 'css-minimizer-webpack-plugin' ); |
| 9 |
const CleanWebpackPlugin = require( 'clean-webpack-plugin' ); |
| 10 |
const TerserPlugin = require( 'terser-webpack-plugin' ); |
| 11 |
const StyleLintPlugin = require( 'stylelint-webpack-plugin' ); |
| 12 |
const FriendlyErrorsPlugin = require( 'friendly-errors-webpack-plugin' ); |
| 13 |
|
| 14 |
// Assets Directory path. |
| 15 |
const JSDir = path.resolve( __dirname, 'assets/admin/js' ); |
| 16 |
const Assets = path.resolve( __dirname, 'assets/admin' ); |
| 17 |
const BUILD_DIR = path.resolve( __dirname, 'assets/admin/build' ); |
| 18 |
|
| 19 |
// Entry points |
| 20 |
const entry = { |
| 21 |
rmpMain: [ Assets + '/rmp-main.js' ] |
| 22 |
}; |
| 23 |
|
| 24 |
// Outputs |
| 25 |
const output = { |
| 26 |
path: BUILD_DIR, |
| 27 |
filename: DEV ? 'js/[name].js' : 'js/[name].min.js' |
| 28 |
}; |
| 29 |
|
| 30 |
const plugins = ( argv ) => [ |
| 31 |
new CleanWebpackPlugin( [ BUILD_DIR ] ), |
| 32 |
|
| 33 |
new MiniCssExtractPlugin( { |
| 34 |
filename: DEV ? 'css/[name].css' : 'css/[name].min.css' |
| 35 |
} ), |
| 36 |
|
| 37 |
new StyleLintPlugin( { |
| 38 |
'extends': 'stylelint-config-wordpress/scss' |
| 39 |
} ), |
| 40 |
|
| 41 |
new FriendlyErrorsPlugin( { |
| 42 |
clearConsole: false |
| 43 |
} ) |
| 44 |
]; |
| 45 |
|
| 46 |
const rules = [ |
| 47 |
{ |
| 48 |
enforce: 'pre', |
| 49 |
test: /\.js$/, |
| 50 |
exclude: /node_modules/, |
| 51 |
use: 'eslint-loader' |
| 52 |
}, |
| 53 |
{ |
| 54 |
test: /\.js$/, |
| 55 |
include: [ JSDir ], |
| 56 |
exclude: /node_modules/, |
| 57 |
use: 'babel-loader' |
| 58 |
}, |
| 59 |
{ |
| 60 |
test: /\.scss$/, |
| 61 |
exclude: /node_modules/, |
| 62 |
use: [ |
| 63 |
MiniCssExtractPlugin.loader, |
| 64 |
'css-loader', |
| 65 |
'postcss-loader', |
| 66 |
'sass-loader' |
| 67 |
] |
| 68 |
} |
| 69 |
]; |
| 70 |
|
| 71 |
const optimization = [ |
| 72 |
new CssMinimizerPlugin(), |
| 73 |
new TerserPlugin() |
| 74 |
]; |
| 75 |
|
| 76 |
module.exports = ( argv ) => ( { |
| 77 |
entry: entry, |
| 78 |
output: output, |
| 79 |
plugins: plugins( argv ), |
| 80 |
|
| 81 |
module: { |
| 82 |
'rules': rules |
| 83 |
}, |
| 84 |
|
| 85 |
optimization: { |
| 86 |
minimize: true, |
| 87 |
minimizer: optimization |
| 88 |
}, |
| 89 |
|
| 90 |
externals: { |
| 91 |
jquery: 'jQuery' |
| 92 |
} |
| 93 |
|
| 94 |
} ); |
| 95 |
|