blob: 36e83058c2918dd4c59dba3afc010ae973e6c8a6 [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',
avm9996337601bc2022-02-21 10:36:45 +010048 publicProfile: './src/contentScripts/publicProfile.js',
49 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020050 profileIndicator: './src/contentScripts/profileIndicator.js',
51
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020052 // Programatically injected content scripts
53 handleInstall: './src/contentScripts/communityConsole/handleInstall.js',
54 handleUpdate: './src/contentScripts/communityConsole/handleUpdate.js',
55
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020056 // Injected JS
57 profileIndicatorInject: './src/injections/profileIndicator.js',
58 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez4757ae82022-12-28 16:41:37 +010059 xhrInterceptorInject: './src/injections/xhrProxy.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010060 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010061 litComponentsInject: './src/injections/litComponentsInject.js',
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020062 updateHandlerLitComponents:
63 './src/injections/updateHandlerLitComponents.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020064
65 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020066 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020067
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020068 // Workflow manager
69 workflowManager: './src/workflows/manager/index.js',
70
avm999632485a3e2021-09-08 22:18:38 +020071 // Common CSS
72 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +010073
74 // Compiled Sass
75 ccDarkTheme: './src/ccDarkTheme/main.scss?asCSSFile',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020076 };
77
78 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010079 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020080
81 let outputPath = path.join(__dirname, 'dist', env.browser_target);
82
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020083 let overridenLocalePaths =
84 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
85
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010086 let preprocessorLoader = {
87 loader: 'webpack-preprocessor-loader',
88 options: {
89 params: {
90 browser_target: env.browser_target,
91 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010092 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010093 },
94 },
95 };
96
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020097 return {
98 entry,
99 output: {
100 filename: '[name].bundle.js',
101 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +0200102 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200103 },
104 plugins: [
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100105 new MiniCssExtractPlugin({
106 filename: '[name].bundle.css',
107 }),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200108 new WebpackShellPluginNext({
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100109 onBuildStart: {
110 scripts: ['make lit_localize_build'],
111 blocking: true,
112 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200113 onBuildEnd: {
114 scripts:
avm99963efce2102021-03-23 23:23:26 +0100115 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200116 path.join(outputPath, 'manifest.json') + ' ' +
117 env.browser_target]
118 }
119 }),
120 new CopyWebpackPlugin({
121 patterns: [
122 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100123 from: path.join(
124 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
125 to: path.join(outputPath, 'icons'),
126 },
127 ]
128 }),
129 new CopyWebpackPlugin({
130 patterns: [
131 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200132 from: path.join(__dirname, 'src/static'),
133 to: outputPath,
134 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200135 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200136 }
137 },
138 ]
139 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200140 new webpack.DefinePlugin({
141 'PRODUCTION': args.mode == 'production',
142 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200143 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200144 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100145 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200146 module: {
147 rules: [
148 {
avm999632485a3e2021-09-08 22:18:38 +0200149 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200150 type: 'json',
151 parser: {
152 parse: json5.parse,
153 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100154 use: [
155 preprocessorLoader,
156 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200157 },
avm999632485a3e2021-09-08 22:18:38 +0200158 {
159 test: /\.s[ac]ss$/i,
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200160 oneOf: [
avm999632485a3e2021-09-08 22:18:38 +0200161 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200162 resourceQuery: /string/,
163 use: [
164 'css-loader',
165 {
166 loader: 'sass-loader',
167 options: {
168 // Prefer `dart-sass`
169 implementation: require('sass'),
170 },
171 },
172 ],
173 },
174 {
Adrià Vilanova Martínezddd30042023-11-01 22:54:17 +0100175 resourceQuery: /asCSSFile/,
176 use: [
177 MiniCssExtractPlugin.loader,
178 'css-loader',
179 {
180 loader: 'sass-loader',
181 options: {
182 // Prefer `dart-sass`
183 implementation: require('sass'),
184 },
185 },
186 ],
187 },
188 {
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +0200189 use: [
190 'style-loader',
191 'css-loader',
192 {
193 loader: 'sass-loader',
194 options: {
195 // Prefer `dart-sass`
196 implementation: require('sass'),
197 },
198 },
199 ],
avm999632485a3e2021-09-08 22:18:38 +0200200 },
201 ],
202 },
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100203 {
204 test: /\.js$/i,
205 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100206 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100207 ],
208 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200209 ]
210 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200211 optimization: {
212 minimizer: [
213 new TerserPlugin({
214 terserOptions: {
215 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100216 ascii_only:
217 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200218 },
219 },
220 }),
221 ],
222 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200223 };
224};