blob: bceb4c04b295a08945d4fbb7aaef88c3a5ca52af [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');
5const WebpackShellPluginNext = require('webpack-shell-plugin-next');
6
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +02007// Pontoon uses their own locale set. This array lets us convert those locales
8// to the ones accepted by Chrome, Firefox, etc.
9const localeOverrides = [
10 {
11 pontoonLocale: 'pt-rBR',
Adrià Vilanova Martínez7f9540d2022-02-17 22:05:14 +010012 webExtLocale: 'pt_BR',
13 },
14 {
15 pontoonLocale: 'pt-rBR',
16 webExtLocale: 'pt_PT',
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020017 },
18];
19
20const getCopyPluginsForOverridenLocales = outputPath => {
21 return localeOverrides.map(l => {
22 return new CopyWebpackPlugin({
23 patterns: [
24 {
25 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
26 to: path.join(outputPath, '_locales/' + l.webExtLocale),
27 globOptions: {
28 ignore: ['**/OWNERS', '**.md'],
29 }
30 },
31 ]
32 });
33 });
34};
35
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020036module.exports = (env, args) => {
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +010037 // NOTE: When adding an entry, add the corresponding source map file to
38 // web_accessible_resources in //templates/manifest.gjson.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020039 let entry = {
40 // Content scripts
41 communityConsoleMain: './src/contentScripts/communityConsole/main.js',
42 communityConsoleStart: './src/contentScripts/communityConsole/start.js',
43 publicForum: './src/contentScripts/publicForum.js',
44 publicThread: './src/contentScripts/publicThread.js',
avm9996337601bc2022-02-21 10:36:45 +010045 publicProfile: './src/contentScripts/publicProfile.js',
46 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020047 profileIndicator: './src/contentScripts/profileIndicator.js',
48
49 // Injected JS
50 profileIndicatorInject: './src/injections/profileIndicator.js',
51 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020052 xhrInterceptorInject: './src/injections/xhrInterceptor.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010053 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020054
55 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020056 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020057
58 // Common CSS
59 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020060 };
61
62 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010063 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020064
65 let outputPath = path.join(__dirname, 'dist', env.browser_target);
66
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020067 let overridenLocalePaths =
68 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
69
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010070 let preprocessorLoader = {
71 loader: 'webpack-preprocessor-loader',
72 options: {
73 params: {
74 browser_target: env.browser_target,
75 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010076 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010077 },
78 },
79 };
80
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020081 return {
82 entry,
83 output: {
84 filename: '[name].bundle.js',
85 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +020086 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020087 },
88 plugins: [
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020089 new WebpackShellPluginNext({
90 onBuildEnd: {
91 scripts:
avm99963efce2102021-03-23 23:23:26 +010092 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020093 path.join(outputPath, 'manifest.json') + ' ' +
94 env.browser_target]
95 }
96 }),
97 new CopyWebpackPlugin({
98 patterns: [
99 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100100 from: path.join(
101 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
102 to: path.join(outputPath, 'icons'),
103 },
104 ]
105 }),
106 new CopyWebpackPlugin({
107 patterns: [
108 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200109 from: path.join(__dirname, 'src/static'),
110 to: outputPath,
111 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200112 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200113 }
114 },
115 ]
116 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200117 new webpack.DefinePlugin({
118 'PRODUCTION': args.mode == 'production',
119 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200120 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100122 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123 module: {
124 rules: [
125 {
avm999632485a3e2021-09-08 22:18:38 +0200126 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200127 type: 'json',
128 parser: {
129 parse: json5.parse,
130 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100131 use: [
132 preprocessorLoader,
133 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200134 },
avm999632485a3e2021-09-08 22:18:38 +0200135 {
136 test: /\.s[ac]ss$/i,
137 use: [
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000138 'style-loader',
avm999632485a3e2021-09-08 22:18:38 +0200139 'css-loader',
140 {
141 loader: 'sass-loader',
142 options: {
143 // Prefer `dart-sass`
144 implementation: require('sass'),
145 },
146 },
147 ],
148 },
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100149 {
150 test: /\.js$/i,
151 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100152 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100153 ],
154 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200155 ]
156 },
157 };
158};