blob: dbd9cccf0329f5ae27072ccda97f638fdfb2b855 [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ínezddd30042023-11-01 22:54:17 +01005const MiniCssExtractPlugin = require('mini-css-extract-plugin');
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +02006const TerserPlugin = require('terser-webpack-plugin');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02007const WebpackShellPluginNext = require('webpack-shell-plugin-next');
8
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +02009// Pontoon uses their own locale set. This array lets us convert those locales
10// to the ones accepted by Chrome, Firefox, etc.
11const localeOverrides = [
12 {
13 pontoonLocale: 'pt-rBR',
Adrià Vilanova Martínez7f9540d2022-02-17 22:05:14 +010014 webExtLocale: 'pt_BR',
15 },
16 {
17 pontoonLocale: 'pt-rBR',
18 webExtLocale: 'pt_PT',
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020019 },
20];
21
22const getCopyPluginsForOverridenLocales = outputPath => {
23 return localeOverrides.map(l => {
24 return new CopyWebpackPlugin({
25 patterns: [
26 {
27 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
28 to: path.join(outputPath, '_locales/' + l.webExtLocale),
29 globOptions: {
30 ignore: ['**/OWNERS', '**.md'],
31 }
32 },
33 ]
34 });
35 });
36};
37
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020038module.exports = (env, args) => {
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +010039 // NOTE: When adding an entry, add the corresponding source map file to
40 // web_accessible_resources in //templates/manifest.gjson.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020041 let entry = {
42 // Content scripts
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020043 communityConsoleMain: './src/platforms/communityConsole/entryPoints/main.ts',
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +020044 communityConsoleStart: './src/platforms/communityConsole/entryPoints/start.ts',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020045 publicForum: './src/contentScripts/publicForum.js',
46 publicThread: './src/contentScripts/publicThread.js',
Adrià Vilanova Martíneza2f79512022-07-10 15:32:32 +020047 publicThreadStart: './src/contentScripts/publicThreadStart.js',
Adrià Vilanova Martínez04ae1032024-02-07 23:42:25 +010048 publicGuide: './src/contentScripts/publicGuide.js',
49 publicGuideStart: './src/contentScripts/publicGuideStart.js',
avm9996337601bc2022-02-21 10:36:45 +010050 publicProfile: './src/contentScripts/publicProfile.js',
51 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020052 profileIndicator: './src/contentScripts/profileIndicator.js',
53
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020054 // Programatically injected content scripts
55 handleInstall: './src/contentScripts/communityConsole/handleInstall.js',
56 handleUpdate: './src/contentScripts/communityConsole/handleUpdate.js',
57
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020058 // Injected JS
59 profileIndicatorInject: './src/injections/profileIndicator.js',
60 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez4757ae82022-12-28 16:41:37 +010061 xhrInterceptorInject: './src/injections/xhrProxy.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010062 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010063 litComponentsInject: './src/injections/litComponentsInject.js',
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020064 updateHandlerLitComponents:
65 './src/injections/updateHandlerLitComponents.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020066
67 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020068 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020069
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020070 // Workflow manager
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020071 workflowManager: './src/features/workflows/core/manager/index.js',
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020072
avm999632485a3e2021-09-08 22:18:38 +020073 // Common CSS
74 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +010075
76 // Compiled Sass
77 ccDarkTheme: './src/ccDarkTheme/main.scss?asCSSFile',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020078 };
79
80 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010081 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020082
83 let outputPath = path.join(__dirname, 'dist', env.browser_target);
84
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020085 let overridenLocalePaths =
86 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
87
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010088 let preprocessorLoader = {
89 loader: 'webpack-preprocessor-loader',
90 options: {
91 params: {
92 browser_target: env.browser_target,
93 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010094 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010095 },
96 },
97 };
98
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020099 return {
100 entry,
101 output: {
102 filename: '[name].bundle.js',
103 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +0200104 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200105 },
106 plugins: [
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100107 new MiniCssExtractPlugin({
108 filename: '[name].bundle.css',
109 }),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200110 new WebpackShellPluginNext({
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100111 onBuildStart: {
112 scripts: ['make lit_localize_build'],
113 blocking: true,
114 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200115 onBuildEnd: {
116 scripts:
avm99963efce2102021-03-23 23:23:26 +0100117 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200118 path.join(outputPath, 'manifest.json') + ' ' +
119 env.browser_target]
120 }
121 }),
122 new CopyWebpackPlugin({
123 patterns: [
124 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100125 from: path.join(
126 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
127 to: path.join(outputPath, 'icons'),
128 },
129 ]
130 }),
131 new CopyWebpackPlugin({
132 patterns: [
133 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200134 from: path.join(__dirname, 'src/static'),
135 to: outputPath,
136 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200137 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 }
139 },
140 ]
141 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200142 new webpack.DefinePlugin({
143 'PRODUCTION': args.mode == 'production',
144 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200145 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200146 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100147 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200148 resolve: {
149 extensions: ['.tsx', '.ts', '.js', '.json', '.wasm'],
150 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200151 module: {
152 rules: [
153 {
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200154 test: /\.js$/i,
155 use: [
156 preprocessorLoader,
157 ],
158 },
159 {
160 test: /\.tsx?$/,
161 use: [
162 'ts-loader',
163 preprocessorLoader,
164 ],
165 exclude: /node_modules/,
166 },
167 {
avm999632485a3e2021-09-08 22:18:38 +0200168 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200169 type: 'json',
170 parser: {
171 parse: json5.parse,
172 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100173 use: [
174 preprocessorLoader,
175 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200176 },
avm999632485a3e2021-09-08 22:18:38 +0200177 {
178 test: /\.s[ac]ss$/i,
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200179 oneOf: [
avm999632485a3e2021-09-08 22:18:38 +0200180 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200181 resourceQuery: /string/,
182 use: [
183 'css-loader',
184 {
185 loader: 'sass-loader',
186 options: {
187 // Prefer `dart-sass`
188 implementation: require('sass'),
189 },
190 },
191 ],
192 },
193 {
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100194 resourceQuery: /asCSSFile/,
195 use: [
196 MiniCssExtractPlugin.loader,
197 'css-loader',
198 {
199 loader: 'sass-loader',
200 options: {
201 // Prefer `dart-sass`
202 implementation: require('sass'),
203 },
204 },
205 ],
206 },
207 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200208 use: [
209 'style-loader',
210 'css-loader',
211 {
212 loader: 'sass-loader',
213 options: {
214 // Prefer `dart-sass`
215 implementation: require('sass'),
216 },
217 },
218 ],
avm999632485a3e2021-09-08 22:18:38 +0200219 },
220 ],
221 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200222 ]
223 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200224 optimization: {
225 minimizer: [
226 new TerserPlugin({
227 terserOptions: {
228 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100229 ascii_only:
230 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200231 },
232 },
233 }),
234 ],
235 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200236 };
237};