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