-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
201 lines (191 loc) · 5.39 KB
/
webpack.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import fs from "fs";
import {join} from "path";
import url from "url";
import dotenv from "dotenv";
import dotenvExpand from "dotenv-expand";
import HtmlWebPackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import ManifestPlugin from "webpack-manifest-plugin";
import CopyPlugin from "webpack-copy-plugin";
import escapeStringRegexp from "escape-string-regexp";
import {DefinePlugin} from "webpack";
import {homepage} from "./package.json";
class InterpolateHtmlPlugin {
constructor(replacements) {
this.replacements = replacements;
}
apply(compiler) {
compiler.hooks.compilation.tap("InterpolateHtmlPlugin", (compilation) => {
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tap(
"InterpolateHtmlPlugin",
(data) => {
// Run HTML through a series of user-specified string replacements.
Object.keys(this.replacements).forEach((key) => {
const value = this.replacements[key];
// eslint-disable-next-line no-param-reassign
data.html = data.html.replace(
new RegExp(`%${escapeStringRegexp(key)}%`, "g"),
value,
);
});
},
);
});
}
}
const ensureSlash = (path) => {
const hasSlash = path.endsWith("/");
if (hasSlash) return path;
return `${path}/`;
};
const {NODE_ENV} = process.env;
if (!NODE_ENV) {
throw new Error(
"The NODE_ENV environment variable is required but was not specified.",
);
}
const dotenvFiles = [
`.env.${NODE_ENV}.local`,
`.env.${NODE_ENV}`,
NODE_ENV !== "test" && ".env.local",
".env",
].filter(Boolean);
dotenvFiles.forEach((dotenvFile) => {
if (fs.existsSync(dotenvFile)) {
dotenvExpand(
dotenv.config({
path: dotenvFile,
}),
);
}
});
const servedUrl = ensureSlash(
process.env.PUBLIC_URL || (homepage ? url.parse(homepage).pathname : "/"),
);
const getClientEnvironment = (publicUrl) => {
const REACT_APP = /^REACT_APP_/i;
const raw = Object.keys(process.env)
.filter((key) => REACT_APP.test(key))
.reduce((env, key) => Object.assign({}, env, {[key]: process.env[key]}), {
// Useful for determining whether we’re running in production mode.
// Most importantly, it switches React into the correct mode.
NODE_ENV: process.env.NODE_ENV || "development",
// Useful for resolving the correct path to static assets in `public`.
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
// This should only be used as an escape hatch. Normally you would put
// images into the `src` and `import` them in code to get their paths.
PUBLIC_URL: publicUrl,
});
// Stringify all values so we can feed into Webpack DefinePlugin
const stringified = {
"process.env": Object.keys(raw).reduce(
(env, key) => Object.assign({}, env, {[key]: JSON.stringify(raw[key])}),
{},
),
};
return {raw, stringified};
};
const appEnv = getClientEnvironment(servedUrl);
module.exports = {
bail: true,
entry: ["@babel/plugin-transform-runtime", "./src/client.js"],
devtool: "source-map",
resolve: {
extensions: [".js", ".jsx"],
},
output: {
publicPath: "/",
filename: "static/js/[name].[hash:8].js",
chunkFilename: "static/js/[name].[chunkhash:8].chunk.js",
},
devServer: {
contentBase: join(__dirname, "dist"),
compress: true,
port: 9000,
historyApiFallback: true,
},
module: {
noParse: [/dtrace-provider/, /source-map-support/],
rules: [
{
oneOf: [
{
test: /\.bmp$|\.gif$|\.jpe?g$|\.png$|\.svg$/,
loader: "url-loader",
options: {
limit: 10000,
name: "static/media/[name].[hash:8].[ext]",
},
},
{
test: /\.(js|jsx|mjs)$/,
exclude: /node_modules/,
loader: "babel-loader",
options: {
babelrc: true,
},
},
],
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: {minimize: true},
},
],
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader?url=false", // Append url=false to make leaflet render correctly.
"postcss-loader",
],
},
{
loader: "file-loader?name=[name].[ext]",
test: /\.ico$|\.woff$|\.ttf$|\.wav$|\.mp3$/,
},
{
test: /\.md$/,
use: "raw-loader",
},
],
},
plugins: [
new CopyPlugin({
dirs: [
{from: "./public", to: "./dist"},
// This was the only way to make the leaflet marker icons load.
{from: "./public/images", to: "./dist/static/css/images"},
],
options: {},
}),
new HtmlWebPackPlugin({
inject: true,
cache: true,
template: "./public/index.html",
filename: "./index.html",
}),
new InterpolateHtmlPlugin(appEnv.raw),
new DefinePlugin(appEnv.stringified),
new MiniCssExtractPlugin({
filename: "static/css/[name].[hash:8].css",
chunkFilename: "static/css/[name].[chunkhash:8].css",
}),
new ManifestPlugin({
fileName: "asset-manifest.json",
}),
],
externals: {
react: "React",
"react-dom": "ReactDOM",
leaflet: "L",
"chart.js": "Chart",
},
node: {
fs: "empty",
},
};