-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.dev.js
289 lines (284 loc) · 12.7 KB
/
webpack.config.dev.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"use strict";
const path = require("path");
const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
const WatchMissingNodeModulesPlugin = require("react-dev-utils/WatchMissingNodeModulesPlugin");
const PrettierPlugin = require("prettier-webpack-plugin");
const ResolveEntryModulesPlugin = require("resolve-entry-modules-webpack-plugin");
const atImport = require("postcss-import");
const postcssURL = require("postcss-url");
const cssNext = require("postcss-cssnext");
const getClientEnvironment = require("./env");
const paths = require("./paths");
// Webpack uses `publicPath` to determine where the app is being served from.
// In development, we serve from the full domain we’re booting the app on.
const protocol = process.env.ASSETS_HTTPS === "true" ? "https" : "http";
const host = process.env.ASSETS_HOST || "localhost";
const port = parseInt(process.env.ASSETS_PORT, 10) || 8080;
const serverBase = `${protocol}://${host}:${port}`;
const publicPath = `${serverBase}${paths.publicPath}`;
// Some apps do not use client-side routing with pushState.
// For these, "homepage" can be set to "." to enable relative asset paths.
const shouldUseRelativeAssetPaths = publicPath === "./";
// Get environment variables to inject into our app.
const env = getClientEnvironment();
// ExtractTextPlugin expects the build output to be flat.
// (See https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/27)
// However, our output is structured with css, js and media folders.
// To have this structure working with relative paths, we have to use custom options.
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split("/").length).join("../") }
: {};
// This is the development configuration.
// It is focused on developer experience and fast rebuilds.
// The production configuration is different and lives in a separate file.
module.exports = {
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
// See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
devtool: "cheap-module-source-map",
// Specify the context we expect app code to be loaded from
context: paths.appSrc,
// These are the "entry points" to our application.
// This means they will be the "root" imports that are included in JS bundle.
// We can have multiple entries and so we merge them with some common values to
// enable "hot" CSS and auto-refreshes for JS.
entry: paths.appEntries.reduce((output, entry) => {
const [name, location] = entry;
// If the entry name begins with `inline` we consider it inline
const inline = /__inline/.test(name);
// Test env?
const testEnv = process.env.ASSETS_ENV && process.env.ASSETS_ENV === "test"
// Build up the definition for each entry
let entryDefinition = [];
// Skip hot-reload and other unnecessary things for "inline" assets
if (!inline && !testEnv) {
entryDefinition = entryDefinition.concat([
// Add our custom dev niceties:
// - Supressing the output of the webpack-dev-server and hot-module-resolveLoader
// in the console. No more [WDS] and [MHR] messages.
// - Automatically reloading extracted CSS
require.resolve("./devNiceties"),
// When you save a file, the client will either apply hot updates (in case
// of CSS changes), or refresh the page (in case of JS changes). When you
// make a syntax error, this client will display a syntax error overlay.
// the line below with these two lines if you prefer the stock client:
require.resolve("webpack-dev-server/client") + `?${serverBase}`,
// Disabled until the ExtractText plugin supports HMR
// https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/592
// require.resolve("webpack/hot/dev-server")
]);
}
// Add these for all entries
entryDefinition = entryDefinition.concat([
// We ship a few polyfills by default:
require.resolve("./polyfills"),
// Finally, this is where the code lives for this entry
location
// We include the app code last so that if there is a runtime error during
// initialization, it doesn't blow up the WebpackDevServer client, and
// changing JS code would still trigger a refresh.
]);
output[name] = entryDefinition;
return output;
}, {}),
output: {
// Next line is not used in dev but WebpackDevServer crashes without it:
path: paths.appBuild,
// Add /* filename */ comments to generated require()s in the output.
pathinfo: true,
// This does not produce real files. It's just the virtual path that is
// served by WebpackDevServer in development.
filename: "[name].js",
chunkFilename: "chunk.[name].js",
// This is the URL that app is served from. We use "/" in development.
publicPath: publicPath
},
resolve: {
// This allows you to set a fallback for where Webpack should look for modules.
// We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
// We placed these paths second because we want `node_modules` to "win"
// if there are any conflicts. This matches Node resolution mechanism.
// https://github.com/facebookincubator/create-react-app/issues/253
modules: ["node_modules"]
.concat(paths.nodePaths),
// These are the reasonable defaults supported by the Node ecosystem.
// We also include JSX as a common component filename extension to support
// some tools, although we do not recommend using it, see:
// https://github.com/facebookincubator/create-react-app/issues/290
extensions: [".js", ".json", ".jsx"]
},
// Resolve loaders (webpack plugins for CSS, images, transpilation) from the
// directory of `react-scripts` itself rather than the project directory.
resolveLoader: {
modules: [paths.ownNodeModules, paths.appNodeModules]
},
module: {
rules: [
// Disable require.ensure as it's not a standard language feature.
{ parser: { requireEnsure: false } },
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|jsx)$/,
enforce: "pre",
use: [
{
// Point ESLint to our predefined config.
options: {
configFile: path.join(__dirname, "../eslintrc"),
useEslintrc: false,
// Automatically fix eslint-issues (i.e., run through prettier)
fix: true
},
loader: "eslint-loader"
}
],
include: paths.appSrc
},
// ** ADDING/UPDATING LOADERS **
// The "url" loader handles all assets unless explicitly excluded.
// The `exclude` list *must* be updated with every change to loader extensions.
// When adding a new loader, you must add its `test`
// as a new entry in the `exclude` list for "url" loader.
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
{
exclude: [/\.html$/, /\.(js|jsx)$/, /\.css$/, /\.json$/],
use: [
{
loader: "file-loader",
options: {
name: "[path][name].[ext]"
}
}
]
},
// Process JS with Babel.
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
use: [
{
loader: "babel-loader",
options: {
babelrc: true,
presets: [require.resolve("babel-preset-react-app")],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true
}
}
]
},
// The notation here is somewhat confusing.
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader normally turns CSS into JS modules injecting <style>,
// but unlike in development configuration, we do something different.
// `ExtractTextPlugin` first applies the "postcss" and "css" loaders
// (second argument), then grabs the result CSS and puts it into a
// separate file in our build process. This way we actually ship
// a single CSS file in production instead of JS code injecting <style>
// tags. If you use code splitting, however, any async bundles will still
// use the "style" loader inside the async code so CSS from them won't be
// in the main CSS file.
{
test: /\.css$/,
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
use: ExtractTextPlugin.extract(
Object.assign(
{
fallback: "style-loader",
use: [
{
loader: "css-loader",
options: {
importLoaders: 1
}
},
{
loader: "postcss-loader",
options: {
ident: "postcss", // https://webpack.js.org/guides/migrating/#complex-options
plugins: () => [
// Add module-like @import support to our CSS. This sets the context for all imports
// to be the base entry point.
atImport(),
// postcss-url "rebases" any `url()` references in CSS to their original relative
// position on the filesystem (so that postcss-import doesn't break things)
postcssURL(),
// cssnext gives us compilation of future-CSS syntax, it also includes autoprefixer
// so we don't need to add that separately.
cssNext({
browsers: [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9" // React doesn't support IE8 anyway
],
features: {
customProperties: {
warnings: false
}
},
})
]
}
}
]
},
extractTextPluginOptions
)
)
}
// ** STOP ** Are you adding a new loader?
// Remember to add the new extension(s) to the "url" loader exclusion list.
]
},
plugins: [
// Friendlier error messages
new FriendlyErrorsWebpackPlugin(),
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
new webpack.DefinePlugin(env.stringified),
// Expose each entry as a resolution point so that we can resolve from the
// root of each entry point and avoid relative requires
new ResolveEntryModulesPlugin(),
// This is necessary to emit hot updates (currently CSS only):
// Disabled until the ExtractText plugin supports HMR
// https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/592
// new webpack.HotModuleReplacementPlugin(),
// Format CSS with Prettier. JS is handled through the eslint-loader
// (so that we can mix in other eslint configuration)
new PrettierPlugin({
parser: "css",
extensions: [".css"]
}),
// Note: this won't work without ExtractTextPlugin.extract(..) in `loaders`.
new ExtractTextPlugin({
filename: "[name].css"
}),
// If you require a missing module and then `npm install` it, you still have
// to restart the development server for Webpack to discover it. This plugin
// makes the discovery automatic so you don't have to restart.
// See https://github.com/facebookincubator/create-react-app/issues/186
new WatchMissingNodeModulesPlugin(paths.appNodeModules)
],
// 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.
node: {
fs: "empty",
net: "empty",
tls: "empty"
},
// 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: false
}
};