Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | 'use strict'; |
| 18 | |
| 19 | const webpack = require('webpack'); |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 20 | const {merge} = require('webpack-merge'); |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 21 | const path = require('path'); |
| 22 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); |
| 23 | const MiniCSSExtractPlugin = require('mini-css-extract-plugin'); |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 24 | const CompressionPlugin = require('compression-webpack-plugin'); |
| 25 | const commonConfig = require('./webpack.config.common'); |
| 26 | const isProd = process.env.NODE_ENV === 'production'; |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 27 | const environment = |
| 28 | isProd ? require('./env/prod.env') : require('./env/staging.env'); |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 29 | |
| 30 | |
| 31 | const webpackConfig = merge(commonConfig, { |
| 32 | mode: 'production', |
| 33 | output: { |
| 34 | path: path.resolve(__dirname, 'dist'), |
| 35 | publicPath: '/', |
| 36 | filename: 'js/[hash].js', |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 37 | chunkFilename: 'js/[id].[hash].chunk.js', |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 38 | }, |
| 39 | optimization: { |
| 40 | runtimeChunk: 'single', |
| 41 | minimizer: [ |
| 42 | new OptimizeCSSAssetsPlugin({ |
| 43 | cssProcessorPluginOptions: { |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 44 | preset: ['default', {discardComments: {removeAll: true}}], |
| 45 | }, |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 46 | }), |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 47 | ], |
| 48 | splitChunks: { |
| 49 | chunks: 'all', |
| 50 | maxInitialRequests: Infinity, |
| 51 | minSize: 0, |
| 52 | cacheGroups: { |
| 53 | vendor: { |
| 54 | test: /[\\/]node_modules[\\/]/, |
| 55 | name(module) { |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 56 | const packageName = module.context |
| 57 | .match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]; |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 58 | return `npm.${packageName.replace('@', '')}`; |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 59 | }, |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 60 | }, |
| 61 | styles: { |
| 62 | test: /\.css$/, |
| 63 | name: 'styles', |
| 64 | chunks: 'all', |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 65 | enforce: true, |
| 66 | }, |
| 67 | }, |
| 68 | }, |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 69 | }, |
| 70 | plugins: [ |
| 71 | new webpack.EnvironmentPlugin(environment), |
| 72 | new MiniCSSExtractPlugin({ |
| 73 | filename: 'css/[name].[hash].css', |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 74 | chunkFilename: 'css/[id].[hash].css', |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 75 | }), |
| 76 | new CompressionPlugin({ |
| 77 | filename: '[path].gz[query]', |
| 78 | algorithm: 'gzip', |
| 79 | test: new RegExp('\\.(js|css)$'), |
| 80 | threshold: 10240, |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 81 | minRatio: 0.8, |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 82 | }), |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 83 | new webpack.HashedModuleIdsPlugin(), |
| 84 | ], |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 85 | }); |
| 86 | |
| 87 | if (!isProd) { |
| 88 | webpackConfig.devtool = 'source-map'; |
| 89 | |
| 90 | if (process.env.npm_config_report) { |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 91 | const BundleAnalyzerPlugin = |
| 92 | require('webpack-bundle-analyzer').BundleAnalyzerPlugin; |
Pablo Gamito | 3f23913 | 2020-07-17 12:33:32 +0100 | [diff] [blame] | 93 | webpackConfig.plugins.push(new BundleAnalyzerPlugin()); |
| 94 | } |
| 95 | } |
| 96 | |
Pablo Gamito | 9d56e69 | 2020-09-02 13:04:28 +0200 | [diff] [blame] | 97 | module.exports = webpackConfig; |