-
Notifications
You must be signed in to change notification settings - Fork 416
/
webpack.config.js
250 lines (242 loc) · 8.5 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) 2018-present, GM Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found in the LICENSE file in the root directory of this source tree.
// You may not use this file except in compliance with the License.
const rehypePrism = require("@mapbox/rehype-prism");
const CaseSensitivePathsPlugin = require("case-sensitive-paths-webpack-plugin");
const { spawnSync } = require("child_process");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin");
const jsonImporter = require("node-sass-json-importer");
const path = require("path");
const retext = require("retext");
const retextSmartypants = require("retext-smartypants");
const TerserPlugin = require("terser-webpack-plugin");
const visit = require("unist-util-visit");
const webpack = require("webpack");
const STATIC_WEBVIZ = process.env.STATIC_WEBVIZ === "true";
const WEBVIZ_DEV = process.env.WEBVIZ_DEV === "true";
// Enable smart quotes:
// https://github.com/mdx-js/mdx/blob/ad58be384c07672dc415b3d9d9f45dcebbfd2eb8/docs/advanced/retext-plugins.md
const smartypantsProcessor = retext().use(retextSmartypants);
function remarkSmartypants() {
function transformer(tree) {
visit(tree, "text", (node) => {
node.value = String(smartypantsProcessor.processSync(node.value));
});
}
return transformer;
}
const config = {
ravenUrl: undefined,
currentVersion: "0.0.1",
minimumChromeVersion: parseInt(process.env.MINIMUM_CHROME_VERSION) || 68,
};
const gitInfo = (() => {
const revParse = spawnSync("git", ["rev-parse", "--short", "HEAD"], { timeout: 5000 });
const diffIndex = spawnSync("git", ["diff-index", "--quiet", "HEAD", "--"], { timeout: 5000 });
if (revParse.error) {
console.log("Error getting git commit hash:", revParse.error); // eslint-disable-line no-console
return undefined;
}
if (diffIndex.error) {
console.log("Error getting git status:", diffIndex.error); // eslint-disable-line no-console
}
return {
hash: revParse.status === 0 && revParse.stdout.toString().trim(),
dirty: diffIndex.status && diffIndex.status !== 0,
};
})();
module.exports = {
devtool: "cheap-module-eval-source-map",
entry:
STATIC_WEBVIZ || WEBVIZ_DEV
? {
webvizCoreBundle: "./packages/webviz-core/src/index.js",
}
: {
docs: "./docs/src/index.js",
webvizCoreBundle: "./packages/webviz-core/src/index.js",
},
output: {
path: WEBVIZ_DEV
? path.resolve(`${__dirname}/dist`)
: STATIC_WEBVIZ
? path.resolve(`${__dirname}/__static_webviz__`)
: path.resolve(`${__dirname}/docs/public/dist`),
publicPath: STATIC_WEBVIZ || WEBVIZ_DEV ? "" : "/dist/",
pathinfo: true,
filename: "[name].js",
devtoolModuleFilenameTemplate: (info) => path.resolve(info.absoluteResourcePath),
},
resolve: {
modules: [path.resolve("."), path.resolve(`${__dirname}/packages`), "node_modules"],
extensions: [".js"],
// Doesn't work properly with linked packages, see
// https://webpack.js.org/configuration/resolve/#resolve-symlinks
// and https://github.com/webpack/webpack/issues/1866
symlinks: false,
alias: {
// The Buffer bundled by webpack copies data when doing Buffer.from(sharedArrayBuffer).
// Force use of the version in webviz-core/node_modules.
buffer$: path.resolve(`${__dirname}/packages/webviz-core/node_modules/buffer`),
},
},
module: {
strictExportPresence: true,
rules: [
{
test: /\.wasm$/,
// Bypass webpack's default importing logic for .wasm files.
// https://webpack.js.org/configuration/module/#ruletype
type: "javascript/auto",
use: {
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
},
},
},
{
test: /\.worker\.js$/,
exclude: /node_modules/,
use: {
loader: "worker-loader",
options: { name: "[name].[ext]?[hash]" },
},
},
{
test: /\.js$/,
exclude: /node_modules/,
use: { loader: "babel-loader?cacheDirectory" },
},
{
test: /\.mdx$/,
use: [
"babel-loader?cacheDirectory",
{
loader: "@mdx-js/loader",
options: {
hastPlugins: [rehypePrism],
mdPlugins: [remarkSmartypants],
},
},
],
},
{
// We use stringified Typescript in Node Playground.
// eslint-disable-next-line no-useless-escape
test: /typescript\/[\.\/\w]*\.ts$/,
exclude: /node_modules/,
use: { loader: "raw-loader" },
},
{ test: /\.md$/, loader: "raw-loader" },
{ test: /\.svg$/, loader: "react-svg-loader" },
{ test: /\.ne$/, loader: "nearley-loader" },
{ test: /\.(png|jpg|gif)$/i, use: [{ loader: "url-loader", options: { limit: 8192 } }] },
{ test: /\.s?css$/, loader: "style-loader", options: { singleton: true } },
{
test: /\.s?css$/,
oneOf: [
{
test: /\.module\./,
loader: "css-loader",
options: { localIdentName: "[path][name]-[sha512:hash:base32:5]--[local]", modules: true, sourceMap: true },
},
{ loader: "css-loader", options: { sourceMap: true } },
],
},
{
test: /\.s?css$/,
loader: "postcss-loader",
options: {
ident: "postcss",
sourceMap: true,
plugins: () => [
require("postcss-flexbugs-fixes"),
require("autoprefixer")({
browsers: [">1%", "last 4 versions", "Firefox ESR", "not ie < 9"],
flexbox: "no-2009",
}),
],
},
},
{
test: /\.scss$/,
loader: "sass-loader",
options: { sourceMap: true, sassOptions: { importer: jsonImporter() } },
},
{ test: /\.woff2?$/, loader: "url-loader" },
{ test: /\.(glb|bag|ttf|bin)$/, loader: "file-loader" },
{
test: /node_modules\/compressjs\/.*\.js/,
loader: "string-replace-loader",
options: {
search: "if (typeof define !== 'function') { var define = require('amdefine')(module); }",
replace: "/* webviz: removed broken amdefine shim (https://github.com/webpack/webpack/issues/5316) */",
},
},
],
},
optimization: {
// use Terser instead of Uglify for async iteration (`for await`) support
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false, // make error stack traces easier to read in production
},
}),
],
},
plugins: [
new webpack.DefinePlugin({
RAVEN_URL: JSON.stringify(config.ravenUrl),
GIT_INFO: JSON.stringify(gitInfo),
CURRENT_VERSION: JSON.stringify(config.currentVersion),
MINIMUM_CHROME_VERSION: JSON.stringify(config.minimumChromeVersion),
}),
new CaseSensitivePathsPlugin(),
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new MonacoWebpackPlugin({
// available options: https://github.com/Microsoft/monaco-editor-webpack-plugin#options
languages: ["typescript", "javascript"],
}),
],
node: {
fs: "empty",
// Originally put in due to the 'source-map-support' dependency pulled in from TypeScript.
module: "empty",
__filename: true,
},
performance: { hints: false },
devServer: {
contentBase: WEBVIZ_DEV ? path.resolve(`${__dirname}/dist`) : path.resolve(`${__dirname}/docs/public`),
hot: true,
open: true,
},
};
if (WEBVIZ_DEV) {
module.exports.plugins.push(
new HtmlWebpackPlugin({
template: path.resolve(`${__dirname}/packages/webviz-core/public/index.html`),
})
);
}
if (process.env.NODE_ENV === "production") {
module.exports.mode = "production";
module.exports.devtool = "source-map";
} else {
if (STATIC_WEBVIZ) {
throw new Error("If STATIC_WEBVIZ=true is set the NODE_ENV=production must be set!");
}
module.exports.mode = "development";
if (WEBVIZ_DEV) {
module.exports.entry.webvizCoreBundle = [module.exports.entry.webvizCoreBundle, "webpack-hot-middleware/client"];
} else {
module.exports.entry.docs = [module.exports.entry.docs, "webpack-hot-middleware/client"];
}
module.exports.plugins.push(new webpack.HotModuleReplacementPlugin());
module.exports.output.globalObject = "this"; // Workaround for https://github.com/webpack/webpack/issues/6642#issuecomment-370222543
}