Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | const path = require('path'); |
| 6 | const webpack = require('webpack'); |
| 7 | const BundleAnalyzerPlugin = |
| 8 | require('webpack-bundle-analyzer').BundleAnalyzerPlugin; |
| 9 | const HtmlWebpackPlugin = require('html-webpack-plugin'); |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 10 | const CircularDependencyPlugin = require('circular-dependency-plugin'); |
| 11 | |
| 12 | const config = { |
| 13 | entry: { |
| 14 | 'mr-app': './static_src/elements/mr-app/mr-app.js', |
| 15 | 'mr-profile-page': |
| 16 | './static_src/elements/chdir/mr-profile-page/mr-profile-page.js', |
| 17 | 'ezt-element-package': './static_src/elements/ezt/ezt-element-package.js', |
| 18 | 'ezt-footer-scripts-package': |
| 19 | './static_src/elements/ezt/ezt-footer-scripts-package.js', |
| 20 | }, |
| 21 | devtool: 'eval-source-map', |
| 22 | plugins: [ |
| 23 | new CircularDependencyPlugin({ |
| 24 | // exclude detection of files based on a RegExp |
| 25 | exclude: /a\.js|node_modules/, |
| 26 | // add errors to webpack instead of warnings |
| 27 | failOnError: true, |
| 28 | // set the current working directory for displaying module paths |
| 29 | cwd: process.cwd(), |
| 30 | }), |
| 31 | new HtmlWebpackPlugin({ |
| 32 | chunks: ['mr-app'], |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 33 | inject: false, |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 34 | template: 'static_src/webpacked-scripts-template.html', |
| 35 | filename: '../../templates/webpack-out/mr-app.ezt', |
| 36 | }), |
| 37 | new HtmlWebpackPlugin({ |
| 38 | chunks: ['mr-profile-page'], |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 39 | inject: false, |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 40 | template: 'static_src/webpacked-scripts-template.html', |
| 41 | filename: '../../templates/webpack-out/mr-profile-page.ezt', |
| 42 | }), |
| 43 | new HtmlWebpackPlugin({ |
| 44 | chunks: ['ezt-element-package'], |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 45 | inject: false, |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 46 | template: 'static_src/webpacked-scripts-template.html', |
| 47 | filename: '../../templates/webpack-out/ezt-element-package.ezt', |
| 48 | }), |
| 49 | new HtmlWebpackPlugin({ |
| 50 | chunks: ['ezt-footer-scripts-package'], |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 51 | inject: false, |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 52 | template: 'static_src/webpacked-scripts-template.html', |
| 53 | filename: '../../templates/webpack-out/ezt-footer-scripts-package.ezt', |
| 54 | }), |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 55 | ], |
| 56 | module: { |
| 57 | rules: [ |
| 58 | { |
| 59 | test: /\.(ts|tsx)$/, |
| 60 | exclude: /node_modules/, |
| 61 | use: ['babel-loader'], |
| 62 | }, |
| 63 | { |
| 64 | test: /\.css$/i, |
| 65 | use: [ |
| 66 | {loader: 'style-loader', options: {injectType: 'styleTag'}}, |
| 67 | { |
| 68 | loader: 'css-loader', |
| 69 | options: { |
| 70 | modules: true, |
| 71 | importLoaders: 1, |
| 72 | }, |
| 73 | }, |
| 74 | 'postcss-loader', |
| 75 | ], |
| 76 | }, |
| 77 | ], |
| 78 | }, |
| 79 | resolve: { |
| 80 | extensions: ['.js', '.ts', '.tsx'], |
| 81 | modules: ['node_modules', 'static_src'], |
| 82 | }, |
| 83 | output: { |
| 84 | filename: '[name].[contenthash].min.js', |
| 85 | path: path.resolve(__dirname, 'static/dist'), |
| 86 | // __webpack_public_path__ is used to dynamically set the public path to |
| 87 | // the App Engine version URL. |
| 88 | publicPath: '/static/dist/', |
| 89 | }, |
| 90 | externals: { |
| 91 | moment: 'moment', |
| 92 | }, |
| 93 | }; |
| 94 | |
| 95 | module.exports = (env, argv) => { |
| 96 | if (argv.mode === 'production') { |
| 97 | // Settings for deploying JS to production. |
| 98 | config.devtool = 'source-map'; |
| 99 | |
| 100 | config.plugins = config.plugins.concat([ |
| 101 | new webpack.DefinePlugin( |
| 102 | {'process.env.NODE_ENV': '"production"'}, |
| 103 | ), |
| 104 | ]); |
| 105 | } |
| 106 | |
| 107 | if (argv.analyze) { |
| 108 | config.plugins.push(new BundleAnalyzerPlugin()); |
| 109 | } |
| 110 | return config; |
| 111 | }; |