webpack.config.dev.js 9.2 KB

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