webpack.config.dev.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
  10. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  11. const getClientEnvironment = require('./env');
  12. const paths = require('./paths');
  13. const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
  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. // "url" loader works like "file" loader except that it embeds assets
  128. // smaller than specified limit in bytes as data URLs to avoid requests.
  129. // A missing `test` is equivalent to a match.
  130. {
  131. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  132. loader: require.resolve('url-loader'),
  133. options: {
  134. limit: 10000,
  135. name: 'static/media/[name].[hash:8].[ext]',
  136. },
  137. },
  138. {
  139. test: /\.(js|jsx|mjs)$/,
  140. include: paths.appSrc,
  141. loader: require.resolve('babel-loader'),
  142. options: {
  143. compact: true,
  144. },
  145. },
  146. // Compile .tsx?
  147. {
  148. test: /\.(ts|tsx)$/,
  149. include: paths.appSrc,
  150. use: [
  151. {
  152. loader: require.resolve('ts-loader'),
  153. options: {
  154. // disable type checker - we will use it in fork plugin
  155. transpileOnly: true,
  156. },
  157. },
  158. ],
  159. },
  160. // "postcss" loader applies autoprefixer to our CSS.
  161. // "css" loader resolves paths in CSS and adds assets as dependencies.
  162. // "style" loader turns CSS into JS modules that inject <style> tags.
  163. // In production, we use a plugin to extract that CSS to a file, but
  164. // in development "style" loader enables hot editing of CSS.
  165. {
  166. test: /\.s?css$/,
  167. use: [
  168. require.resolve('style-loader'),
  169. {
  170. loader: require.resolve('css-loader'),
  171. options: {
  172. importLoaders: 1
  173. },
  174. },
  175. {
  176. loader: require.resolve('postcss-loader'),
  177. options: {
  178. // Necessary for external CSS imports to work
  179. // https://github.com/facebookincubator/create-react-app/issues/2677
  180. ident: 'postcss',
  181. plugins: () => [
  182. require('postcss-flexbugs-fixes'),
  183. autoprefixer({
  184. browsers: [
  185. '>1%',
  186. 'last 4 versions',
  187. 'Firefox ESR',
  188. 'not ie < 9', // React doesn't support IE8 anyway
  189. ],
  190. flexbox: 'no-2009',
  191. }),
  192. ],
  193. },
  194. },
  195. require.resolve('sass-loader'),
  196. ],
  197. },
  198. // "file" loader makes sure those assets get served by WebpackDevServer.
  199. // When you `import` an asset, you get its (virtual) filename.
  200. // In production, they would get copied to the `build` folder.
  201. // This loader doesn't use a "test" so it will catch all modules
  202. // that fall through the other loaders.
  203. {
  204. // Exclude `js` files to keep "css" loader working as it injects
  205. // its runtime that would otherwise processed through "file" loader.
  206. // Also exclude `html` and `json` extensions so they get processed
  207. // by webpacks internal loaders.
  208. exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/,/\.scss$/],
  209. loader: require.resolve('file-loader'),
  210. options: {
  211. name: 'static/media/[name].[hash:8].[ext]',
  212. },
  213. },
  214. ],
  215. },
  216. // ** STOP ** Are you adding a new loader?
  217. // Make sure to add the new loader(s) before the "file" loader.
  218. ],
  219. },
  220. plugins: [
  221. // Makes some environment variables available in index.html.
  222. // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
  223. // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  224. // In development, this will be an empty string.
  225. new InterpolateHtmlPlugin(env.raw),
  226. // Generates an `index.html` file with the <script> injected.
  227. new HtmlWebpackPlugin({
  228. inject: true,
  229. template: paths.appHtml,
  230. }),
  231. // Add module names to factory functions so they appear in browser profiler.
  232. new webpack.NamedModulesPlugin(),
  233. // Makes some environment variables available to the JS code, for example:
  234. // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
  235. new webpack.DefinePlugin(env.stringified),
  236. // This is necessary to emit hot updates (currently CSS only):
  237. new webpack.HotModuleReplacementPlugin(),
  238. // Watcher doesn't work well if you mistype casing in a path so we use
  239. // a plugin that prints an error when you attempt to do this.
  240. // See https://github.com/facebookincubator/create-react-app/issues/240
  241. new CaseSensitivePathsPlugin(),
  242. // If you require a missing module and then `npm install` it, you still have
  243. // to restart the development server for Webpack to discover it. This plugin
  244. // makes the discovery automatic so you don't have to restart.
  245. // See https://github.com/facebookincubator/create-react-app/issues/186
  246. new WatchMissingNodeModulesPlugin(paths.appNodeModules),
  247. // Moment.js is an extremely popular library that bundles large locale files
  248. // by default due to how Webpack interprets its code. This is a practical
  249. // solution that requires the user to opt into importing specific locales.
  250. // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
  251. // You can remove this if you don't use Moment.js:
  252. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  253. // Perform type checking and linting in a separate process to speed up compilation
  254. new ForkTsCheckerWebpackPlugin({
  255. async: false,
  256. watch: paths.appSrc,
  257. tsconfig: paths.appTsConfig,
  258. tslint: paths.appTsLint,
  259. }),
  260. ],
  261. // Some libraries import Node modules but don't use them in the browser.
  262. // Tell Webpack to provide empty mocks for them so importing them works.
  263. node: {
  264. dgram: 'empty',
  265. fs: 'empty',
  266. net: 'empty',
  267. tls: 'empty',
  268. child_process: 'empty',
  269. },
  270. // Turn off performance hints during development because we don't do any
  271. // splitting or minification in interest of speed. These warnings become
  272. // cumbersome.
  273. performance: {
  274. hints: false,
  275. },
  276. };