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