-
Notifications
You must be signed in to change notification settings - Fork 95
/
webpack.common.js
145 lines (137 loc) · 3.34 KB
/
webpack.common.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
/* eslint import/no-extraneous-dependencies: off */
const path = require("path");
const url = require("url");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");
const nodeEnv = process.env.NODE_ENV || "production";
const siteHost = process.env.HOST || "localhost";
const sitePort = process.env.PORT || "8080";
const siteUrl = process.env.SITE_URL || `http://${siteHost}:${sitePort}/`;
const siteId = process.env.SITE_ID || "";
const downloadFirefoxUtmSource =
process.env.DOWNLOAD_FIREFOX_UTM_SOURCE || new url.URL(siteUrl).hostname;
const isDev = nodeEnv === "development";
const defaultEnv = {
NODE_ENV: nodeEnv,
ADDON_URL: "https://addons.mozilla.org/firefox/addon/firefox-color/",
SITE_URL: siteUrl,
DOWNLOAD_FIREFOX_UTM_SOURCE: downloadFirefoxUtmSource,
LOADER_DELAY_PERIOD: "2000"
};
const processEnv = {};
Object.keys(defaultEnv).forEach(key => {
processEnv[key] = JSON.stringify(process.env[key] || defaultEnv[key]);
});
const commonBabelOptions = {
cacheDirectory: true,
presets: [
[
"@babel/preset-env",
{
targets: ["last 2 versions"],
modules: false
}
],
"@babel/preset-react"
],
plugins: [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties"
]
};
const webpackConfig = {
devtool: "source-map",
mode: isDev ? "development" : "production",
watchOptions: {
aggregateTimeout: 500,
poll: 1000
},
resolve: {
extensions: [".js", ".jsx"]
},
optimization: {
splitChunks: {
automaticNameDelimiter: "/",
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
priority: 0
}
}
}
},
plugins: [
new MiniCssExtractPlugin(),
new WriteFilePlugin(),
new webpack.DefinePlugin({ "process.env": processEnv })
],
module: {
rules: [
{
test: /\.js$/,
oneOf: [
{
exclude: /node_modules/,
loader: "babel-loader",
options: commonBabelOptions
},
{
include: [path.resolve(__dirname, "node_modules/query-string")],
loader: "babel-loader",
options: commonBabelOptions
}
]
},
{
test: /\.scss$/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"]
},
{
test: /\.(ttf|woff|eot)$/i,
use: [
{
loader: "file-loader",
options: {
hash: "sha512",
digest: "hex",
name: "fonts/[name]-[hash].[ext]"
}
}
]
},
{
test: /\.(jpe?g|gif|png|svg)$/i,
use: [
{
loader: "file-loader",
options: {
hash: "sha512",
digest: "hex",
name: "images/[name]-[hash].[ext]"
}
},
{
loader: "image-webpack-loader",
options: {
bypassOnDebug: true
}
}
]
}
]
}
};
module.exports = {
siteHost,
sitePort,
siteUrl,
siteId,
nodeEnv,
webpackConfig
};