Ingen beskrivning

webpack.config.dev.js 12KB

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