UNPKG

9.1 kBJavaScriptView Raw
1'use strict';
2
3const autoprefixer = require('autoprefixer');
4const path = require('path');
5const webpack = require('webpack');
6const ExtractTextPlugin = require('extract-text-webpack-plugin');
7const ManifestPlugin = require('webpack-manifest-plugin');
8const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
9const eslintFormatter = require('react-dev-utils/eslintFormatter');
10const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
11const paths = require('./paths');
12const getClientEnvironment = require('./env');
13console.log('WHIMMY WHAM')
14
15// Webpack uses `publicPath` to determine where the app is being served from.
16// It requires a trailing slash, or the file assets will get an incorrect path.
17const publicPath = paths.servedPath;
18// Some apps do not use client-side routing with pushState.
19// For these, "homepage" can be set to "." to enable relative asset paths.
20const shouldUseRelativeAssetPaths = publicPath === './';
21// Source maps are resource heavy and can cause out of memory issue for large source files.
22const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
23// `publicUrl` is just like `publicPath`, but we will provide it to our app
24// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
25// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
26const publicUrl = publicPath.slice(0, -1);
27// Get environment variables to inject into our app.
28const env = getClientEnvironment(publicUrl);
29
30// Assert this just to be safe.
31// Development builds of React are slow and not intended for production.
32if (env.stringified['process.env'].NODE_ENV !== '"production"') {
33 throw new Error('Production builds must have NODE_ENV=production.');
34}
35
36// Note: defined here because it will be used more than once.
37const cssFilename = 'static/css/[name].[contenthash:8].css';
38
39// ExtractTextPlugin expects the build output to be flat.
40// (See https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/27)
41// However, our output is structured with css, js and media folders.
42// To have this structure working with relative paths, we have to use custom options.
43const extractTextPluginOptions = shouldUseRelativeAssetPaths
44 ? // Making sure that the publicPath goes back to to build folder.
45 { publicPath: Array(cssFilename.split('/').length).join('../') }
46 : {};
47
48// This is the production configuration.
49// It compiles slowly and is focused on producing a fast and minimal bundle.
50// The development configuration is different and lives in a separate file.
51module.exports = {
52 // Don't attempt to continue if there are any errors.
53 bail: true,
54 // We generate sourcemaps in production. This is slow but gives good results.
55 // You can exclude the *.map files from the build during deployment.
56 devtool: shouldUseSourceMap ? 'source-map' : false,
57 // In production, we only want to load the polyfills and the app code.
58 entry: [require.resolve('./polyfills'), paths.appIndexJs],
59 output: {
60 // The build folder.
61 path: paths.appBuild,
62 // Generated JS file names (with nested folders).
63 // There will be one main bundle, and one file per asynchronous chunk.
64 // We don't currently advertise code splitting but Webpack supports it.
65 filename: 'index.js',
66 library: '',
67 libraryTarget: 'commonjs',
68 // We inferred the "public path" (such as / or /my-project) from homepage.
69 publicPath: publicPath,
70 // Point sourcemap entries to original disk location (format as URL on Windows)
71 devtoolModuleFilenameTemplate: info =>
72 path
73 .relative(paths.appSrc, info.absoluteResourcePath)
74 .replace(/\\/g, '/'),
75 },
76 resolve: {
77 // This allows you to set a fallback for where Webpack should look for modules.
78 // We placed these paths second because we want `node_modules` to "win"
79 // if there are any conflicts. This matches Node resolution mechanism.
80 // https://github.com/facebookincubator/create-react-app/issues/253
81 modules: ['node_modules', paths.appNodeModules, paths.appSrc].concat(
82 // It is guaranteed to exist because we tweak it in `env.js`
83 process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
84 ),
85 // These are the reasonable defaults supported by the Node ecosystem.
86 // We also include JSX as a common component filename extension to support
87 // some tools, although we do not recommend using it, see:
88 // https://github.com/facebookincubator/create-react-app/issues/290
89 // `web` extension prefixes have been added for better support
90 // for React Native Web.
91 extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
92 alias: {
93
94 // Support React Native Web
95 // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
96 'react-native': 'react-native-web',
97 'SRC': paths.appSrc
98 },
99 plugins: [
100 // Prevents users from importing files from outside of src/ (or node_modules/).
101 // This often causes confusion because we only process files within src/ with babel.
102 // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
103 // please link the files into your node_modules/ and let module-resolution kick in.
104 // Make sure your source files are compiled, as they will not be processed in any way.
105 new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
106 ],
107 },
108 module: {
109 strictExportPresence: true,
110 rules: [
111 // TODO: Disable require.ensure as it's not a standard language feature.
112 // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
113 // { parser: { requireEnsure: false } },
114
115 // First, run the linter.
116 // It's important to do this before Babel processes the JS.
117 {
118 test: /\.(js|jsx|mjs)$/,
119 enforce: 'pre',
120 use: [
121 {
122 options: {
123 formatter: eslintFormatter,
124 eslintPath: require.resolve('eslint'),
125
126 },
127 loader: require.resolve('eslint-loader'),
128 },
129 ],
130 include: paths.appSrc,
131 },
132 {
133 // "oneOf" will traverse all following loaders until one will
134 // match the requirements. When no loader matches it will fall
135 // back to the "file" loader at the end of the loader list.
136 oneOf: [
137 // Process JS with Babel.
138 {
139 test: /\.(js|jsx|mjs)$/,
140 include: paths.appSrc,
141 loader: require.resolve('babel-loader'),
142 options: {
143
144 compact: true,
145 },
146 },
147 ],
148 },
149 ],
150 },
151 plugins: [
152 // Makes some environment variables available to the JS code, for example:
153 // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
154 new webpack.DefinePlugin(env.stringified),
155 // Generate a service worker script that will precache, and keep up to date,
156 // the HTML & assets that are part of the Webpack build.
157 new SWPrecacheWebpackPlugin({
158 // By default, a cache-busting query parameter is appended to requests
159 // used to populate the caches, to ensure the responses are fresh.
160 // If a URL is already hashed by Webpack, then there is no concern
161 // about it being stale, and the cache-busting can be skipped.
162 dontCacheBustUrlsMatching: /\.\w{8}\./,
163 filename: 'service-worker.js',
164 logger(message) {
165 if (message.indexOf('Total precache size is') === 0) {
166 // This message occurs for every build and is a bit too noisy.
167 return;
168 }
169 if (message.indexOf('Skipping static resource') === 0) {
170 // This message obscures real errors so we ignore it.
171 // https://github.com/facebookincubator/create-react-app/issues/2612
172 return;
173 }
174 console.log(message);
175 },
176 minify: false,
177 // For unknown URLs, fallback to the index page
178 navigateFallback: publicUrl + '/index.html',
179 // Ignores URLs starting from /__ (useful for Firebase):
180 // https://github.com/facebookincubator/create-react-app/issues/2237#issuecomment-302693219
181 navigateFallbackWhitelist: [/^(?!\/__).*/],
182 // Don't precache sourcemaps (they're large) and build asset manifest:
183 staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/],
184 }),
185 // Moment.js is an extremely popular library that bundles large locale files
186 // by default due to how Webpack interprets its code. This is a practical
187 // solution that requires the user to opt into importing specific locales.
188 // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
189 // You can remove this if you don't use Moment.js:
190 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
191 ],
192 // Some libraries import Node modules but don't use them in the browser.
193 // Tell Webpack to provide empty mocks for them so importing them works.
194 node: {
195 dgram: 'empty',
196 fs: 'empty',
197 net: 'empty',
198 tls: 'empty',
199 child_process: 'empty',
200 },
201};