Advertisement
Guest User

webpack.config.dev.js

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