forked from firefox-devtools/devtools-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
203 lines (176 loc) · 5.83 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
require("babel-register");
const path = require("path");
const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const {
isDevelopment,
isFirefoxPanel,
getValue,
setConfig
} = require("devtools-config");
const NODE_ENV = process.env.NODE_ENV || "development";
const TARGET = process.env.TARGET || "local";
module.exports = (webpackConfig, envConfig, options = {}) => {
setConfig(envConfig);
webpackConfig.context = path.resolve(__dirname, "src");
webpackConfig.devtool = "source-map";
webpackConfig.module = webpackConfig.module || {};
webpackConfig.module.rules = webpackConfig.module.rules || [];
webpackConfig.module.rules.push({
test: /\.json$/,
loader: "json-loader"
});
webpackConfig.module.rules.push({
test: /\.js$/,
exclude: request => {
// Some paths are excluded from Babel
let excludedPaths = ["fs", "node_modules"];
let excludedRe = new RegExp(`(${excludedPaths.join("|")})`);
let excluded = !!request.match(excludedRe);
if (options && options.babelExcludes) {
// If the tool defines an additional exclude regexp for Babel.
excluded = excluded || !!request.match(options.babelExcludes);
}
return excluded && !request.match(/node_modules(\/|\\)devtools-/);
},
loader: `babel-loader?ignore=src/lib`
});
webpackConfig.module.rules.push({
test: /\.properties$/,
loader: "raw-loader"
});
webpackConfig.node = { fs: "empty" };
webpackConfig.plugins = webpackConfig.plugins || [];
webpackConfig.plugins.push(
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(NODE_ENV),
TARGET: JSON.stringify(TARGET)
},
DebuggerConfig: JSON.stringify(envConfig)
})
);
if (isDevelopment()) {
/*
* SVGs are loaded in one of two ways in JS w/ SVG inline loader
* and in CSS w/ the CSS loader.
*
* Inline SVGs are included in the JS bundle and mounted w/ React.
*
* SVG URLs like chrome://devtools/skin/images/arrow.svg are mapped
* by the postcss-loader to /mc/devtools/client/themes/arrow.svg
* and are hosted in `development-server` with an express server.
*
* CSS URLs like resource://devtools/client/themes/variables.css are mapped by the
* NormalModuleReplacementPlugin to
* devtools-mc-assets/assets/devtools/client/themes/arrow.svg.
* The modules are then resolved by the css-loader, which allows modules like
* variables.css to be bundled.
*
* We use several PostCSS plugins to make local development a little easier:
* autoprefixer, bidirection. These plugins help support chrome + firefox
* development w/ new CSS features like RTL, mask, ...
*/
webpackConfig.module.rules.push({
test: /svg$/,
loader: "svg-inline-loader"
});
const cssUses = [{
loader: "style-loader"
}, {
loader: "css-loader",
options: {
importLoaders: 1,
url: false
}
}];
if (!options.disablePostCSS) {
cssUses.push({ loader: "postcss-loader" });
}
webpackConfig.module.rules.push({
test: /\.css$/,
use: cssUses
});
webpackConfig.plugins.push(
new webpack.NormalModuleReplacementPlugin(
/(resource:|chrome:).*.css/,
function (resource) {
const newUrl = resource.request
.replace(
/(.\/chrome:\/\/|.\/resource:\/\/)/,
`devtools-mc-assets/assets/`
)
.replace(/devtools\/skin/, "devtools/client/themes")
.replace(/devtools\/content/, "devtools/client");
resource.request = newUrl;
}
)
);
} else {
/*
* SVGs are loaded in one of two ways in JS w/ SVG inline loader
* and in CSS w/ the CSS loader.
*
* Inline SVGs are included in the JS bundle and mounted w/ React.
*
* SVG URLs like /images/arrow.svg are mapped
* by the postcss-loader to chrome://chrome://devtools/skin/images/debugger/arrow.svg,
* copied to devtools/themes/images/debugger and added to the jar.mn
*
* SVG URLS like chrome://devtools/skin/images/add.svg are
* ignored as they are already available in MC.
*/
const cssUses = [{
loader: "css-loader",
options: {
importLoaders: 1,
url: false
}
}];
if (!options.disablePostCSS) {
cssUses.push({ loader: "postcss-loader" });
}
// Extract CSS into a single file
webpackConfig.module.rules.push({
test: /\.css$/,
exclude: request => {
// If the tool defines an exclude regexp for CSS files.
return (
webpackConfig.cssExcludes && request.match(webpackConfig.cssExcludes)
);
},
use: ExtractTextPlugin.extract({
filename: "*.css",
use: cssUses
})
});
webpackConfig.module.rules.push({
test: /svg$/,
loader: "svg-inline-loader"
});
webpackConfig.plugins.push(new ExtractTextPlugin("[name].css"));
}
if (isFirefoxPanel()) {
webpackConfig = require("./webpack.config.devtools")(
webpackConfig,
envConfig,
options
);
}
// NOTE: This is only needed to fix a bug with chrome devtools' debugger and
// destructuring params https://github.com/devtools-html/debugger.html/issues/67
if (getValue("transformParameters")) {
webpackConfig.module.rules.forEach(spec => {
if (spec.isJavaScriptLoader) {
const idx = spec.rules.findIndex(loader =>
loader.includes("babel-loader")
);
spec.rules[idx] += "&plugins[]=transform-es2015-parameters";
}
});
}
return webpackConfig;
};