Advertisement
Guest User

webpack.config.dev.js

a guest
Oct 30th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 13.10 KB | None | 0 0
  1. const autoprefixer = require('autoprefixer');
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
  6. const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
  7. const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
  8. const eslintFormatter = require('react-dev-utils/eslintFormatter');
  9. const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
  10. const StyleLintPlugin = require('stylelint-webpack-plugin');
  11. const getClientEnvironment = require('./env');
  12. const 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. 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.  
  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.     // Errors should be considered fatal in development
  49.     require.resolve('react-error-overlay'),
  50.     // Finally, this is your app's code:
  51.     paths.appIndexJs,
  52.     // We include the app code last so that if there is a runtime error during
  53.     // initialization, it doesn't blow up the WebpackDevServer client, and
  54.     // changing JS code would still trigger a refresh.
  55.   ],
  56.   output: {
  57.     // Next line is not used in dev but WebpackDevServer crashes without it:
  58.     path: paths.appBuild,
  59.     // Add /* filename */ comments to generated require()s in the output.
  60.     pathinfo: true,
  61.     // This does not produce a real file. It's just the virtual path that is
  62.     // served by WebpackDevServer in development. This is the JS bundle
  63.     // containing code from all our entry points, and the Webpack runtime.
  64.     filename: 'static/js/bundle.js',
  65.     // There are also additional JS chunk files if you use code splitting.
  66.     chunkFilename: 'static/js/[name].chunk.js',
  67.     // This is the URL that app is served from. We use "/" in development.
  68.     publicPath,
  69.     // Point sourcemap entries to original disk location (format as URL on Windows)
  70.     devtoolModuleFilenameTemplate: info =>
  71.       path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
  72.   },
  73.   resolve: {
  74.     // This allows you to set a fallback for where Webpack should look for modules.
  75.     // We placed these paths second because we want `node_modules` to "win"
  76.     // if there are any conflicts. This matches Node resolution mechanism.
  77.     // https://github.com/facebookincubator/create-react-app/issues/253
  78.     modules: ['node_modules', paths.appNodeModules].concat(
  79.       // It is guaranteed to exist because we tweak it in `env.js`
  80.       process.env.NODE_PATH.split(path.delimiter).filter(Boolean),
  81.     ),
  82.     // These are the reasonable defaults supported by the Node ecosystem.
  83.     // We also include JSX as a common component filename extension to support
  84.     // some tools, although we do not recommend using it, see:
  85.     // https://github.com/facebookincubator/create-react-app/issues/290
  86.     // `web` extension prefixes have been added for better support
  87.     // for React Native Web.
  88.     extensions: ['.web.js', '.js', '.json', '.web.jsx', '.jsx'],
  89.     alias: {
  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),
  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)$/,
  114.         enforce: 'pre',
  115.         use: [
  116.           {
  117.             options: {
  118.               formatter: eslintFormatter,
  119.             },
  120.             loader: require.resolve('eslint-loader'),
  121.           },
  122.         ],
  123.         include: paths.appSrc,
  124.       },
  125.       // ** ADDING/UPDATING LOADERS **
  126.       // The "file" loader handles all assets unless explicitly excluded.
  127.       // The `exclude` list *must* be updated with every change to loader extensions.
  128.       // When adding a new loader, you must add its `test`
  129.       // as a new entry in the `exclude` list for "file" loader.
  130.  
  131.       // "file" loader makes sure those assets get served by WebpackDevServer.
  132.       // When you `import` an asset, you get its (virtual) filename.
  133.       // In production, they would get copied to the `build` folder.
  134.       {
  135.         exclude: [
  136.           /\.html$/,
  137.           /\.(js|jsx)$/,
  138.           /\.css$/,
  139.           /\.scss$/,
  140.           /\.json$/,
  141.           /\.bmp$/,
  142.           /\.gif$/,
  143.           /\.jpe?g$/,
  144.           /\.png$/,
  145.         ],
  146.         loader: require.resolve('file-loader'),
  147.         options: {
  148.           name: 'static/media/[name].[hash:8].[ext]',
  149.         },
  150.       },
  151.       // "url" loader works like "file" loader except that it embeds assets
  152.       // smaller than specified limit in bytes as data URLs to avoid requests.
  153.       // A missing `test` is equivalent to a match.
  154.       {
  155.         test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  156.         loader: require.resolve('url-loader'),
  157.         options: {
  158.           limit: 10000,
  159.           name: 'static/media/[name].[hash:8].[ext]',
  160.         },
  161.       },
  162.       // Process JS with Babel.
  163.       {
  164.         test: /\.(js|jsx)$/,
  165.         include: paths.appSrc,
  166.         loader: require.resolve('babel-loader'),
  167.         options: {
  168.           // This is a feature of `babel-loader` for webpack (not Babel itself).
  169.           // It enables caching results in ./node_modules/.cache/babel-loader/
  170.           // directory for faster rebuilds.
  171.           cacheDirectory: false,
  172.         },
  173.       },
  174.       {
  175.         test: /\.scss$/,
  176.         include: paths.appSrc,
  177.         loaders: [
  178.           require.resolve('style-loader'),
  179.           require.resolve('css-loader'),
  180.           {
  181.             loader: require.resolve('postcss-loader'),
  182.             options: {
  183.               // Necessary for external CSS imports to work
  184.               // https://github.com/facebookincubator/create-react-app/issues/2677
  185.               ident: 'postcss',
  186.               plugins: () => [
  187.                 require('postcss-flexbugs-fixes'),
  188.                 autoprefixer({
  189.                   browsers: [
  190.                     '>1%',
  191.                     'last 4 versions',
  192.                     'Firefox ESR',
  193.                     'not ie < 9', // React doesn't support IE8 anyway
  194.                   ],
  195.                   flexbox: 'no-2009',
  196.                 }),
  197.               ],
  198.             },
  199.           },
  200.           require.resolve('sass-loader'),
  201.         ],
  202.       },
  203.       // "postcss" loader applies autoprefixer to our CSS.
  204.       // "css" loader resolves paths in CSS and adds assets as dependencies.
  205.       // "style" loader turns CSS into JS modules that inject <style> tags.
  206.       // In production, we use a plugin to extract that CSS to a file, but
  207.       // in development "style" loader enables hot editing of CSS.
  208.       {
  209.         test: /\.css$/,
  210.         use: [
  211.           require.resolve('style-loader'),
  212.           {
  213.             loader: require.resolve('css-loader'),
  214.             options: {
  215.               importLoaders: 1,
  216.             },
  217.           },
  218.           {
  219.             loader: require.resolve('postcss-loader'),
  220.             options: {
  221.               // Necessary for external CSS imports to work
  222.               // https://github.com/facebookincubator/create-react-app/issues/2677
  223.               ident: 'postcss',
  224.               plugins: () => [
  225.                 require('postcss-flexbugs-fixes'),
  226.                 autoprefixer({
  227.                   browsers: [
  228.                     '>1%',
  229.                     'last 4 versions',
  230.                     'Firefox ESR',
  231.                     'not ie < 9', // React doesn't support IE8 anyway
  232.                   ],
  233.                   flexbox: 'no-2009',
  234.                 }),
  235.               ],
  236.             },
  237.           },
  238.         ],
  239.       },
  240.       // ** STOP ** Are you adding a new loader?
  241.       // Remember to add the new extension(s) to the "file" loader exclusion list.
  242.     ],
  243.   },
  244.   plugins: [
  245.     // Makes some environment variables available in index.html.
  246.     // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
  247.     // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  248.     // In development, this will be an empty string.
  249.     new InterpolateHtmlPlugin(env.raw),
  250.     // Generates an `index.html` file with the <script> injected.
  251.     new HtmlWebpackPlugin({
  252.       inject: true,
  253.       template: paths.appHtml,
  254.     }),
  255.     // Add module names to factory functions so they appear in browser profiler.
  256.     new webpack.NamedModulesPlugin(),
  257.     // Makes some environment variables available to the JS code, for example:
  258.     // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
  259.     new webpack.DefinePlugin(env.stringified),
  260.     // This is necessary to emit hot updates (currently CSS only):
  261.     new webpack.HotModuleReplacementPlugin(),
  262.     // Watcher doesn't work well if you mistype casing in a path so we use
  263.     // a plugin that prints an error when you attempt to do this.
  264.     // See https://github.com/facebookincubator/create-react-app/issues/240
  265.     new CaseSensitivePathsPlugin(),
  266.     // If you require a missing module and then `npm install` it, you still have
  267.     // to restart the development server for Webpack to discover it. This plugin
  268.     // makes the discovery automatic so you don't have to restart.
  269.     // See https://github.com/facebookincubator/create-react-app/issues/186
  270.     new WatchMissingNodeModulesPlugin(paths.appNodeModules),
  271.     // Moment.js is an extremely popular library that bundles large locale files
  272.     // by default due to how Webpack interprets its code. This is a practical
  273.     // solution that requires the user to opt into importing specific locales.
  274.     // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
  275.     // You can remove this if you don't use Moment.js:
  276.     new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  277.     new StyleLintPlugin({
  278.       configFile: '.stylelintrc',
  279.       context: 'src/ClientBundle/Resources/react-app/src',
  280.       files: '**/*.scss',
  281.       failOnError: false,
  282.       quiet: false,
  283.     }),
  284.   ],
  285.   // Some libraries import Node modules but don't use them in the browser.
  286.   // Tell Webpack to provide empty mocks for them so importing them works.
  287.   node: {
  288.     dgram: 'empty',
  289.     fs: 'empty',
  290.     net: 'empty',
  291.     tls: 'empty',
  292.   },
  293.   // Turn off performance hints during development because we don't do any
  294.   // splitting or minification in interest of speed. These warnings become
  295.   // cumbersome.
  296.   performance: {
  297.     hints: false,
  298.   },
  299. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement