forked from springload/draftail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
230 lines (206 loc) · 6.44 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const autoprefixer = require("autoprefixer");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const sass = require("sass");
require("dotenv").config();
const pkg = require("./package.json");
// Key is hard-coded because it will be public on the demo site anyway.
// Key usage is limited to whitelisted Referrers.
const EMBEDLY_API_KEY_PROD = "d23c29a928fe4d89bda46b0291914c9c";
const EMBEDLY_API_KEY = process.env.EMBEDLY_API_KEY || EMBEDLY_API_KEY_PROD;
const GOOGLE_ANALYTICS_PROD = "UA-126695868-1";
const SENTRY_DSN_PROD =
"https://[email protected]/251576";
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
const node = {
dgram: "empty",
fs: "empty",
net: "empty",
tls: "empty",
child_process: "empty",
};
const stats = {
// Add chunk information (setting this to `false` allows for a less verbose output)
chunks: false,
// Add the hash of the compilation
hash: false,
// `webpack --colors` equivalent
colors: true,
// Add information about the reasons why modules are included
reasons: false,
// Add webpack version information
version: false,
};
/**
* Base Webpack config, defining how our code should compile.
*/
const webpackConfig = (environment) => {
const isProduction = environment === "production";
const publicPath = "/";
const examplesPath = path.join(__dirname, "examples");
const icons = fs.readFileSync(
path.join(examplesPath, "constants", "icons.svg"),
"utf-8",
);
const htmlPluginConfig = {
template: path.join(examplesPath, "index.html"),
hash: true,
data: {
publicPath,
icons,
PKG_VERSION: pkg.version,
SENTRY_DSN: isProduction ? SENTRY_DSN_PROD : null,
GOOGLE_ANALYTICS: isProduction ? GOOGLE_ANALYTICS_PROD : null,
},
};
const compiler = {
// Disable Webpack mode to use our own optimisations.
mode: "none",
// See http://webpack.github.io/docs/configuration.html#devtool
devtool: "source-map",
entry: {
// Stylesheet shipped with the package.
draftail: ["./lib/index.scss"],
index: ["./examples/utils/polyfills", "./examples/index"],
examples: ["./examples/utils/polyfills", "./examples/examples"],
},
output: {
path: path.join(__dirname, "public"),
filename: "[name].bundle.js",
publicPath,
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new HtmlWebpackPlugin(
Object.assign({}, htmlPluginConfig, {
filename: "index.html",
chunks: ["vendor", "index"],
}),
),
new HtmlWebpackPlugin(
Object.assign({}, htmlPluginConfig, {
filename: "examples/index.html",
chunks: ["vendor", "examples"],
}),
),
new BundleAnalyzerPlugin({
// Can be `server`, `static` or `disabled`.
analyzerMode: "static",
// Path to bundle report file that will be generated in `static` mode.
reportFilename: path.join(__dirname, "public", "webpack-stats.html"),
// Automatically open report in default browser
openAnalyzer: false,
logLevel: environment === "production" ? "info" : "warn",
}),
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
EMBEDLY_API_KEY: JSON.stringify(
isProduction ? EMBEDLY_API_KEY_PROD : EMBEDLY_API_KEY,
),
"process.env.NODE_ENV": JSON.stringify(environment),
PKG_VERSION: JSON.stringify(pkg.version),
}),
],
module: {
rules: [
{
test: /\.js$/,
use: ["babel-loader"],
exclude: /node_modules/,
},
{
test: /\.(scss|css)$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : "style-loader",
{
loader: "css-loader",
options: {
sourceMap: !isProduction,
minimize: isProduction,
},
},
{
loader: "postcss-loader",
options: {
sourceMap: !isProduction,
plugins: () => [autoprefixer()],
},
},
{
loader: "sass-loader",
options: {
sourceMap: !isProduction,
implementation: sass,
},
},
],
},
],
},
optimization: {
minimize: isProduction,
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
uglifyOptions: {
compress: {
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebookincubator/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
},
output: {
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebookincubator/create-react-app/issues/2488
ascii_only: true,
},
},
}),
],
splitChunks: {
cacheGroups: {
vendor: {
name: "vendor",
chunks: "initial",
minChunks: 2,
reuseExistingChunk: true,
},
},
},
},
// Turn off performance hints during development because we don't do any
// splitting or minification in interest of speed. These warnings become
// cumbersome.
performance: {
hints: isProduction && "warning",
},
stats,
node,
// https://webpack.js.org/configuration/dev-server/#devserver
devServer: {
contentBase: path.join(__dirname, "public"),
watchContentBase: true,
compress: true,
hot: true,
port: 4000,
overlay: true,
clientLogLevel: "none",
stats,
disableHostCheck: true,
},
};
return compiler;
};
module.exports = webpackConfig;