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