blob: 5b7b49fe6bcaf8059b96671eac0698e1fa094670 [file] [log] [blame]
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02001const webpack = require('webpack');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02002const path = require('path');
3const json5 = require('json5');
4const CopyWebpackPlugin = require('copy-webpack-plugin');
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +02005const HtmlWebpackPlugin = require('html-webpack-plugin');
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +01006const MiniCssExtractPlugin = require('mini-css-extract-plugin');
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +02007const TerserPlugin = require('terser-webpack-plugin');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02008const WebpackShellPluginNext = require('webpack-shell-plugin-next');
9
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020010// Pontoon uses their own locale set. This array lets us convert those locales
11// to the ones accepted by Chrome, Firefox, etc.
12const localeOverrides = [
13 {
14 pontoonLocale: 'pt-rBR',
Adrià Vilanova Martínez7f9540d2022-02-17 22:05:14 +010015 webExtLocale: 'pt_BR',
16 },
17 {
18 pontoonLocale: 'pt-rBR',
19 webExtLocale: 'pt_PT',
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020020 },
21];
22
23const getCopyPluginsForOverridenLocales = outputPath => {
24 return localeOverrides.map(l => {
25 return new CopyWebpackPlugin({
26 patterns: [
27 {
28 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
29 to: path.join(outputPath, '_locales/' + l.webExtLocale),
30 globOptions: {
31 ignore: ['**/OWNERS', '**.md'],
32 }
33 },
34 ]
35 });
36 });
37};
38
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020039module.exports = (env, args) => {
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +010040 // NOTE: When adding an entry, add the corresponding source map file to
41 // web_accessible_resources in //templates/manifest.gjson.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020042 let entry = {
43 // Content scripts
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +020044 communityConsoleMain:
45 './src/platforms/communityConsole/entryPoints/main.ts',
46 communityConsoleStart:
47 './src/platforms/communityConsole/entryPoints/start.ts',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020048 publicForum: './src/contentScripts/publicForum.js',
49 publicThread: './src/contentScripts/publicThread.js',
Adrià Vilanova Martíneza2f79512022-07-10 15:32:32 +020050 publicThreadStart: './src/contentScripts/publicThreadStart.js',
Adrià Vilanova Martínez04ae1032024-02-07 23:42:25 +010051 publicGuide: './src/contentScripts/publicGuide.js',
52 publicGuideStart: './src/contentScripts/publicGuideStart.js',
avm9996337601bc2022-02-21 10:36:45 +010053 publicProfile: './src/contentScripts/publicProfile.js',
54 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020055 profileIndicator: './src/contentScripts/profileIndicator.js',
56
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020057 // Programatically injected content scripts
58 handleInstall: './src/contentScripts/communityConsole/handleInstall.js',
59 handleUpdate: './src/contentScripts/communityConsole/handleUpdate.js',
60
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020061 // Injected JS
62 profileIndicatorInject: './src/injections/profileIndicator.js',
63 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez4757ae82022-12-28 16:41:37 +010064 xhrInterceptorInject: './src/injections/xhrProxy.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010065 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010066 litComponentsInject: './src/injections/litComponentsInject.js',
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020067 updateHandlerLitComponents:
68 './src/injections/updateHandlerLitComponents.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020069
70 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020071 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020072
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020073 // Workflow manager
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020074 workflowManager: './src/features/workflows/core/manager/index.js',
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020075
avm999632485a3e2021-09-08 22:18:38 +020076 // Common CSS
77 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +010078
79 // Compiled Sass
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020080 ccDarkTheme: './src/features/ccDarkTheme/core/styles/main.scss?asCSSFile',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020081 };
82
83 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010084 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020085
86 let outputPath = path.join(__dirname, 'dist', env.browser_target);
87
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020088 let overridenLocalePaths =
89 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
90
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010091 let preprocessorLoader = {
92 loader: 'webpack-preprocessor-loader',
93 options: {
94 params: {
95 browser_target: env.browser_target,
96 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010097 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010098 },
99 },
100 };
101
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200102 return {
103 entry,
104 output: {
105 filename: '[name].bundle.js',
106 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +0200107 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200108 },
109 plugins: [
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100110 new MiniCssExtractPlugin({
111 filename: '[name].bundle.css',
112 }),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200113 new WebpackShellPluginNext({
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100114 onBuildStart: {
115 scripts: ['make lit_localize_build'],
116 blocking: true,
117 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200118 onBuildEnd: {
119 scripts:
avm99963efce2102021-03-23 23:23:26 +0100120 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 path.join(outputPath, 'manifest.json') + ' ' +
122 env.browser_target]
123 }
124 }),
125 new CopyWebpackPlugin({
126 patterns: [
127 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100128 from: path.join(
129 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
130 to: path.join(outputPath, 'icons'),
131 },
132 ]
133 }),
134 new CopyWebpackPlugin({
135 patterns: [
136 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200137 from: path.join(__dirname, 'src/static'),
138 to: outputPath,
139 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200140 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200141 }
142 },
143 ]
144 }),
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +0200145 new HtmlWebpackPlugin({
146 filename: 'workflows.html',
147 template: 'src/features/workflows/templates/workflows.html.ejs',
148 chunks: ['workflowManager'],
149 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200150 new webpack.DefinePlugin({
151 'PRODUCTION': args.mode == 'production',
152 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200153 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200154 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100155 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200156 resolve: {
157 extensions: ['.tsx', '.ts', '.js', '.json', '.wasm'],
158 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200159 module: {
160 rules: [
161 {
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200162 test: /\.js$/i,
163 use: [
164 preprocessorLoader,
165 ],
166 },
167 {
168 test: /\.tsx?$/,
169 use: [
170 'ts-loader',
171 preprocessorLoader,
172 ],
173 exclude: /node_modules/,
174 },
175 {
avm999632485a3e2021-09-08 22:18:38 +0200176 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200177 type: 'json',
178 parser: {
179 parse: json5.parse,
180 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100181 use: [
182 preprocessorLoader,
183 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200184 },
avm999632485a3e2021-09-08 22:18:38 +0200185 {
186 test: /\.s[ac]ss$/i,
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200187 oneOf: [
avm999632485a3e2021-09-08 22:18:38 +0200188 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200189 resourceQuery: /string/,
190 use: [
191 'css-loader',
192 {
193 loader: 'sass-loader',
194 options: {
195 // Prefer `dart-sass`
196 implementation: require('sass'),
197 },
198 },
199 ],
200 },
201 {
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100202 resourceQuery: /asCSSFile/,
203 use: [
204 MiniCssExtractPlugin.loader,
205 'css-loader',
206 {
207 loader: 'sass-loader',
208 options: {
209 // Prefer `dart-sass`
210 implementation: require('sass'),
211 },
212 },
213 ],
214 },
215 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200216 use: [
217 'style-loader',
218 'css-loader',
219 {
220 loader: 'sass-loader',
221 options: {
222 // Prefer `dart-sass`
223 implementation: require('sass'),
224 },
225 },
226 ],
avm999632485a3e2021-09-08 22:18:38 +0200227 },
228 ],
229 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200230 ]
231 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200232 optimization: {
233 minimizer: [
234 new TerserPlugin({
235 terserOptions: {
236 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100237 ascii_only:
238 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200239 },
240 },
241 }),
242 ],
243 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200244 };
245};