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, paths.appSrc].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 | 'SRC': paths.appSrc
|
92 | },
|
93 | plugins: [
|
94 | // Prevents users from importing files from outside of src/ (or node_modules/).
|
95 | // This often causes confusion because we only process files within src/ with babel.
|
96 | // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
|
97 | // please link the files into your node_modules/ and let module-resolution kick in.
|
98 | // Make sure your source files are compiled, as they will not be processed in any way.
|
99 | new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
|
100 | ],
|
101 | },
|
102 | module: {
|
103 | strictExportPresence: true,
|
104 | rules: [
|
105 | // TODO: Disable require.ensure as it's not a standard language feature.
|
106 | // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
|
107 | // { parser: { requireEnsure: false } },
|
108 |
|
109 | // First, run the linter.
|
110 | // It's important to do this before Babel processes the JS.
|
111 | {
|
112 | test: /\.(js|jsx|mjs)$/,
|
113 | enforce: 'pre',
|
114 | use: [
|
115 | {
|
116 | options: {
|
117 | formatter: eslintFormatter,
|
118 | eslintPath: require.resolve('eslint'),
|
119 |
|
120 | },
|
121 | loader: require.resolve('eslint-loader'),
|
122 | },
|
123 | ],
|
124 | include: paths.appSrc,
|
125 | },
|
126 | {
|
127 | // "oneOf" will traverse all following loaders until one will
|
128 | // match the requirements. When no loader matches it will fall
|
129 | // back to the "file" loader at the end of the loader list.
|
130 | oneOf: [
|
131 | // "url" loader works like "file" loader except that it embeds assets
|
132 | // smaller than specified limit in bytes as data URLs to avoid requests.
|
133 | // A missing `test` is equivalent to a match.
|
134 | {
|
135 | test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
|
136 | loader: require.resolve('url-loader'),
|
137 | options: {
|
138 | limit: 10000,
|
139 | name: 'static/media/[name].[hash:8].[ext]',
|
140 | },
|
141 | },
|
142 | // Process JS with Babel.
|
143 | {
|
144 | test: /\.(js|jsx|mjs)$/,
|
145 | include: paths.appSrc,
|
146 | loader: require.resolve('babel-loader'),
|
147 | options: {
|
148 |
|
149 | // This is a feature of `babel-loader` for webpack (not Babel itself).
|
150 | // It enables caching results in ./node_modules/.cache/babel-loader/
|
151 | // directory for faster rebuilds.
|
152 | cacheDirectory: true,
|
153 | },
|
154 | },
|
155 | // "postcss" loader applies autoprefixer to our CSS.
|
156 | // "css" loader resolves paths in CSS and adds assets as dependencies.
|
157 | // "style" loader turns CSS into JS modules that inject <style> tags.
|
158 | // In production, we use a plugin to extract that CSS to a file, but
|
159 | // in development "style" loader enables hot editing of CSS.
|
160 | {
|
161 | test: /\.css$/,
|
162 | use: [
|
163 | require.resolve('style-loader'),
|
164 | {
|
165 | loader: require.resolve('css-loader'),
|
166 | options: {
|
167 | importLoaders: 1,
|
168 | },
|
169 | },
|
170 | {
|
171 | loader: require.resolve('postcss-loader'),
|
172 | options: {
|
173 | // Necessary for external CSS imports to work
|
174 | // https://github.com/facebookincubator/create-react-app/issues/2677
|
175 | ident: 'postcss',
|
176 | plugins: () => [
|
177 | require('postcss-flexbugs-fixes'),
|
178 | autoprefixer({
|
179 | browsers: [
|
180 | '>1%',
|
181 | 'last 4 versions',
|
182 | 'Firefox ESR',
|
183 | 'not ie < 9', // React doesn't support IE8 anyway
|
184 | ],
|
185 | flexbox: 'no-2009',
|
186 | }),
|
187 | ],
|
188 | },
|
189 | },
|
190 | ],
|
191 | },
|
192 | // "file" loader makes sure those assets get served by WebpackDevServer.
|
193 | // When you `import` an asset, you get its (virtual) filename.
|
194 | // In production, they would get copied to the `build` folder.
|
195 | // This loader doesn't use a "test" so it will catch all modules
|
196 | // that fall through the other loaders.
|
197 | {
|
198 | // Exclude `js` files to keep "css" loader working as it injects
|
199 | // its runtime that would otherwise processed through "file" loader.
|
200 | // Also exclude `html` and `json` extensions so they get processed
|
201 | // by webpacks internal loaders.
|
202 | exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
|
203 | loader: require.resolve('file-loader'),
|
204 | options: {
|
205 | name: 'static/media/[name].[hash:8].[ext]',
|
206 | },
|
207 | },
|
208 | ],
|
209 | },
|
210 | // ** STOP ** Are you adding a new loader?
|
211 | // Make sure to add the new loader(s) before the "file" loader.
|
212 | ],
|
213 | },
|
214 | plugins: [
|
215 | // Makes some environment variables available in index.html.
|
216 | // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
|
217 | // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
218 | // In development, this will be an empty string.
|
219 | new InterpolateHtmlPlugin(env.raw),
|
220 | // Generates an `index.html` file with the <script> injected.
|
221 | new HtmlWebpackPlugin({
|
222 | inject: true,
|
223 | template: paths.appHtml,
|
224 | }),
|
225 | // Add module names to factory functions so they appear in browser profiler.
|
226 | new webpack.NamedModulesPlugin(),
|
227 | // Makes some environment variables available to the JS code, for example:
|
228 | // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
|
229 | new webpack.DefinePlugin(env.stringified),
|
230 | // This is necessary to emit hot updates (currently CSS only):
|
231 | new webpack.HotModuleReplacementPlugin(),
|
232 | // Watcher doesn't work well if you mistype casing in a path so we use
|
233 | // a plugin that prints an error when you attempt to do this.
|
234 | // See https://github.com/facebookincubator/create-react-app/issues/240
|
235 | new CaseSensitivePathsPlugin(),
|
236 | // If you require a missing module and then `npm install` it, you still have
|
237 | // to restart the development server for Webpack to discover it. This plugin
|
238 | // makes the discovery automatic so you don't have to restart.
|
239 | // See https://github.com/facebookincubator/create-react-app/issues/186
|
240 | new WatchMissingNodeModulesPlugin(paths.appNodeModules),
|
241 | // Moment.js is an extremely popular library that bundles large locale files
|
242 | // by default due to how Webpack interprets its code. This is a practical
|
243 | // solution that requires the user to opt into importing specific locales.
|
244 | // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
|
245 | // You can remove this if you don't use Moment.js:
|
246 | new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
247 | ],
|
248 | // Some libraries import Node modules but don't use them in the browser.
|
249 | // Tell Webpack to provide empty mocks for them so importing them works.
|
250 | node: {
|
251 | dgram: 'empty',
|
252 | fs: 'empty',
|
253 | net: 'empty',
|
254 | tls: 'empty',
|
255 | child_process: 'empty',
|
256 | },
|
257 | // Turn off performance hints during development because we don't do any
|
258 | // splitting or minification in interest of speed. These warnings become
|
259 | // cumbersome.
|
260 | performance: {
|
261 | hints: false,
|
262 | },
|
263 | };
|