|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
const autoprefixer = require('autoprefixer'); |
|
4
|
|
|
const path = require('path'); |
|
5
|
|
|
const webpack = require('webpack'); |
|
6
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin'); |
|
7
|
|
|
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin'); |
|
8
|
|
|
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); |
|
9
|
|
|
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin'); |
|
10
|
|
|
const eslintFormatter = require('react-dev-utils/eslintFormatter'); |
|
11
|
|
|
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'); |
|
12
|
|
|
const getClientEnvironment = require('./env'); |
|
13
|
|
|
const paths = require('./paths'); |
|
14
|
|
|
|
|
15
|
|
|
// Webpack uses `publicPath` to determine where the app is being served from. |
|
16
|
|
|
// In development, we always serve from the root. This makes config easier. |
|
17
|
|
|
const publicPath = '/'; |
|
18
|
|
|
// `publicUrl` is just like `publicPath`, but we will provide it to our app |
|
19
|
|
|
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript. |
|
20
|
|
|
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz. |
|
21
|
|
|
const publicUrl = ''; |
|
22
|
|
|
// Get environment variables to inject into our app. |
|
23
|
|
|
const env = getClientEnvironment(publicUrl); |
|
24
|
|
|
|
|
25
|
|
|
// This is the development configuration. |
|
26
|
|
|
// It is focused on developer experience and fast rebuilds. |
|
27
|
|
|
// The production configuration is different and lives in a separate file. |
|
28
|
|
|
module.exports = { |
|
29
|
|
|
// You may want 'eval' instead if you prefer to see the compiled output in DevTools. |
|
30
|
|
|
// See the discussion in https://github.com/facebookincubator/create-react-app/issues/343. |
|
31
|
|
|
devtool: 'cheap-module-source-map', |
|
32
|
|
|
// These are the "entry points" to our application. |
|
33
|
|
|
// This means they will be the "root" imports that are included in JS bundle. |
|
34
|
|
|
// The first two entry points enable "hot" CSS and auto-refreshes for JS. |
|
35
|
|
|
entry: [ |
|
36
|
|
|
// Include an alternative client for WebpackDevServer. A client's job is to |
|
37
|
|
|
// connect to WebpackDevServer by a socket and get notified about changes. |
|
38
|
|
|
// When you save a file, the client will either apply hot updates (in case |
|
39
|
|
|
// of CSS changes), or refresh the page (in case of JS changes). When you |
|
40
|
|
|
// make a syntax error, this client will display a syntax error overlay. |
|
41
|
|
|
// Note: instead of the default WebpackDevServer client, we use a custom one |
|
42
|
|
|
// to bring better experience for Create React App users. You can replace |
|
43
|
|
|
// the line below with these two lines if you prefer the stock client: |
|
44
|
|
|
// require.resolve('webpack-dev-server/client') + '?/', |
|
45
|
|
|
// require.resolve('webpack/hot/dev-server'), |
|
46
|
|
|
require.resolve('react-dev-utils/webpackHotDevClient'), |
|
47
|
|
|
// We ship a few polyfills by default: |
|
48
|
|
|
require.resolve('./polyfills'), |
|
49
|
|
|
// Errors should be considered fatal in development |
|
50
|
|
|
require.resolve('react-error-overlay'), |
|
51
|
|
|
// Finally, this is your app's code: |
|
52
|
|
|
paths.appIndexJs, |
|
53
|
|
|
// We include the app code last so that if there is a runtime error during |
|
54
|
|
|
// initialization, it doesn't blow up the WebpackDevServer client, and |
|
55
|
|
|
// changing JS code would still trigger a refresh. |
|
56
|
|
|
], |
|
57
|
|
|
output: { |
|
58
|
|
|
// Next line is not used in dev but WebpackDevServer crashes without it: |
|
59
|
|
|
path: paths.appBuild, |
|
60
|
|
|
// Add /* filename */ comments to generated require()s in the output. |
|
61
|
|
|
pathinfo: true, |
|
62
|
|
|
// This does not produce a real file. It's just the virtual path that is |
|
63
|
|
|
// served by WebpackDevServer in development. This is the JS bundle |
|
64
|
|
|
// containing code from all our entry points, and the Webpack runtime. |
|
65
|
|
|
filename: 'static/js/bundle.js', |
|
66
|
|
|
// There are also additional JS chunk files if you use code splitting. |
|
67
|
|
|
chunkFilename: 'static/js/[name].chunk.js', |
|
68
|
|
|
// This is the URL that app is served from. We use "/" in development. |
|
69
|
|
|
publicPath: publicPath, |
|
70
|
|
|
// Point sourcemap entries to original disk location (format as URL on Windows) |
|
71
|
|
|
devtoolModuleFilenameTemplate: info => |
|
72
|
|
|
path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'), |
|
73
|
|
|
}, |
|
74
|
|
|
resolve: { |
|
75
|
|
|
// This allows you to set a fallback for where Webpack should look for modules. |
|
76
|
|
|
// We placed these paths second because we want `node_modules` to "win" |
|
77
|
|
|
// if there are any conflicts. This matches Node resolution mechanism. |
|
78
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/253 |
|
79
|
|
|
modules: ['node_modules', paths.appNodeModules].concat( |
|
80
|
|
|
// It is guaranteed to exist because we tweak it in `env.js` |
|
81
|
|
|
process.env.NODE_PATH.split(path.delimiter).filter(Boolean) |
|
82
|
|
|
), |
|
83
|
|
|
// These are the reasonable defaults supported by the Node ecosystem. |
|
84
|
|
|
// We also include JSX as a common component filename extension to support |
|
85
|
|
|
// some tools, although we do not recommend using it, see: |
|
86
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/290 |
|
87
|
|
|
// `web` extension prefixes have been added for better support |
|
88
|
|
|
// for React Native Web. |
|
89
|
|
|
extensions: ['.web.js', '.js', '.json', '.web.jsx', '.jsx'], |
|
90
|
|
|
alias: { |
|
91
|
|
|
|
|
92
|
|
|
// Support React Native Web |
|
93
|
|
|
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/ |
|
94
|
|
|
'react-native': 'react-native-web', |
|
95
|
|
|
}, |
|
96
|
|
|
plugins: [ |
|
97
|
|
|
// Prevents users from importing files from outside of src/ (or node_modules/). |
|
98
|
|
|
// This often causes confusion because we only process files within src/ with babel. |
|
99
|
|
|
// To fix this, we prevent you from importing files out of src/ -- if you'd like to, |
|
100
|
|
|
// please link the files into your node_modules/ and let module-resolution kick in. |
|
101
|
|
|
// Make sure your source files are compiled, as they will not be processed in any way. |
|
102
|
|
|
new ModuleScopePlugin(paths.appSrc), |
|
103
|
|
|
], |
|
104
|
|
|
}, |
|
105
|
|
|
module: { |
|
106
|
|
|
strictExportPresence: true, |
|
107
|
|
|
rules: [ |
|
108
|
|
|
// TODO: Disable require.ensure as it's not a standard language feature. |
|
109
|
|
|
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176. |
|
110
|
|
|
// { parser: { requireEnsure: false } }, |
|
111
|
|
|
|
|
112
|
|
|
// First, run the linter. |
|
113
|
|
|
// It's important to do this before Babel processes the JS. |
|
114
|
|
|
{ |
|
115
|
|
|
test: /\.(js|jsx)$/, |
|
116
|
|
|
enforce: 'pre', |
|
117
|
|
|
use: [ |
|
118
|
|
|
{ |
|
119
|
|
|
options: { |
|
120
|
|
|
formatter: eslintFormatter, |
|
121
|
|
|
|
|
122
|
|
|
}, |
|
123
|
|
|
loader: require.resolve('eslint-loader'), |
|
124
|
|
|
}, |
|
125
|
|
|
], |
|
126
|
|
|
include: paths.appSrc, |
|
127
|
|
|
}, |
|
128
|
|
|
// ** ADDING/UPDATING LOADERS ** |
|
129
|
|
|
// The "file" loader handles all assets unless explicitly excluded. |
|
130
|
|
|
// The `exclude` list *must* be updated with every change to loader extensions. |
|
131
|
|
|
// When adding a new loader, you must add its `test` |
|
132
|
|
|
// as a new entry in the `exclude` list for "file" loader. |
|
133
|
|
|
|
|
134
|
|
|
// "file" loader makes sure those assets get served by WebpackDevServer. |
|
135
|
|
|
// When you `import` an asset, you get its (virtual) filename. |
|
136
|
|
|
// In production, they would get copied to the `build` folder. |
|
137
|
|
|
{ |
|
138
|
|
|
exclude: [ |
|
139
|
|
|
/\.html$/, |
|
140
|
|
|
/\.(js|jsx)$/, |
|
141
|
|
|
/\.css$/, |
|
142
|
|
|
/\.json$/, |
|
143
|
|
|
/\.bmp$/, |
|
144
|
|
|
/\.gif$/, |
|
145
|
|
|
/\.jpe?g$/, |
|
146
|
|
|
/\.png$/, |
|
147
|
|
|
], |
|
148
|
|
|
loader: require.resolve('file-loader'), |
|
149
|
|
|
options: { |
|
150
|
|
|
name: 'static/media/[name].[hash:8].[ext]', |
|
151
|
|
|
}, |
|
152
|
|
|
}, |
|
153
|
|
|
// "url" loader works like "file" loader except that it embeds assets |
|
154
|
|
|
// smaller than specified limit in bytes as data URLs to avoid requests. |
|
155
|
|
|
// A missing `test` is equivalent to a match. |
|
156
|
|
|
{ |
|
157
|
|
|
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/], |
|
158
|
|
|
loader: require.resolve('url-loader'), |
|
159
|
|
|
options: { |
|
160
|
|
|
limit: 10000, |
|
161
|
|
|
name: 'static/media/[name].[hash:8].[ext]', |
|
162
|
|
|
}, |
|
163
|
|
|
}, |
|
164
|
|
|
// Process JS with Babel. |
|
165
|
|
|
{ |
|
166
|
|
|
test: /\.(js|jsx)$/, |
|
167
|
|
|
include: paths.appSrc, |
|
168
|
|
|
loader: require.resolve('babel-loader'), |
|
169
|
|
|
options: { |
|
170
|
|
|
|
|
171
|
|
|
// This is a feature of `babel-loader` for webpack (not Babel itself). |
|
172
|
|
|
// It enables caching results in ./node_modules/.cache/babel-loader/ |
|
173
|
|
|
// directory for faster rebuilds. |
|
174
|
|
|
cacheDirectory: true, |
|
175
|
|
|
}, |
|
176
|
|
|
}, |
|
177
|
|
|
// "postcss" loader applies autoprefixer to our CSS. |
|
178
|
|
|
// "css" loader resolves paths in CSS and adds assets as dependencies. |
|
179
|
|
|
// "style" loader turns CSS into JS modules that inject <style> tags. |
|
180
|
|
|
// In production, we use a plugin to extract that CSS to a file, but |
|
181
|
|
|
// in development "style" loader enables hot editing of CSS. |
|
182
|
|
|
{ |
|
183
|
|
|
test: /\.css$/, |
|
184
|
|
|
use: [ |
|
185
|
|
|
require.resolve('style-loader'), |
|
186
|
|
|
{ |
|
187
|
|
|
loader: require.resolve('css-loader'), |
|
188
|
|
|
options: { |
|
189
|
|
|
importLoaders: 1, |
|
190
|
|
|
}, |
|
191
|
|
|
}, |
|
192
|
|
|
{ |
|
193
|
|
|
loader: require.resolve('postcss-loader'), |
|
194
|
|
|
options: { |
|
195
|
|
|
// Necessary for external CSS imports to work |
|
196
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/2677 |
|
197
|
|
|
ident: 'postcss', |
|
198
|
|
|
plugins: () => [ |
|
199
|
|
|
require('postcss-flexbugs-fixes'), |
|
200
|
|
|
autoprefixer({ |
|
201
|
|
|
browsers: [ |
|
202
|
|
|
'>1%', |
|
203
|
|
|
'last 4 versions', |
|
204
|
|
|
'Firefox ESR', |
|
205
|
|
|
'not ie < 9', // React doesn't support IE8 anyway |
|
206
|
|
|
], |
|
207
|
|
|
flexbox: 'no-2009', |
|
208
|
|
|
}), |
|
209
|
|
|
], |
|
210
|
|
|
}, |
|
211
|
|
|
}, |
|
212
|
|
|
], |
|
213
|
|
|
}, |
|
214
|
|
|
// ** STOP ** Are you adding a new loader? |
|
215
|
|
|
// Remember to add the new extension(s) to the "file" loader exclusion list. |
|
216
|
|
|
], |
|
217
|
|
|
}, |
|
218
|
|
|
plugins: [ |
|
219
|
|
|
// Makes some environment variables available in index.html. |
|
220
|
|
|
// The public URL is available as %PUBLIC_URL% in index.html, e.g.: |
|
221
|
|
|
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> |
|
222
|
|
|
// In development, this will be an empty string. |
|
223
|
|
|
new InterpolateHtmlPlugin(env.raw), |
|
224
|
|
|
// Generates an `index.html` file with the <script> injected. |
|
225
|
|
|
new HtmlWebpackPlugin({ |
|
226
|
|
|
inject: true, |
|
227
|
|
|
template: paths.appHtml, |
|
228
|
|
|
}), |
|
229
|
|
|
// Add module names to factory functions so they appear in browser profiler. |
|
230
|
|
|
new webpack.NamedModulesPlugin(), |
|
231
|
|
|
// Makes some environment variables available to the JS code, for example: |
|
232
|
|
|
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`. |
|
233
|
|
|
new webpack.DefinePlugin(env.stringified), |
|
234
|
|
|
// This is necessary to emit hot updates (currently CSS only): |
|
235
|
|
|
new webpack.HotModuleReplacementPlugin(), |
|
236
|
|
|
// Watcher doesn't work well if you mistype casing in a path so we use |
|
237
|
|
|
// a plugin that prints an error when you attempt to do this. |
|
238
|
|
|
// See https://github.com/facebookincubator/create-react-app/issues/240 |
|
239
|
|
|
new CaseSensitivePathsPlugin(), |
|
240
|
|
|
// If you require a missing module and then `npm install` it, you still have |
|
241
|
|
|
// to restart the development server for Webpack to discover it. This plugin |
|
242
|
|
|
// makes the discovery automatic so you don't have to restart. |
|
243
|
|
|
// See https://github.com/facebookincubator/create-react-app/issues/186 |
|
244
|
|
|
new WatchMissingNodeModulesPlugin(paths.appNodeModules), |
|
245
|
|
|
// Moment.js is an extremely popular library that bundles large locale files |
|
246
|
|
|
// by default due to how Webpack interprets its code. This is a practical |
|
247
|
|
|
// solution that requires the user to opt into importing specific locales. |
|
248
|
|
|
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack |
|
249
|
|
|
// You can remove this if you don't use Moment.js: |
|
250
|
|
|
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), |
|
251
|
|
|
], |
|
252
|
|
|
// Some libraries import Node modules but don't use them in the browser. |
|
253
|
|
|
// Tell Webpack to provide empty mocks for them so importing them works. |
|
254
|
|
|
node: { |
|
255
|
|
|
dgram: 'empty', |
|
256
|
|
|
fs: 'empty', |
|
257
|
|
|
net: 'empty', |
|
258
|
|
|
tls: 'empty', |
|
259
|
|
|
}, |
|
260
|
|
|
// Turn off performance hints during development because we don't do any |
|
261
|
|
|
// splitting or minification in interest of speed. These warnings become |
|
262
|
|
|
// cumbersome. |
|
263
|
|
|
performance: { |
|
264
|
|
|
hints: false, |
|
265
|
|
|
}, |
|
266
|
|
|
}; |
|
267
|
|
|
|