|
| 1 | +import * as autoprefixer from 'autoprefixer'; |
| 2 | +import * as ExtractTextPlugin from 'extract-text-webpack-plugin'; |
| 3 | +import * as HtmlWebpackPlugin from 'html-webpack-plugin'; |
| 4 | +import * as InterpolateHtmlPlugin from 'react-dev-utils/InterpolateHtmlPlugin'; |
| 5 | +import * as Webpack from 'webpack'; |
| 6 | +import { Configuration } from 'webpack'; |
| 7 | +import * as ManifestPlugin from 'webpack-manifest-plugin'; |
| 8 | + |
| 9 | +import { getClientEnvironment } from './env'; |
| 10 | +import paths from './paths'; |
| 11 | + |
| 12 | +const publicPath = paths.servedPath; |
| 13 | +const shouldUseRelativeAssetPaths = publicPath === './'; |
| 14 | +const publicUrl = publicPath.slice(0, -1); |
| 15 | +const env = getClientEnvironment(publicUrl); |
| 16 | + |
| 17 | +if (env.stringified['process.env'].NODE_ENV !== '"production"') { |
| 18 | + throw new Error('Production builds must have NODE_ENV=production.'); |
| 19 | +} |
| 20 | + |
| 21 | +const cssFilename = 'static/css/[name].[contenthash:8].css'; |
| 22 | + |
| 23 | +const extractTextPluginOptions = shouldUseRelativeAssetPaths |
| 24 | + ? { publicPath: Array(cssFilename.split('/').length).join('../') } |
| 25 | + : {}; |
| 26 | + |
| 27 | +const config: Configuration = { |
| 28 | + bail: true, |
| 29 | + devtool: 'source-map', |
| 30 | + entry: [ |
| 31 | + require.resolve('./polyfills'), |
| 32 | + paths.appIndexJs |
| 33 | + ], |
| 34 | + output: { |
| 35 | + path: paths.appBuild, |
| 36 | + filename: 'static/js/[name].[chunkhash:8].js', |
| 37 | + chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js', |
| 38 | + publicPath |
| 39 | + }, |
| 40 | + resolve: { |
| 41 | + extensions: ['.ts', '.tsx', '.js', '.json', '.jsx'], |
| 42 | + modules: ['node_modules'].concat(paths.nodePaths) |
| 43 | + }, |
| 44 | + resolveLoader: { |
| 45 | + modules: [ |
| 46 | + paths.ownNodeModules, |
| 47 | + paths.appNodeModules |
| 48 | + ] |
| 49 | + }, |
| 50 | + module: { |
| 51 | + rules: [{ |
| 52 | + test: /\.(ts|tsx)$/, |
| 53 | + enforce: 'pre', |
| 54 | + use: [{ |
| 55 | + loader: 'tslint-loader' |
| 56 | + }], |
| 57 | + include: paths.appSrc |
| 58 | + }, { |
| 59 | + test: /\(.*)$/, |
| 60 | + exclude: [ |
| 61 | + /\.html$/, |
| 62 | + /\.(ts|tsx)$/, |
| 63 | + /\.css$/, |
| 64 | + /\.scss$/, |
| 65 | + /\.json$/, |
| 66 | + /\.svg$/ |
| 67 | + ], |
| 68 | + loader: 'url-loader', |
| 69 | + options: { |
| 70 | + limit: 10000, |
| 71 | + name: 'static/media/[name].[hash:8].[ext]' |
| 72 | + } |
| 73 | + }, { |
| 74 | + test: /\.(ts|tsx)$/, |
| 75 | + include: paths.appSrc, |
| 76 | + loader: 'ts-loader' |
| 77 | + }, { |
| 78 | + test: /\.css$/, |
| 79 | + loader: ExtractTextPlugin.extract(Object.assign({ |
| 80 | + fallback: 'style-loader', |
| 81 | + use: [ |
| 82 | + { |
| 83 | + loader: 'css-loader', |
| 84 | + options: { |
| 85 | + importLoaders: 1 |
| 86 | + } |
| 87 | + }, { |
| 88 | + loader: 'postcss-loader', |
| 89 | + options: { |
| 90 | + ident: 'postcss', |
| 91 | + plugins: (): any[] => [autoprefixer({ |
| 92 | + browsers: [ |
| 93 | + '>1%', |
| 94 | + 'last 4 versions', |
| 95 | + 'Firefox ESR', |
| 96 | + 'not ie < 9', |
| 97 | + ] |
| 98 | + })] |
| 99 | + } |
| 100 | + } |
| 101 | + ] |
| 102 | + }, extractTextPluginOptions)) |
| 103 | + // Note: this won't work without `new ExtractTextPlugin()` in `plugins`. |
| 104 | + }, { |
| 105 | + test: /\.svg$/, |
| 106 | + loader: 'file-loader', |
| 107 | + options: { |
| 108 | + name: 'static/media/[name].[hash:8].[ext]' |
| 109 | + } |
| 110 | + } |
| 111 | + ] |
| 112 | + }, |
| 113 | + plugins: [ |
| 114 | + new InterpolateHtmlPlugin(env.raw), |
| 115 | + // Generates an `index.html` file with the <script> injected. |
| 116 | + new HtmlWebpackPlugin({ |
| 117 | + inject: true, |
| 118 | + template: paths.appHtml, |
| 119 | + minify: { |
| 120 | + removeComments: true, |
| 121 | + collapseWhitespace: true, |
| 122 | + removeRedundantAttributes: true, |
| 123 | + useShortDoctype: true, |
| 124 | + removeEmptyAttributes: true, |
| 125 | + removeStyleLinkTypeAttributes: true, |
| 126 | + keepClosingSlash: true, |
| 127 | + minifyJS: true, |
| 128 | + minifyCSS: true, |
| 129 | + minifyURLs: true |
| 130 | + } |
| 131 | + }), |
| 132 | + // Makes some environment variables available to the JS code, for example: |
| 133 | + // if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`. |
| 134 | + // It is absolutely essential that NODE_ENV was set to production here. |
| 135 | + // Otherwise React will be compiled in the very slow development mode. |
| 136 | + new Webpack.DefinePlugin(env.stringified), |
| 137 | + // Minify the code. |
| 138 | + new Webpack.optimize.UglifyJsPlugin({ |
| 139 | + compress: { |
| 140 | + screw_ie8: true, // React doesn't support IE8 |
| 141 | + warnings: false |
| 142 | + }, |
| 143 | + mangle: { |
| 144 | + screw_ie8: true |
| 145 | + }, |
| 146 | + output: { |
| 147 | + comments: false, |
| 148 | + screw_ie8: true |
| 149 | + } as any, |
| 150 | + sourceMap: true |
| 151 | + }), |
| 152 | + new ExtractTextPlugin({ |
| 153 | + filename: cssFilename |
| 154 | + }), |
| 155 | + new ManifestPlugin({ |
| 156 | + fileName: 'asset-manifest.json' |
| 157 | + }) |
| 158 | + ], |
| 159 | + node: { |
| 160 | + fs: 'empty', |
| 161 | + net: 'empty', |
| 162 | + tls: 'empty' |
| 163 | + }, |
| 164 | +}; |
| 165 | + |
| 166 | +export default config; |
0 commit comments