Advertisement
Guest User

Untitled

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