-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (68 loc) · 2.18 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
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const wpmerge = require("webpack-merge").smart;
const CleanPlugin = require("clean-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlPlugin = require("html-webpack-plugin");
const ExternalsPlugin = require("html-webpack-externals-plugin");
const { HOT, NODE_ENV, PORT = 8080, HOST = "localhost" } = process.env;
const NODE_MODULES = path.resolve(__dirname, "..");
const DEV = NODE_ENV === "development";
const plugins = [];
for (let i = 0, files = fs.readdirSync(NODE_MODULES); files[i]; i++) {
if (files[i].match(/^wprun-config-(\w+[-]?)+/g)) {
plugins.push(require(path.resolve(NODE_MODULES, files[i])));
}
}
module.exports = (root, config = {}) => {
const {
copyPluginOptions = [],
externalsPluginOptions = [],
htmlPluginOptions = {},
...options
} = config;
const defaults = {
entry: "./index.js",
devtool: DEV ? "inline-cheap-source-map" : undefined,
context: path.join(root, "src"),
resolve: {
modules: [path.resolve(root, "./src"), "node_modules"],
},
plugins: [
new CleanPlugin(["dist"], { root, verbose: true }),
new HtmlPlugin({
template: path.resolve(__dirname, "public/index.ejs"),
minify: {
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
minifyCSS: true,
},
...htmlPluginOptions,
}),
new CopyPlugin([
{ from: "*.css", to: "./", context: path.resolve(__dirname, "public") },
...copyPluginOptions,
]),
new ExternalsPlugin({
cwpOptions: { context: path.resolve(root, "node_modules") },
...externalsPluginOptions,
}),
],
devServer: {
compress: true,
overlay: true,
open: true,
host: HOST,
port: PORT,
hot: HOT === "true",
publicPath: `http://localhost:${PORT}/`,
stats: { chunks: false, colors: true, reasons: false },
},
};
if (HOT === "true") {
defaults.plugins.push(new webpack.HotModuleReplacementPlugin());
}
return plugins.concat(options).reduce((obj, cfg = {}) => wpmerge(obj, cfg), defaults);
};