blob: cf1e816322e3894d40dd681916ae3c38e8d57f6c [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
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020023module.exports = (env, args) => {
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +010024 // NOTE: When adding an entry, add the corresponding source map file to
25 // web_accessible_resources in //templates/manifest.gjson.
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +010026 const entry = {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020027 // Content scripts
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +020028 communityConsoleMain:
Adrià Vilanova Martínezf92172f2024-10-19 15:55:15 +020029 './src/entryPoints/communityConsole/contentScripts/main.ts',
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +020030 communityConsoleStart:
Adrià Vilanova Martínezf92172f2024-10-19 15:55:15 +020031 './src/entryPoints/communityConsole/contentScripts/start.ts',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020032 publicForum: './src/contentScripts/publicForum.js',
33 publicThread: './src/contentScripts/publicThread.js',
Adrià Vilanova Martínez80f6c9f2024-11-10 01:09:48 +010034 publicThreadStart: './src/entryPoints/twBasic/thread/start.ts',
Adrià Vilanova Martínez04ae1032024-02-07 23:42:25 +010035 publicGuide: './src/contentScripts/publicGuide.js',
36 publicGuideStart: './src/contentScripts/publicGuideStart.js',
avm9996337601bc2022-02-21 10:36:45 +010037 publicProfile: './src/contentScripts/publicProfile.js',
38 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020039 profileIndicator: './src/contentScripts/profileIndicator.js',
40
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020041 // Programatically injected content scripts
42 handleInstall: './src/contentScripts/communityConsole/handleInstall.js',
43 handleUpdate: './src/contentScripts/communityConsole/handleUpdate.js',
44
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020045 // Injected JS
46 profileIndicatorInject: './src/injections/profileIndicator.js',
47 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez9c418ab2024-12-05 15:34:40 +010048 xhrInterceptorInject: './src/entryPoints/communityConsole/injections/xhrProxy.ts',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010049 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010050 litComponentsInject: './src/injections/litComponentsInject.js',
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020051 updateHandlerLitComponents:
52 './src/injections/updateHandlerLitComponents.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020053
54 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020055 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020056
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020057 // Workflow manager
Adrià Vilanova Martínezc78c3ad2024-05-18 00:35:51 +020058 workflowManager: './src/features/workflows/core/manager/index.js',
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020059
avm999632485a3e2021-09-08 22:18:38 +020060 // Common CSS
61 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +010062
63 // Compiled Sass
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020064 ccDarkTheme: './src/features/ccDarkTheme/core/styles/main.scss?asCSSFile',
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +010065
66 // Background script (or service worker for MV3)
67 bg: './src/bg.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020068 };
69
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +010070 // NOTE: When adding an entry, add the corresponding source map file to
71 // web_accessible_resources in //templates/manifest.gjson.
72 const styles = [
73 {
74 origin: './src/features/enhancedAnnouncementsDot/ui/styles.css',
75 destination: 'css/enhanced_announcements_dot.css',
76 },
77 {
78 origin: './src/features/fixedToolbar/ui/styles.css',
79 destination: 'css/fixed_toolbar.css',
80 },
81 {
82 origin: './src/features/imageMaxHeight/ui/styles.css',
83 destination: 'css/image_max_height.css',
84 },
85 {
86 origin: './src/features/increaseContrast/ui/styles.css',
87 destination: 'css/increase_contrast.css',
88 },
89 {
90 origin: './src/features/repositionExpandThread/ui/styles.css',
91 destination: 'css/reposition_expand_thread.css',
92 },
93 {
94 origin: './src/features/stickySidebarHeaders/ui/styles.css',
95 destination: 'css/sticky_sidebar_headers.css',
96 },
97 ];
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020098
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +010099 const outputPath = path.join(__dirname, 'dist', env.browser_target);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200100
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +0100101 const overridenLocalePaths =
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200102 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
103
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100104 let preprocessorLoader = {
105 loader: 'webpack-preprocessor-loader',
106 options: {
107 params: {
108 browser_target: env.browser_target,
109 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +0100110 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100111 },
112 },
113 };
114
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200115 return {
116 entry,
117 output: {
118 filename: '[name].bundle.js',
119 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +0200120 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 },
122 plugins: [
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100123 new MiniCssExtractPlugin({
124 filename: '[name].bundle.css',
125 }),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200126 new WebpackShellPluginNext({
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100127 onBuildStart: {
128 scripts: ['make lit_localize_build'],
129 blocking: true,
130 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131 onBuildEnd: {
132 scripts:
avm99963efce2102021-03-23 23:23:26 +0100133 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200134 path.join(outputPath, 'manifest.json') + ' ' +
135 env.browser_target]
136 }
137 }),
138 new CopyWebpackPlugin({
139 patterns: [
140 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100141 from: path.join(
142 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
143 to: path.join(outputPath, 'icons'),
144 },
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100145 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200146 from: path.join(__dirname, 'src/static'),
147 to: outputPath,
148 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200149 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200150 }
151 },
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +0100152 ...getStylesCopyPatterns(styles),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200153 ]
154 }),
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +0200155 new HtmlWebpackPlugin({
156 filename: 'workflows.html',
Adrià Vilanova Martínezed1aba32024-10-26 21:23:12 +0200157 template:
158 'src/features/workflows/presentation/templates/workflows.html.ejs',
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +0200159 chunks: ['workflowManager'],
160 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200161 new webpack.DefinePlugin({
162 'PRODUCTION': args.mode == 'production',
163 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200164 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200165 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100166 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200167 resolve: {
168 extensions: ['.tsx', '.ts', '.js', '.json', '.wasm'],
169 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200170 module: {
171 rules: [
172 {
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200173 test: /\.js$/i,
174 use: [
175 preprocessorLoader,
176 ],
177 },
178 {
179 test: /\.tsx?$/,
180 use: [
181 'ts-loader',
182 preprocessorLoader,
183 ],
184 exclude: /node_modules/,
185 },
186 {
avm999632485a3e2021-09-08 22:18:38 +0200187 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200188 type: 'json',
189 parser: {
190 parse: json5.parse,
191 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100192 use: [
193 preprocessorLoader,
194 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200195 },
avm999632485a3e2021-09-08 22:18:38 +0200196 {
197 test: /\.s[ac]ss$/i,
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200198 oneOf: [
avm999632485a3e2021-09-08 22:18:38 +0200199 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200200 resourceQuery: /string/,
201 use: [
202 'css-loader',
203 {
204 loader: 'sass-loader',
205 options: {
206 // Prefer `dart-sass`
207 implementation: require('sass'),
208 },
209 },
210 ],
211 },
212 {
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100213 resourceQuery: /asCSSFile/,
214 use: [
215 MiniCssExtractPlugin.loader,
216 'css-loader',
217 {
218 loader: 'sass-loader',
219 options: {
220 // Prefer `dart-sass`
221 implementation: require('sass'),
222 },
223 },
224 ],
225 },
226 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200227 use: [
228 'style-loader',
229 'css-loader',
230 {
231 loader: 'sass-loader',
232 options: {
233 // Prefer `dart-sass`
234 implementation: require('sass'),
235 },
236 },
237 ],
avm999632485a3e2021-09-08 22:18:38 +0200238 },
239 ],
240 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200241 ]
242 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200243 optimization: {
244 minimizer: [
245 new TerserPlugin({
246 terserOptions: {
247 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100248 ascii_only:
249 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200250 },
251 },
252 }),
253 ],
254 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200255 };
256};
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +0100257
258const getCopyPluginsForOverridenLocales = outputPath => {
259 return localeOverrides.map(l => {
260 return new CopyWebpackPlugin({
261 patterns: [
262 {
263 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
264 to: path.join(outputPath, '_locales/' + l.webExtLocale),
265 globOptions: {
266 ignore: ['**/OWNERS', '**.md'],
267 }
268 },
269 ]
270 });
271 });
272};
273
274const getStylesCopyPatterns = styles => {
275 return styles.map(s => {
276 return {
277 from: path.join(__dirname, s.origin),
278 to: s.destination,
279 };
280 });
281}