/
webpack4.config.fn.js
111 lines (104 loc) · 3 KB
/
webpack4.config.fn.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
import ExtractCssChunksPlugin from "extract-css-chunks-webpack-plugin";
import OptimizeCSSAssetsPlugin from "optimize-css-assets-webpack-plugin";
import StatsPlugin from "webpack-stats-plugin";
import callerDependency from "../callerDependency";
import path from "path";
import webpack from "webpack";
export default function ({entrypoints, outputDir, outputUrl, hot, minify, longTermCaching, stats}) {
return {
entry: entrypoints,
output: {
path: outputDir,
// other than hot mode, the public path is set at runtime.
publicPath: hot ? outputUrl : undefined,
filename: `[name]${longTermCaching ? ".[chunkhash]" : ""}.bundle.js`,
chunkFilename: `[id]${longTermCaching ? ".[chunkhash]" : ""}.bundle.js`,
},
//eslint-disable-next-line no-process-env
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
module: {
rules: clean([
{
test: /\.(js|jsx)$/,
loader: "babel-loader",
exclude: /node_modules/,
},
{
test: /\.(eot|woff|woff2|ttf|ttc|png|svg|jpg|jpeg|gif|cgm|tiff|webp|bmp|ico)$/i,
loader: "file-loader",
exclude: /node_modules/,
},
{
test: /\.json/,
loader: "json-loader",
exclude: /node_modules/,
},
{
test: /\.less/,
loader: [
"less-loader",
],
exclude: /node_modules/,
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: ExtractCssChunksPlugin.loader,
options: {
hot: hot,
},
},
'css-loader',
"sass-loader",
],
},
{
test: /\.md/,
loader: "raw-loader",
exclude: /node_modules/,
},
]),
},
resolve: {
alias: {
// These need to be singletons.
"react": callerDependency("react"),
"react-server": callerDependency("react-server"),
},
},
resolveLoader: {
modules: [
path.resolve(path.join(path.dirname(require.resolve("webpack")), "../..")),
],
},
optimization: {
minimize: minify,
},
plugins: clean([
new ExtractCssChunksPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: `[name]${longTermCaching ? ".[contenthash]" : ""}.css`,
chunkFilename: `[id]${longTermCaching ? ".[contenthash]" : ""}.css`,
}),
stats && new StatsPlugin.StatsWriterPlugin({
fields: ["assets", "assetsByChunkName", "chunks", "errors", "warnings", "version", "hash", "time", "filteredModules", "children", "modules"],
}),
// We always like sourcemaps, so if we're minifying then put the sourcemaps in a separate file
// in the sourcemaps/ directory, otherwise inline the sourcemap with the file.
new webpack.SourceMapDevToolPlugin({
filename: minify ? 'sourcemaps/[file].map' : false,
}),
minify && new OptimizeCSSAssetsPlugin(),
hot && new webpack.HotModuleReplacementPlugin(),
]),
};
}
function clean(thing) {
if (Array.isArray(thing)) {
return thing.filter(v => Boolean(v));
}
return Object.keys(thing)
.reduce((o, k) => thing[k] == null ? o : (o[k] = thing[k], o), {});
}