UNPKG

12.4 kBJavaScriptView Raw
1'use strict';
2
3const autoprefixer = require('autoprefixer');
4const path = require('path');
5const webpack = require('webpack');
6const HtmlWebpackPlugin = require('html-webpack-plugin');
7const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
8const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
9const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
10const eslintFormatter = require('react-dev-utils/eslintFormatter');
11const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
12const getClientEnvironment = require('./env');
13const 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.
17const 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.
21const publicUrl = '';
22// Get environment variables to inject into our app.
23const 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.
28module.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 // "file" loader makes sure those assets get served by WebpackDevServer.
192 // When you `import` an asset, you get its (virtual) filename.
193 // In production, they would get copied to the `build` folder.
194 // This loader doesn't use a "test" so it will catch all modules
195 // that fall through the other loaders.
196 {
197 // Exclude `js` files to keep "css" loader working as it injects
198 // its runtime that would otherwise processed through "file" loader.
199 // Also exclude `html` and `json` extensions so they get processed
200 // by webpacks internal loaders.
201 exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
202 loader: require.resolve('file-loader'),
203 options: {
204 name: 'static/media/[name].[hash:8].[ext]',
205 },
206 },
207 ],
208 },
209 // ** STOP ** Are you adding a new loader?
210 // Make sure to add the new loader(s) before the "file" loader.
211 ],
212 },
213 plugins: [
214 // Makes some environment variables available in index.html.
215 // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
216 // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
217 // In development, this will be an empty string.
218 new InterpolateHtmlPlugin(env.raw),
219 // Generates an `index.html` file with the <script> injected.
220 new HtmlWebpackPlugin({
221 inject: true,
222 template: paths.appHtml,
223 }),
224 // Add module names to factory functions so they appear in browser profiler.
225 new webpack.NamedModulesPlugin(),
226 // Makes some environment variables available to the JS code, for example:
227 // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
228 new webpack.DefinePlugin(env.stringified),
229 // This is necessary to emit hot updates (currently CSS only):
230 new webpack.HotModuleReplacementPlugin(),
231 // Watcher doesn't work well if you mistype casing in a path so we use
232 // a plugin that prints an error when you attempt to do this.
233 // See https://github.com/facebookincubator/create-react-app/issues/240
234 new CaseSensitivePathsPlugin(),
235 // If you require a missing module and then `npm install` it, you still have
236 // to restart the development server for Webpack to discover it. This plugin
237 // makes the discovery automatic so you don't have to restart.
238 // See https://github.com/facebookincubator/create-react-app/issues/186
239 new WatchMissingNodeModulesPlugin(paths.appNodeModules),
240 // Moment.js is an extremely popular library that bundles large locale files
241 // by default due to how Webpack interprets its code. This is a practical
242 // solution that requires the user to opt into importing specific locales.
243 // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
244 // You can remove this if you don't use Moment.js:
245 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
246 ],
247 // Some libraries import Node modules but don't use them in the browser.
248 // Tell Webpack to provide empty mocks for them so importing them works.
249 node: {
250 dgram: 'empty',
251 fs: 'empty',
252 net: 'empty',
253 tls: 'empty',
254 child_process: 'empty',
255 },
256 // Turn off performance hints during development because we don't do any
257 // splitting or minification in interest of speed. These warnings become
258 // cumbersome.
259 performance: {
260 hints: false,
261 },
262};