|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var autoprefixer = require('autoprefixer'); |
| 4 | +var webpack = require('webpack'); |
| 5 | +var HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 6 | +var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin'); |
| 7 | +var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); |
| 8 | +var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin'); |
| 9 | +var getClientEnvironment = require('./env'); |
| 10 | +var paths = require('./paths'); |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +// Webpack uses `publicPath` to determine where the app is being served from. |
| 15 | +// In development, we always serve from the root. This makes config easier. |
| 16 | +var publicPath = '/'; |
| 17 | +// `publicUrl` is just like `publicPath`, but we will provide it to our app |
| 18 | +// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript. |
| 19 | +// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz. |
| 20 | +var publicUrl = ''; |
| 21 | +// Get environment variables to inject into our app. |
| 22 | +var env = getClientEnvironment(publicUrl); |
| 23 | + |
| 24 | +// This is the development configuration. |
| 25 | +// It is focused on developer experience and fast rebuilds. |
| 26 | +// The production configuration is different and lives in a separate file. |
| 27 | +module.exports = { |
| 28 | + // You may want 'eval' instead if you prefer to see the compiled output in DevTools. |
| 29 | + // See the discussion in https://github.com/facebookincubator/create-react-app/issues/343. |
| 30 | + devtool: 'cheap-module-source-map', |
| 31 | + // These are the "entry points" to our application. |
| 32 | + // This means they will be the "root" imports that are included in JS bundle. |
| 33 | + // The first two entry points enable "hot" CSS and auto-refreshes for JS. |
| 34 | + entry: [ |
| 35 | + // Include an alternative client for WebpackDevServer. A client's job is to |
| 36 | + // connect to WebpackDevServer by a socket and get notified about changes. |
| 37 | + // When you save a file, the client will either apply hot updates (in case |
| 38 | + // of CSS changes), or refresh the page (in case of JS changes). When you |
| 39 | + // make a syntax error, this client will display a syntax error overlay. |
| 40 | + // Note: instead of the default WebpackDevServer client, we use a custom one |
| 41 | + // to bring better experience for Create React App users. You can replace |
| 42 | + // the line below with these two lines if you prefer the stock client: |
| 43 | + // require.resolve('webpack-dev-server/client') + '?/', |
| 44 | + // require.resolve('webpack/hot/dev-server'), |
| 45 | + require.resolve('react-dev-utils/webpackHotDevClient'), |
| 46 | + // We ship a few polyfills by default: |
| 47 | + require.resolve('./polyfills'), |
| 48 | + // Finally, this is your app's code: |
| 49 | + paths.appIndexJs |
| 50 | + // We include the app code last so that if there is a runtime error during |
| 51 | + // initialization, it doesn't blow up the WebpackDevServer client, and |
| 52 | + // changing JS code would still trigger a refresh. |
| 53 | + ], |
| 54 | + output: { |
| 55 | + // Next line is not used in dev but WebpackDevServer crashes without it: |
| 56 | + path: paths.appBuild, |
| 57 | + // Add /* filename */ comments to generated require()s in the output. |
| 58 | + pathinfo: true, |
| 59 | + // This does not produce a real file. It's just the virtual path that is |
| 60 | + // served by WebpackDevServer in development. This is the JS bundle |
| 61 | + // containing code from all our entry points, and the Webpack runtime. |
| 62 | + filename: 'static/js/bundle.js', |
| 63 | + // This is the URL that app is served from. We use "/" in development. |
| 64 | + publicPath: publicPath |
| 65 | + }, |
| 66 | + resolve: { |
| 67 | + // This allows you to set a fallback for where Webpack should look for modules. |
| 68 | + // We read `NODE_PATH` environment variable in `paths.js` and pass paths here. |
| 69 | + // We use `fallback` instead of `root` because we want `node_modules` to "win" |
| 70 | + // if there any conflicts. This matches Node resolution mechanism. |
| 71 | + // https://github.com/facebookincubator/create-react-app/issues/253 |
| 72 | + fallback: paths.nodePaths, |
| 73 | + // These are the reasonable defaults supported by the Node ecosystem. |
| 74 | + // We also include JSX as a common component filename extension to support |
| 75 | + // some tools, although we do not recommend using it, see: |
| 76 | + // https://github.com/facebookincubator/create-react-app/issues/290 |
| 77 | + extensions: ['.js', '.json', '.jsx', ''], |
| 78 | + alias: { |
| 79 | + // Support React Native Web |
| 80 | + // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/ |
| 81 | + 'react-native': 'react-native-web' |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + module: { |
| 86 | + // First, run the linter. |
| 87 | + // It's important to do this before Babel processes the JS. |
| 88 | + preLoaders: [ |
| 89 | + { |
| 90 | + test: /\.(js|jsx)$/, |
| 91 | + loader: 'eslint', |
| 92 | + include: paths.appSrc, |
| 93 | + } |
| 94 | + ], |
| 95 | + loaders: [ |
| 96 | + // ** ADDING/UPDATING LOADERS ** |
| 97 | + // The "url" loader handles all assets unless explicitly excluded. |
| 98 | + // The `exclude` list *must* be updated with every change to loader extensions. |
| 99 | + // When adding a new loader, you must add its `test` |
| 100 | + // as a new entry in the `exclude` list for "url" loader. |
| 101 | + |
| 102 | + // "url" loader embeds assets smaller than specified size as data URLs to avoid requests. |
| 103 | + // Otherwise, it acts like the "file" loader. |
| 104 | + { |
| 105 | + exclude: [ |
| 106 | + /\.html$/, |
| 107 | + // We have to write /\.(js|jsx)(\?.*)?$/ rather than just /\.(js|jsx)$/ |
| 108 | + // because you might change the hot reloading server from the custom one |
| 109 | + // to Webpack's built-in webpack-dev-server/client?/, which would not |
| 110 | + // get properly excluded by /\.(js|jsx)$/ because of the query string. |
| 111 | + // Webpack 2 fixes this, but for now we include this hack. |
| 112 | + // https://github.com/facebookincubator/create-react-app/issues/1713 |
| 113 | + /\.(js|jsx)(\?.*)?$/, |
| 114 | + /\.css$/, |
| 115 | + /\.json$/, |
| 116 | + /\.svg$/ |
| 117 | + ], |
| 118 | + loader: 'url', |
| 119 | + query: { |
| 120 | + limit: 10000, |
| 121 | + name: 'static/media/[name].[hash:8].[ext]' |
| 122 | + } |
| 123 | + }, |
| 124 | + // Process JS with Babel. |
| 125 | + { |
| 126 | + test: /\.(js|jsx)$/, |
| 127 | + include: paths.appSrc, |
| 128 | + loader: 'babel', |
| 129 | + query: { |
| 130 | + |
| 131 | + // This is a feature of `babel-loader` for webpack (not Babel itself). |
| 132 | + // It enables caching results in ./node_modules/.cache/babel-loader/ |
| 133 | + // directory for faster rebuilds. |
| 134 | + cacheDirectory: true |
| 135 | + } |
| 136 | + }, |
| 137 | + // "postcss" loader applies autoprefixer to our CSS. |
| 138 | + // "css" loader resolves paths in CSS and adds assets as dependencies. |
| 139 | + // "style" loader turns CSS into JS modules that inject <style> tags. |
| 140 | + // In production, we use a plugin to extract that CSS to a file, but |
| 141 | + // in development "style" loader enables hot editing of CSS. |
| 142 | + { |
| 143 | + test: /\.css$/, |
| 144 | + loader: 'style!css?importLoaders=1!postcss' |
| 145 | + }, |
| 146 | + // JSON is not enabled by default in Webpack but both Node and Browserify |
| 147 | + // allow it implicitly so we also enable it. |
| 148 | + { |
| 149 | + test: /\.json$/, |
| 150 | + loader: 'json' |
| 151 | + }, |
| 152 | + // "file" loader for svg |
| 153 | + { |
| 154 | + test: /\.svg$/, |
| 155 | + loader: 'file', |
| 156 | + query: { |
| 157 | + name: 'static/media/[name].[hash:8].[ext]' |
| 158 | + } |
| 159 | + } |
| 160 | + // ** STOP ** Are you adding a new loader? |
| 161 | + // Remember to add the new extension(s) to the "url" loader exclusion list. |
| 162 | + ] |
| 163 | + }, |
| 164 | + |
| 165 | + // We use PostCSS for autoprefixing only. |
| 166 | + postcss: function() { |
| 167 | + return [ |
| 168 | + autoprefixer({ |
| 169 | + browsers: [ |
| 170 | + '>1%', |
| 171 | + 'last 4 versions', |
| 172 | + 'Firefox ESR', |
| 173 | + 'not ie < 9', // React doesn't support IE8 anyway |
| 174 | + ] |
| 175 | + }), |
| 176 | + ]; |
| 177 | + }, |
| 178 | + plugins: [ |
| 179 | + // Makes some environment variables available in index.html. |
| 180 | + // The public URL is available as %PUBLIC_URL% in index.html, e.g.: |
| 181 | + // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> |
| 182 | + // In development, this will be an empty string. |
| 183 | + new InterpolateHtmlPlugin(env.raw), |
| 184 | + // Generates an `index.html` file with the <script> injected. |
| 185 | + new HtmlWebpackPlugin({ |
| 186 | + inject: true, |
| 187 | + template: paths.appHtml, |
| 188 | + }), |
| 189 | + // Makes some environment variables available to the JS code, for example: |
| 190 | + // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`. |
| 191 | + new webpack.DefinePlugin(env.stringified), |
| 192 | + // This is necessary to emit hot updates (currently CSS only): |
| 193 | + new webpack.HotModuleReplacementPlugin(), |
| 194 | + // Watcher doesn't work well if you mistype casing in a path so we use |
| 195 | + // a plugin that prints an error when you attempt to do this. |
| 196 | + // See https://github.com/facebookincubator/create-react-app/issues/240 |
| 197 | + new CaseSensitivePathsPlugin(), |
| 198 | + // If you require a missing module and then `npm install` it, you still have |
| 199 | + // to restart the development server for Webpack to discover it. This plugin |
| 200 | + // makes the discovery automatic so you don't have to restart. |
| 201 | + // See https://github.com/facebookincubator/create-react-app/issues/186 |
| 202 | + new WatchMissingNodeModulesPlugin(paths.appNodeModules) |
| 203 | + ], |
| 204 | + // Some libraries import Node modules but don't use them in the browser. |
| 205 | + // Tell Webpack to provide empty mocks for them so importing them works. |
| 206 | + node: { |
| 207 | + fs: 'empty', |
| 208 | + net: 'empty', |
| 209 | + tls: 'empty' |
| 210 | + } |
| 211 | +}; |
0 commit comments