1 | // @remove-on-eject-begin
|
2 | /**
|
3 | * Copyright (c) 2015-present, Facebook, Inc.
|
4 | * All rights reserved.
|
5 | *
|
6 | * This source code is licensed under the BSD-style license found in the
|
7 | * LICENSE file in the root directory of this source tree. An additional grant
|
8 | * of patent rights can be found in the PATENTS file in the same directory.
|
9 | */
|
10 | // @remove-on-eject-end
|
11 |
|
12 | var autoprefixer = require('autoprefixer');
|
13 | var webpack = require('webpack');
|
14 | var HtmlWebpackPlugin = require('html-webpack-plugin');
|
15 | var ExtractTextPlugin = require('extract-text-webpack-plugin');
|
16 | var ManifestPlugin = require('webpack-manifest-plugin');
|
17 | var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
|
18 | var url = require('url');
|
19 | var paths = require('./paths');
|
20 | var getClientEnvironment = require('./env');
|
21 | var webpackConfigHelper = require('./webpackConfigHelper');
|
22 |
|
23 | // @remove-on-eject-begin
|
24 | // `path` is not used after eject - see https://github.com/facebookincubator/create-react-app/issues/1174
|
25 | var path = require('path');
|
26 | // @remove-on-eject-end
|
27 |
|
28 | // Webpack uses `publicPath` to determine where the app is being served from.
|
29 | // It requires a trailing slash, or the file assets will get an incorrect path.
|
30 | var publicPath = paths.servedPath;
|
31 | // Some apps do not use client-side routing with pushState.
|
32 | // For these, "homepage" can be set to "." to enable relative asset paths.
|
33 | var shouldUseRelativeAssetPaths = publicPath === './';
|
34 | // `publicUrl` is just like `publicPath`, but we will provide it to our app
|
35 | // as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
|
36 | // Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
|
37 | var publicUrl = publicPath.slice(0, -1);
|
38 | // Get environment variables to inject into our app.
|
39 | var env = getClientEnvironment(publicUrl);
|
40 |
|
41 | // Assert this just to be safe.
|
42 | // Development builds of React are slow and not intended for production.
|
43 | if (env.stringified['process.env'].NODE_ENV !== '"production"') {
|
44 | throw new Error('Production builds must have NODE_ENV=production.');
|
45 | }
|
46 |
|
47 | // Note: defined here because it will be used more than once.
|
48 | const cssFilename = 'static/css/[name].[contenthash:8].css';
|
49 |
|
50 | // ExtractTextPlugin expects the build output to be flat.
|
51 | // (See https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/27)
|
52 | // However, our output is structured with css, js and media folders.
|
53 | // To have this structure working with relative paths, we have to use custom options.
|
54 | const extractTextPluginOptions = shouldUseRelativeAssetPaths
|
55 | // Making sure that the publicPath goes back to to build folder.
|
56 | ? { publicPath: Array(cssFilename.split('/').length).join('../') }
|
57 | : undefined;
|
58 |
|
59 | var entryPoints = webpackConfigHelper.buildEntryPointConfig({
|
60 | // In production, we only want to load the polyfills and the app code.
|
61 | base: [
|
62 | require.resolve('./polyfills')
|
63 | ],
|
64 | app: paths.appIndexJs
|
65 | });
|
66 |
|
67 | // This is the production configuration.
|
68 | // It compiles slowly and is focused on producing a fast and minimal bundle.
|
69 | // The development configuration is different and lives in a separate file.
|
70 | module.exports = {
|
71 | // Don't attempt to continue if there are any errors.
|
72 | bail: true,
|
73 | // We generate sourcemaps in production. This is slow but gives good results.
|
74 | // You can exclude the *.map files from the build during deployment.
|
75 | devtool: 'source-map',
|
76 | // In production, we only want to load the polyfills and the app code.
|
77 | entry: entryPoints,
|
78 | output: {
|
79 | // The build folder.
|
80 | path: paths.appBuild,
|
81 | // Generated JS file names (with nested folders).
|
82 | // There will be one main bundle, and one file per asynchronous chunk.
|
83 | // We don't currently advertise code splitting but Webpack supports it.
|
84 | filename: 'static/js/[name].[chunkhash:8].js',
|
85 | chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
|
86 | // We inferred the "public path" (such as / or /my-project) from homepage.
|
87 | publicPath: publicPath
|
88 | },
|
89 | resolve: {
|
90 | // This allows you to set a fallback for where Webpack should look for modules.
|
91 | // We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
|
92 | // We use `fallback` instead of `root` because we want `node_modules` to "win"
|
93 | // if there any conflicts. This matches Node resolution mechanism.
|
94 | // https://github.com/facebookincubator/create-react-app/issues/253
|
95 | fallback: paths.nodePaths,
|
96 | // These are the reasonable defaults supported by the Node ecosystem.
|
97 | // We also include JSX as a common component filename extension to support
|
98 | // some tools, although we do not recommend using it, see:
|
99 | // https://github.com/facebookincubator/create-react-app/issues/290
|
100 | extensions: ['.ts', '.tsx', '.js', '.json', '.jsx', ''],
|
101 | alias: {
|
102 | // Support React Native Web
|
103 | // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
|
104 | 'react-native': 'react-native-web'
|
105 | }
|
106 | },
|
107 | // @remove-on-eject-begin
|
108 | // Resolve loaders (webpack plugins for CSS, images, transpilation) from the
|
109 | // directory of `react-scripts` itself rather than the project directory.
|
110 | resolveLoader: {
|
111 | root: paths.ownNodeModules,
|
112 | moduleTemplates: ['*-loader']
|
113 | },
|
114 | // @remove-on-eject-end
|
115 | module: {
|
116 | // First, run the linter.
|
117 | // It's important to do this before Babel processes the JS.
|
118 | preLoaders: [
|
119 | {
|
120 | test: /\.(ts|tsx)$/,
|
121 | loader: 'tslint',
|
122 | include: paths.appSrc
|
123 | },
|
124 | {
|
125 | test: /\.(js|jsx)$/,
|
126 | loader: 'eslint',
|
127 | include: paths.appSrc
|
128 | }
|
129 | ],
|
130 | loaders: [
|
131 | // ** ADDING/UPDATING LOADERS **
|
132 | // The "url" loader handles all assets unless explicitly excluded.
|
133 | // The `exclude` list *must* be updated with every change to loader extensions.
|
134 | // When adding a new loader, you must add its `test`
|
135 | // as a new entry in the `exclude` list in the "url" loader.
|
136 |
|
137 | // "url" loader embeds assets smaller than specified size as data URLs to avoid requests.
|
138 | // Otherwise, it acts like the "file" loader.
|
139 | {
|
140 | exclude: [
|
141 | /\.html$/,
|
142 | /\.(ts|tsx)$/,
|
143 | /\.(js|jsx)$/,
|
144 | /\.css$/,
|
145 | /\.scss$/,
|
146 | /\.json$/,
|
147 | /\.svg$/
|
148 | ],
|
149 | loader: 'url',
|
150 | query: {
|
151 | limit: 10000,
|
152 | name: 'static/media/[name].[hash:8].[ext]'
|
153 | }
|
154 | },
|
155 | // Process TS with Typescript.
|
156 | {
|
157 | test: /\.(ts|tsx)$/,
|
158 | include: paths.appSrc,
|
159 | loader: 'ts'
|
160 | },
|
161 | // Process JS with Babel.
|
162 | {
|
163 | test: /\.(js|jsx)$/,
|
164 | include: paths.appSrc,
|
165 | loader: 'babel',
|
166 | // @remove-on-eject-begin
|
167 | query: {
|
168 | babelrc: false,
|
169 | presets: [require.resolve('babel-preset-react-app')],
|
170 | },
|
171 | // @remove-on-eject-end
|
172 | },
|
173 | // The notation here is somewhat confusing.
|
174 | // "postcss" loader applies autoprefixer to our CSS.
|
175 | // "css" loader resolves paths in CSS and adds assets as dependencies.
|
176 | // "style" loader normally turns CSS into JS modules injecting <style>,
|
177 | // but unlike in development configuration, we do something different.
|
178 | // `ExtractTextPlugin` first applies the "postcss" and "css" loaders
|
179 | // (second argument), then grabs the result CSS and puts it into a
|
180 | // separate file in our build process. This way we actually ship
|
181 | // a single CSS file in production instead of JS code injecting <style>
|
182 | // tags. If you use code splitting, however, any async bundles will still
|
183 | // use the "style" loader inside the async code so CSS from them won't be
|
184 | // in the main CSS file.
|
185 | {
|
186 | test: /\.css$/,
|
187 | loader: ExtractTextPlugin.extract(
|
188 | 'style',
|
189 | 'css?importLoaders=1!postcss',
|
190 | extractTextPluginOptions
|
191 | )
|
192 | // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
|
193 | },
|
194 | {
|
195 | test: /\.scss$/,
|
196 | loader: ExtractTextPlugin.extract(
|
197 | 'style',
|
198 | 'css?sourcepMap&importLoaders=3!postcss!resolve-url!sass?sourceMap',
|
199 | extractTextPluginOptions
|
200 | )
|
201 | },
|
202 | // JSON is not enabled by default in Webpack but both Node and Browserify
|
203 | // allow it implicitly so we also enable it.
|
204 | {
|
205 | test: /\.json$/,
|
206 | loader: 'json'
|
207 | },
|
208 | // "file" loader for svg
|
209 | {
|
210 | test: /\.svg$/,
|
211 | loader: 'file',
|
212 | query: {
|
213 | name: 'static/media/[name].[hash:8].[ext]'
|
214 | }
|
215 | }
|
216 | // ** STOP ** Are you adding a new loader?
|
217 | // Remember to add the new extension(s) to the "url" loader exclusion list.
|
218 | ]
|
219 | },
|
220 | // @remove-on-eject-begin
|
221 | // Point ESLint to our predefined config.
|
222 | eslint: {
|
223 | // TODO: consider separate config for production,
|
224 | // e.g. to enable no-console and no-debugger only in production.
|
225 | configFile: path.join(__dirname, '../.eslintrc'),
|
226 | useEslintrc: false
|
227 | },
|
228 | // @remove-on-eject-end
|
229 | // We use PostCSS for autoprefixing only.
|
230 | postcss: function() {
|
231 | return [
|
232 | autoprefixer({
|
233 | browsers: [
|
234 | '>1%',
|
235 | 'last 4 versions',
|
236 | 'Firefox ESR',
|
237 | 'not ie < 9', // React doesn't support IE8 anyway
|
238 | ]
|
239 | }),
|
240 | ];
|
241 | },
|
242 | plugins: [
|
243 | // Makes some environment variables available in index.html.
|
244 | // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
|
245 | // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
246 | // In production, it will be an empty string unless you specify "homepage"
|
247 | // in `package.json`, in which case it will be the pathname of that URL.
|
248 | new InterpolateHtmlPlugin(env.raw),
|
249 | // Makes some environment variables available to the JS code, for example:
|
250 | // if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
|
251 | // It is absolutely essential that NODE_ENV was set to production here.
|
252 | // Otherwise React will be compiled in the very slow development mode.
|
253 | new webpack.DefinePlugin(env.stringified),
|
254 | // This helps ensure the builds are consistent if source hasn't changed:
|
255 | new webpack.optimize.OccurrenceOrderPlugin(),
|
256 | // Try to dedupe duplicated modules, if any:
|
257 | new webpack.optimize.DedupePlugin(),
|
258 | // Minify the code.
|
259 | new webpack.optimize.UglifyJsPlugin({
|
260 | compress: {
|
261 | screw_ie8: true, // React doesn't support IE8
|
262 | warnings: false
|
263 | },
|
264 | mangle: {
|
265 | screw_ie8: true
|
266 | },
|
267 | output: {
|
268 | comments: false,
|
269 | screw_ie8: true
|
270 | }
|
271 | }),
|
272 | // Note: this won't work without ExtractTextPlugin.extract(..) in `loaders`.
|
273 | new ExtractTextPlugin(cssFilename),
|
274 | // Generate a manifest file which contains a mapping of all asset filenames
|
275 | // to their corresponding output file so that tools can pick it up without
|
276 | // having to parse `index.html`.
|
277 | new ManifestPlugin({
|
278 | fileName: 'asset-manifest.json'
|
279 | })
|
280 | ].concat(webpackConfigHelper.buildHtmlWebpackPlugins({
|
281 | entryPointConfig: entryPoints,
|
282 | production: true
|
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 | fs: 'empty',
|
288 | net: 'empty',
|
289 | tls: 'empty'
|
290 | }
|
291 | };
|