blob: 033685f268895dc5a46592c9d4d39811284231a6 [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ínez6b560432022-10-23 20:26:17 +02005const TerserPlugin = require('terser-webpack-plugin');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02006const WebpackShellPluginNext = require('webpack-shell-plugin-next');
7
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +02008// Pontoon uses their own locale set. This array lets us convert those locales
9// to the ones accepted by Chrome, Firefox, etc.
10const localeOverrides = [
11 {
12 pontoonLocale: 'pt-rBR',
Adrià Vilanova Martínez7f9540d2022-02-17 22:05:14 +010013 webExtLocale: 'pt_BR',
14 },
15 {
16 pontoonLocale: 'pt-rBR',
17 webExtLocale: 'pt_PT',
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020018 },
19];
20
21const getCopyPluginsForOverridenLocales = outputPath => {
22 return localeOverrides.map(l => {
23 return new CopyWebpackPlugin({
24 patterns: [
25 {
26 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
27 to: path.join(outputPath, '_locales/' + l.webExtLocale),
28 globOptions: {
29 ignore: ['**/OWNERS', '**.md'],
30 }
31 },
32 ]
33 });
34 });
35};
36
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020037module.exports = (env, args) => {
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +010038 // NOTE: When adding an entry, add the corresponding source map file to
39 // web_accessible_resources in //templates/manifest.gjson.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020040 let entry = {
41 // Content scripts
42 communityConsoleMain: './src/contentScripts/communityConsole/main.js',
43 communityConsoleStart: './src/contentScripts/communityConsole/start.js',
44 publicForum: './src/contentScripts/publicForum.js',
45 publicThread: './src/contentScripts/publicThread.js',
Adrià Vilanova Martíneza2f79512022-07-10 15:32:32 +020046 publicThreadStart: './src/contentScripts/publicThreadStart.js',
avm9996337601bc2022-02-21 10:36:45 +010047 publicProfile: './src/contentScripts/publicProfile.js',
48 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020049 profileIndicator: './src/contentScripts/profileIndicator.js',
50
51 // Injected JS
52 profileIndicatorInject: './src/injections/profileIndicator.js',
53 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez4757ae82022-12-28 16:41:37 +010054 xhrInterceptorInject: './src/injections/xhrProxy.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010055 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010056 litComponentsInject: './src/injections/litComponentsInject.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020057
58 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020059 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020060
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020061 // Workflow manager
62 workflowManager: './src/workflows/manager/index.js',
63
avm999632485a3e2021-09-08 22:18:38 +020064 // Common CSS
65 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020066 };
67
68 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010069 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020070
71 let outputPath = path.join(__dirname, 'dist', env.browser_target);
72
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020073 let overridenLocalePaths =
74 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
75
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010076 let preprocessorLoader = {
77 loader: 'webpack-preprocessor-loader',
78 options: {
79 params: {
80 browser_target: env.browser_target,
81 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010082 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010083 },
84 },
85 };
86
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020087 return {
88 entry,
89 output: {
90 filename: '[name].bundle.js',
91 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +020092 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020093 },
94 plugins: [
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020095 new WebpackShellPluginNext({
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +010096 onBuildStart: {
97 scripts: ['make lit_localize_build'],
98 blocking: true,
99 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200100 onBuildEnd: {
101 scripts:
avm99963efce2102021-03-23 23:23:26 +0100102 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200103 path.join(outputPath, 'manifest.json') + ' ' +
104 env.browser_target]
105 }
106 }),
107 new CopyWebpackPlugin({
108 patterns: [
109 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100110 from: path.join(
111 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
112 to: path.join(outputPath, 'icons'),
113 },
114 ]
115 }),
116 new CopyWebpackPlugin({
117 patterns: [
118 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200119 from: path.join(__dirname, 'src/static'),
120 to: outputPath,
121 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200122 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123 }
124 },
125 ]
126 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200127 new webpack.DefinePlugin({
128 'PRODUCTION': args.mode == 'production',
129 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200130 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100132 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200133 module: {
134 rules: [
135 {
avm999632485a3e2021-09-08 22:18:38 +0200136 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200137 type: 'json',
138 parser: {
139 parse: json5.parse,
140 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100141 use: [
142 preprocessorLoader,
143 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200144 },
avm999632485a3e2021-09-08 22:18:38 +0200145 {
146 test: /\.s[ac]ss$/i,
147 use: [
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000148 'style-loader',
avm999632485a3e2021-09-08 22:18:38 +0200149 'css-loader',
150 {
151 loader: 'sass-loader',
152 options: {
153 // Prefer `dart-sass`
154 implementation: require('sass'),
155 },
156 },
157 ],
158 },
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100159 {
160 test: /\.js$/i,
161 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100162 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100163 ],
164 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200165 ]
166 },
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200167 optimization: {
168 minimizer: [
169 new TerserPlugin({
170 terserOptions: {
171 format: {
Adrià Vilanova Martíneza03d03a2023-03-11 23:30:07 +0100172 ascii_only:
173 true, // Due to https://iavm.xyz/b/twpowertools/145#c1
Adrià Vilanova Martínez6b560432022-10-23 20:26:17 +0200174 },
175 },
176 }),
177 ],
178 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200179 };
180};