This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
163 lines (150 loc) · 3.96 KB
/
index.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
157
158
159
160
161
162
163
const AssetListWebpackPlugin = require("asset-list-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const TerserPlugin = require("terser-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const { DefinePlugin } = require("webpack");
/**
* Delete contents of the output directory using clean-webpack-plugin
*
* @param {Object} options clean-webpack-plugin options
* @return {Object} Plugin config for clean-webpack-plugin
*/
exports.cleanOutput = (options) => ({
plugins: [new CleanWebpackPlugin(options)],
});
/**
* Compile JavaScript using babel-loader
*
* @param {Object} [FnParams={}] Object of function parameters
* @param {Object} [FnParams.exclude] Rule exclude value
* @param {Object} [FnParams.include] Rule include value
* @param {Object} [FnParams.options] babel-loader options
* @param {RegExp} [FnParams.test=/\.m?jsx?$/i] Rule test value
* @return {Object} Module config for babel-loader
*/
exports.compileJS = ({ exclude, include, options, test } = {}) => {
if (!test) test = /\.m?jsx?$/i;
return {
module: {
rules: [
{
test,
exclude,
include,
use: {
loader: "babel-loader",
options,
},
},
],
},
};
};
/**
* Copy files from one location to another with copy-webpack-plugin
*
* @param {Array} [patterns] Array of patterns
* @param {Object} [options] copy-webpack-plugin options
* @return {Object} Plugin config for copy-webpack-plugin
*/
exports.copyFiles = (patterns, options) => ({
plugins: [
new CopyPlugin({
patterns,
options,
}),
],
});
/**
* Generate bundle asset list with asset-list-webpack-plugin
*
* @param {Object} [FnParams={}] Object of function parameters
* @param {Object} [FnParams.format] Format of generated JSON file
* @param {Object} [FnParams.key] Set keys used for JSON file
* @param {Object} [FnParams.name] Name of generated JSON file
* @return {Object} Plugin config for asset-list-webpack-plugin
*/
exports.genAssetList = ({ format, key, name } = {}) => ({
plugins: [new AssetListWebpackPlugin({ format, key, name })],
});
/**
* Generate JavaScript source maps
*
* @param {string} [type] Option to control how source maps are generated
* @return {Object} devtool config
*/
exports.genSourceMaps = (type) => ({
devtool: type,
});
/**
* Exclude modules from bundle with webpack-node-externals
*
* @param {Object} [options] webpack-node-externals options
* @return {Object} externals config
*/
exports.ignoreNodeModules = (options) => ({
externals: [nodeExternals(options)],
});
/**
* Minify JavaScript with terser-webpack-plugin
*
* @param {Object} options terser-webpack-plugin options
* @return {Object} optimization config
*/
exports.minifyJS = (options) => ({
optimization: {
minimize: true,
minimizer: [new TerserPlugin(options)],
},
});
/**
* Set free variable with DefinePlugin
*
* @param {string} key Free variable key
* @param {*} value Free variable value
* @return {Object} Plugin config
*/
exports.setFreeVariable = (key, value) => {
const env = {};
env[key] = JSON.stringify(value);
return {
plugins: [new DefinePlugin(env)],
};
};
/**
* Set mode to determine which webpack optimizations to use
*
* @param {string} [mode] webpack mode configuration optional value
* @return mode config
*/
exports.setMode = (mode) => ({
mode,
});
/**
* Setup development server with webpack-dev-server
*
* @param {Object} [devServer] devServer options
* @return devServer config
*/
exports.setupDevServer = (devServer) => ({
devServer,
});
/**
* Split vendor dependencies from main bundle
*
* @return optimization config
*/
exports.splitVendor = () => ({
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: "vendor",
chunks: "initial",
},
},
},
},
});