blob: 0413c5f8d18351245f29304e99c98081805d4f7a [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:
Adrià Vilanova Martínezf92172f2024-10-19 15:55:15 +020045 './src/entryPoints/communityConsole/contentScripts/main.ts',
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +020046 communityConsoleStart:
Adrià Vilanova Martínezf92172f2024-10-19 15:55:15 +020047 './src/entryPoints/communityConsole/contentScripts/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ínez80f6c9f2024-11-10 01:09:48 +010050 publicThreadStart: './src/entryPoints/twBasic/thread/start.ts',
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',
Adrià Vilanova Martínezed1aba32024-10-26 21:23:12 +0200147 template:
148 'src/features/workflows/presentation/templates/workflows.html.ejs',
Adrià Vilanova Martínez39f48f82024-09-29 17:55:04 +0200149 chunks: ['workflowManager'],
150 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200151 new webpack.DefinePlugin({
152 'PRODUCTION': args.mode == 'production',
153 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200154 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200155 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100156 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200157 resolve: {
158 extensions: ['.tsx', '.ts', '.js', '.json', '.wasm'],
159 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200160 module: {
161 rules: [
162 {
Adrià Vilanova Martínezbbc8d7c2024-04-21 16:28:05 +0200163 test: /\.js$/i,
164 use: [
165 preprocessorLoader,
166 ],
167 },
168 {
169 test: /\.tsx?$/,
170 use: [
171 'ts-loader',
172 preprocessorLoader,
173 ],
174 exclude: /node_modules/,
175 },
176 {
avm999632485a3e2021-09-08 22:18:38 +0200177 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200178 type: 'json',
179 parser: {
180 parse: json5.parse,
181 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100182 use: [
183 preprocessorLoader,
184 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200185 },
avm999632485a3e2021-09-08 22:18:38 +0200186 {
187 test: /\.s[ac]ss$/i,
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200188 oneOf: [
avm999632485a3e2021-09-08 22:18:38 +0200189 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200190 resourceQuery: /string/,
191 use: [
192 'css-loader',
193 {
194 loader: 'sass-loader',
195 options: {
196 // Prefer `dart-sass`
197 implementation: require('sass'),
198 },
199 },
200 ],
201 },
202 {
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100203 resourceQuery: /asCSSFile/,
204 use: [
205 MiniCssExtractPlugin.loader,
206 'css-loader',
207 {
208 loader: 'sass-loader',
209 options: {
210 // Prefer `dart-sass`
211 implementation: require('sass'),
212 },
213 },
214 ],
215 },
216 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200217 use: [
218 'style-loader',
219 'css-loader',
220 {
221 loader: 'sass-loader',
222 options: {
223 // Prefer `dart-sass`
224 implementation: require('sass'),
225 },
226 },
227 ],
avm999632485a3e2021-09-08 22:18:38 +0200228 },
229 ],
230 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200231 ]
232 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200233 optimization: {
234 minimizer: [
235 new TerserPlugin({
236 terserOptions: {
237 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100238 ascii_only:
239 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200240 },
241 },
242 }),
243 ],
244 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200245 };
246};