webpack.config.dev.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
  6. const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
  7. const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
  8. const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
  9. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  10. const getClientEnvironment = require('./env');
  11. const paths = require('./paths');
  12. const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
  13. const loaders = require('./loaders');
  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. const 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. const publicUrl = '';
  21. // Get environment variables to inject into our app.
  22. const env = getClientEnvironment(publicUrl);
  23. // This is the development configuration.
  24. // It is focused on developer experience and fast rebuilds.
  25. // The production configuration is different and lives in a separate file.
  26. module.exports = {
  27. // You may want 'eval' instead if you prefer to see the compiled output in DevTools.
  28. // See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
  29. devtool: 'cheap-module-source-map',
  30. // These are the "entry points" to our application.
  31. // This means they will be the "root" imports that are included in JS bundle.
  32. // The first two entry points enable "hot" CSS and auto-refreshes for JS.
  33. entry: [
  34. // We ship a few polyfills by default:
  35. require.resolve('./polyfills'),
  36. // Include an alternative client for WebpackDevServer. A client's job is to
  37. // connect to WebpackDevServer by a socket and get notified about changes.
  38. // When you save a file, the client will either apply hot updates (in case
  39. // of CSS changes), or refresh the page (in case of JS changes). When you
  40. // make a syntax error, this client will display a syntax error overlay.
  41. // Note: instead of the default WebpackDevServer client, we use a custom one
  42. // to bring better experience for Create React App users. You can replace
  43. // the line below with these two lines if you prefer the stock client:
  44. // require.resolve('webpack-dev-server/client') + '?/',
  45. // require.resolve('webpack/hot/dev-server'),
  46. require.resolve('react-dev-utils/webpackHotDevClient'),
  47. // Finally, this is your app's code:
  48. paths.appIndexJs,
  49. // We include the app code last so that if there is a runtime error during
  50. // initialization, it doesn't blow up the WebpackDevServer client, and
  51. // changing JS code would still trigger a refresh.
  52. ],
  53. output: {
  54. // Add /* filename */ comments to generated require()s in the output.
  55. pathinfo: true,
  56. // This does not produce a real file. It's just the virtual path that is
  57. // served by WebpackDevServer in development. This is the JS bundle
  58. // containing code from all our entry points, and the Webpack runtime.
  59. filename: 'static/js/bundle.js',
  60. // There are also additional JS chunk files if you use code splitting.
  61. chunkFilename: 'static/js/[name].chunk.js',
  62. // This is the URL that app is served from. We use "/" in development.
  63. publicPath: publicPath,
  64. // Point sourcemap entries to original disk location (format as URL on Windows)
  65. devtoolModuleFilenameTemplate: info =>
  66. path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
  67. },
  68. resolve: {
  69. // This allows you to set a fallback for where Webpack should look for modules.
  70. // We placed these paths second because we want `node_modules` to "win"
  71. // if there are any conflicts. This matches Node resolution mechanism.
  72. // https://github.com/facebookincubator/create-react-app/issues/253
  73. modules: ['node_modules', paths.appNodeModules].concat(
  74. // It is guaranteed to exist because we tweak it in `env.js`
  75. process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
  76. ),
  77. // These are the reasonable defaults supported by the Node ecosystem.
  78. // We also include JSX as a common component filename extension to support
  79. // some tools, although we do not recommend using it, see:
  80. // https://github.com/facebookincubator/create-react-app/issues/290
  81. // `web` extension prefixes have been added for better support
  82. // for React Native Web.
  83. extensions: [
  84. '.mjs',
  85. '.web.ts',
  86. '.ts',
  87. '.web.tsx',
  88. '.tsx',
  89. '.web.js',
  90. '.js',
  91. '.json',
  92. '.web.jsx',
  93. '.jsx',
  94. ],
  95. alias: {
  96. // Support React Native Web
  97. // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
  98. 'react-native': 'react-native-web',
  99. },
  100. plugins: [
  101. // Prevents users from importing files from outside of src/ (or node_modules/).
  102. // This often causes confusion because we only process files within src/ with babel.
  103. // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
  104. // please link the files into your node_modules/ and let module-resolution kick in.
  105. // Make sure your source files are compiled, as they will not be processed in any way.
  106. new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
  107. new TsconfigPathsPlugin({ configFile: paths.appTsConfig }),
  108. ],
  109. },
  110. module: {
  111. strictExportPresence: true,
  112. rules: [
  113. // TODO: Disable require.ensure as it's not a standard language feature.
  114. // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
  115. // { parser: { requireEnsure: false } },
  116. {
  117. test: /\.(js|jsx|mjs)$/,
  118. loader: require.resolve('source-map-loader'),
  119. enforce: 'pre',
  120. include: paths.appSrc,
  121. },
  122. {
  123. // "oneOf" will traverse all following loaders until one will
  124. // match the requirements. When no loader matches it will fall
  125. // back to the "file" loader at the end of the loader list.
  126. oneOf: [
  127. loaders.urlLoader,
  128. loaders.jsLoader,
  129. loaders.tsLoader,
  130. loaders.cssLoaderDev,
  131. loaders.scssLoaderDev,
  132. loaders.lessLoaderDev,
  133. loaders.fileLoader,
  134. ],
  135. },
  136. // ** STOP ** Are you adding a new loader?
  137. // Make sure to add the new loader(s) before the "file" loader.
  138. ],
  139. },
  140. plugins: [
  141. // Makes some environment variables available in index.html.
  142. // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
  143. // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  144. // In development, this will be an empty string.
  145. new InterpolateHtmlPlugin(env.raw),
  146. // Generates an `index.html` file with the <script> injected.
  147. new HtmlWebpackPlugin({
  148. inject: true,
  149. template: paths.appHtml,
  150. }),
  151. // Add module names to factory functions so they appear in browser profiler.
  152. new webpack.NamedModulesPlugin(),
  153. // Makes some environment variables available to the JS code, for example:
  154. // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
  155. new webpack.DefinePlugin(env.stringified),
  156. // This is necessary to emit hot updates (currently CSS only):
  157. new webpack.HotModuleReplacementPlugin(),
  158. // Watcher doesn't work well if you mistype casing in a path so we use
  159. // a plugin that prints an error when you attempt to do this.
  160. // See https://github.com/facebookincubator/create-react-app/issues/240
  161. new CaseSensitivePathsPlugin(),
  162. // If you require a missing module and then `npm install` it, you still have
  163. // to restart the development server for Webpack to discover it. This plugin
  164. // makes the discovery automatic so you don't have to restart.
  165. // See https://github.com/facebookincubator/create-react-app/issues/186
  166. new WatchMissingNodeModulesPlugin(paths.appNodeModules),
  167. // Moment.js is an extremely popular library that bundles large locale files
  168. // by default due to how Webpack interprets its code. This is a practical
  169. // solution that requires the user to opt into importing specific locales.
  170. // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
  171. // You can remove this if you don't use Moment.js:
  172. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  173. // Perform type checking and linting in a separate process to speed up compilation
  174. new ForkTsCheckerWebpackPlugin({
  175. async: false,
  176. watch: paths.appSrc,
  177. tsconfig: paths.appTsConfig,
  178. tslint: paths.appTsLint,
  179. }),
  180. ],
  181. // Some libraries import Node modules but don't use them in the browser.
  182. // Tell Webpack to provide empty mocks for them so importing them works.
  183. node: {
  184. dgram: 'empty',
  185. fs: 'empty',
  186. net: 'empty',
  187. tls: 'empty',
  188. child_process: 'empty',
  189. },
  190. // Turn off performance hints during development because we don't do any
  191. // splitting or minification in interest of speed. These warnings become
  192. // cumbersome.
  193. performance: {
  194. hints: false,
  195. },
  196. };