blob: 02540552dda4bed47f2c7967bb37e554332295f3 [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
43 communityConsoleMain: './src/contentScripts/communityConsole/main.js',
44 communityConsoleStart: './src/contentScripts/communityConsole/start.js',
45 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
71 workflowManager: './src/workflows/manager/index.js',
72
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ínez3465e772021-07-11 19:18:41 +0200148 module: {
149 rules: [
150 {
avm999632485a3e2021-09-08 22:18:38 +0200151 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200152 type: 'json',
153 parser: {
154 parse: json5.parse,
155 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100156 use: [
157 preprocessorLoader,
158 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200159 },
avm999632485a3e2021-09-08 22:18:38 +0200160 {
161 test: /\.s[ac]ss$/i,
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200162 oneOf: [
avm999632485a3e2021-09-08 22:18:38 +0200163 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200164 resourceQuery: /string/,
165 use: [
166 'css-loader',
167 {
168 loader: 'sass-loader',
169 options: {
170 // Prefer `dart-sass`
171 implementation: require('sass'),
172 },
173 },
174 ],
175 },
176 {
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100177 resourceQuery: /asCSSFile/,
178 use: [
179 MiniCssExtractPlugin.loader,
180 'css-loader',
181 {
182 loader: 'sass-loader',
183 options: {
184 // Prefer `dart-sass`
185 implementation: require('sass'),
186 },
187 },
188 ],
189 },
190 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200191 use: [
192 'style-loader',
193 'css-loader',
194 {
195 loader: 'sass-loader',
196 options: {
197 // Prefer `dart-sass`
198 implementation: require('sass'),
199 },
200 },
201 ],
avm999632485a3e2021-09-08 22:18:38 +0200202 },
203 ],
204 },
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100205 {
206 test: /\.js$/i,
207 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100208 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100209 ],
210 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200211 ]
212 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200213 optimization: {
214 minimizer: [
215 new TerserPlugin({
216 terserOptions: {
217 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100218 ascii_only:
219 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200220 },
221 },
222 }),
223 ],
224 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200225 };
226};