blob: 33f7a8d8229f1b61986e2334f6e52616dbd6d873 [file] [log] [blame]
Pablo Gamito3f239132020-07-17 12:33:32 +01001/*
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
19const webpack = require('webpack');
Pablo Gamito9d56e692020-09-02 13:04:28 +020020const {merge} = require('webpack-merge');
Pablo Gamito3f239132020-07-17 12:33:32 +010021const path = require('path');
22const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
23const MiniCSSExtractPlugin = require('mini-css-extract-plugin');
Pablo Gamito3f239132020-07-17 12:33:32 +010024const CompressionPlugin = require('compression-webpack-plugin');
25const commonConfig = require('./webpack.config.common');
26const isProd = process.env.NODE_ENV === 'production';
Pablo Gamito9d56e692020-09-02 13:04:28 +020027const environment =
28 isProd ? require('./env/prod.env') : require('./env/staging.env');
Pablo Gamito3f239132020-07-17 12:33:32 +010029
30
31const webpackConfig = merge(commonConfig, {
32 mode: 'production',
33 output: {
34 path: path.resolve(__dirname, 'dist'),
35 publicPath: '/',
36 filename: 'js/[hash].js',
Pablo Gamito9d56e692020-09-02 13:04:28 +020037 chunkFilename: 'js/[id].[hash].chunk.js',
Pablo Gamito3f239132020-07-17 12:33:32 +010038 },
39 optimization: {
40 runtimeChunk: 'single',
41 minimizer: [
42 new OptimizeCSSAssetsPlugin({
43 cssProcessorPluginOptions: {
Pablo Gamito9d56e692020-09-02 13:04:28 +020044 preset: ['default', {discardComments: {removeAll: true}}],
45 },
Pablo Gamito3f239132020-07-17 12:33:32 +010046 }),
Pablo Gamito3f239132020-07-17 12:33:32 +010047 ],
48 splitChunks: {
49 chunks: 'all',
50 maxInitialRequests: Infinity,
51 minSize: 0,
52 cacheGroups: {
53 vendor: {
54 test: /[\\/]node_modules[\\/]/,
55 name(module) {
Pablo Gamito9d56e692020-09-02 13:04:28 +020056 const packageName = module.context
57 .match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
Pablo Gamito3f239132020-07-17 12:33:32 +010058 return `npm.${packageName.replace('@', '')}`;
Pablo Gamito9d56e692020-09-02 13:04:28 +020059 },
Pablo Gamito3f239132020-07-17 12:33:32 +010060 },
61 styles: {
62 test: /\.css$/,
63 name: 'styles',
64 chunks: 'all',
Pablo Gamito9d56e692020-09-02 13:04:28 +020065 enforce: true,
66 },
67 },
68 },
Pablo Gamito3f239132020-07-17 12:33:32 +010069 },
70 plugins: [
71 new webpack.EnvironmentPlugin(environment),
72 new MiniCSSExtractPlugin({
73 filename: 'css/[name].[hash].css',
Pablo Gamito9d56e692020-09-02 13:04:28 +020074 chunkFilename: 'css/[id].[hash].css',
Pablo Gamito3f239132020-07-17 12:33:32 +010075 }),
76 new CompressionPlugin({
77 filename: '[path].gz[query]',
78 algorithm: 'gzip',
79 test: new RegExp('\\.(js|css)$'),
80 threshold: 10240,
Pablo Gamito9d56e692020-09-02 13:04:28 +020081 minRatio: 0.8,
Pablo Gamito3f239132020-07-17 12:33:32 +010082 }),
Pablo Gamito9d56e692020-09-02 13:04:28 +020083 new webpack.HashedModuleIdsPlugin(),
84 ],
Pablo Gamito3f239132020-07-17 12:33:32 +010085});
86
87if (!isProd) {
88 webpackConfig.devtool = 'source-map';
89
90 if (process.env.npm_config_report) {
Pablo Gamito9d56e692020-09-02 13:04:28 +020091 const BundleAnalyzerPlugin =
92 require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
Pablo Gamito3f239132020-07-17 12:33:32 +010093 webpackConfig.plugins.push(new BundleAnalyzerPlugin());
94 }
95}
96
Pablo Gamito9d56e692020-09-02 13:04:28 +020097module.exports = webpackConfig;