-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.config.js
137 lines (133 loc) · 5.67 KB
/
webpack.common.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
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
const path = require("path");
const Html = require("html-webpack-plugin");
const Favicons = require("favicons-webpack-plugin");
// https://hackernoon.com/a-tale-of-webpack-4-and-how-to-finally-configure-it-in-the-right-way-4e94c8e7e5c1
const MiniCssExtract = require("mini-css-extract-plugin");
const postcssCssnext = require("postcss-cssnext");
const Visualiser = require("webpack-visualizer-plugin");
const Compression = require("compression-webpack-plugin");
const PreloadHtml = require("preload-webpack-plugin");
const Minify = require("babel-minify-webpack-plugin");
const paths = ({ outputPath }) => {
const base = process.cwd();
const webroot = path.join(base, outputPath);
const favicon = path.join(base, "icons8-socks.png");
const src = path.join(base, "src");
const appClient = path.join(src, "app-client", "app-client.mjs");
const appClientCss = path.join(src, "app-client", "app-client.pcss");
return {
webroot,
favicon,
src,
appClient,
appClientCss,
};
};
const config = ({ debug = false, paths, publicPath }) => {
return {
mode: debug ? "development" : "production",
entry: { index: [paths.appClientCss, paths.appClient] },
output: {
pathinfo: debug,
path: paths.webroot,
filename: "[name].[hash].mjs",
publicPath,
},
optimization: debug ? undefined : { splitChunks: { chunks: "all" } },
module: {
rules: [
{
test: /\.js$|\.mjs$/,
include: paths.src,
use: {
loader: "babel-loader",
options: {
cacheDirectory: debug,
plugins: ["@babel/plugin-syntax-dynamic-import"],
presets: [
[
"@babel/preset-env",
{
debug: true,
spec: true,
modules: false,
useBuiltIns: "usage",
targets: {
node: "10.3.0", // target named node exports
browsers: [
// target native async function and generators
"chrome >= 55",
"edge >= 15",
"firefox >= 53",
"safari >= 10.1",
"and_chr >= 55",
"and_ff >= 53",
"ios_saf >= 10.3",
"and_uc >= 11.8",
"samsung >= 6.2",
],
},
},
],
],
},
},
},
{
test: /\.pcss$/,
use: [
// MiniCssExtractPlugin has no HMR support https://github.com/webpack-contrib/mini-css-extract-plugin/issues/34
// debug
// ? { loader: "style-loader", options: { sourceMap: true } }
// : MiniCssExtractPlugin.loader,
// But we cannot use it in HTTPS (Firefox), so we use it anyway
MiniCssExtract.loader,
{ loader: "css-loader", options: { sourceMap: debug } },
{
loader: "postcss-loader",
options: {
sourceMap: debug ? "inline" : false, // true does not seem to work, with dev-server at least, need to try production
plugins: [postcssCssnext],
},
},
],
},
],
},
plugins: [
new Favicons({
logo: paths.favicon,
icons: {
android: false,
appleIcon: false,
appleStartup: false,
favicons: true,
firefox: false,
},
}),
new MiniCssExtract({ filename: "index.[contenthash].css" }),
new Html({
minify: debug ? false : {},
// Template is generated from SSR via ViperHtml (ejs-loader)
meta: {
viewport: "width=device-width", // https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
description: "Author: Sladi Ri, Length: 1 pages",
},
template: "index-template.ejs",
}),
].concat(
debug
? []
: [
new PreloadHtml(),
new Minify(
{ mangle: { topLevel: true } },
{ test: /\.js($|\?)|\.mjs($|\?)/i },
),
new Compression(),
new Visualiser({ filename: "statistics.html" }),
],
),
};
};
module.exports = { paths, config };