Advertisement
ikkew

webpack.config.dev.js

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