-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
86 lines (76 loc) · 2.11 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
const path = require('path');
const glob = require('glob');
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
/**
* Retrieve all entries from subdirectories of the src directory.
*
* Valid entry points are index.js and view.js.
*
* @param {string} sourceDir The source directory.
* @param {string} type The type of build in progress.
* @returns {object} An object of entry points.
*/
const getEntries = (sourceDir, type) => {
const entries = {};
const filescan = 'blocks' === type ? '**/*(index|view).js' : '*.js';
const files = glob.sync(sourceDir + '/' + filescan);
files.forEach((file) => {
const relativePath = path.relative(sourceDir, file);
const entry = path.join(
path.dirname(relativePath),
path.basename(relativePath, '.js')
);
entries[entry] = './' + file;
});
return entries;
};
module.exports = (env) => {
// Default to `blocks` if no type is provided. This adjusts the
// entry point search pattern to look for blocks and accounts for
// the block.json file during build.
const type = env.type || 'blocks';
// Default to `src` if no source directory is provided.
const sourceDir = env.dir || 'src';
// Default to `../build` if no build directory is provided.
let buildDir = env.build || path.resolve(__dirname, sourceDir, '../build');
return {
entry: getEntries(sourceDir, type),
output: {
path: path.resolve(__dirname, buildDir),
filename: '[name].js',
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
}),
],
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: ['@babel/plugin-transform-runtime'],
},
},
},
],
},
plugins: [new DependencyExtractionWebpackPlugin()],
// External dependencies that should not be bundled.
externals: {
react: 'React',
'react-dom': 'ReactDOM',
},
};
};