webpack.config.dev.js 9.4 KB

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