blob: 05542b9eb4b58d7ea8ee4a919468d5fce3ed2905 [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',
12 webExtLocale: 'pt', // This way it targets both 'pt_BR' and 'pt_PT'
13 },
14];
15
16const getCopyPluginsForOverridenLocales = outputPath => {
17 return localeOverrides.map(l => {
18 return new CopyWebpackPlugin({
19 patterns: [
20 {
21 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
22 to: path.join(outputPath, '_locales/' + l.webExtLocale),
23 globOptions: {
24 ignore: ['**/OWNERS', '**.md'],
25 }
26 },
27 ]
28 });
29 });
30};
31
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020032module.exports = (env, args) => {
33 let entry = {
34 // Content scripts
35 communityConsoleMain: './src/contentScripts/communityConsole/main.js',
36 communityConsoleStart: './src/contentScripts/communityConsole/start.js',
37 publicForum: './src/contentScripts/publicForum.js',
38 publicThread: './src/contentScripts/publicThread.js',
39 profile: './src/contentScripts/profile.js',
40 profileIndicator: './src/contentScripts/profileIndicator.js',
41
42 // Injected JS
43 profileIndicatorInject: './src/injections/profileIndicator.js',
44 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020045 xhrInterceptorInject: './src/injections/xhrInterceptor.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020046
47 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020048 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020049
50 // Common CSS
51 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020052 };
53
54 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010055 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020056
57 let outputPath = path.join(__dirname, 'dist', env.browser_target);
58
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020059 let overridenLocalePaths =
60 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
61
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010062 let preprocessorLoader = {
63 loader: 'webpack-preprocessor-loader',
64 options: {
65 params: {
66 browser_target: env.browser_target,
67 production: args.mode == 'production',
68 },
69 },
70 };
71
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020072 return {
73 entry,
74 output: {
75 filename: '[name].bundle.js',
76 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +020077 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020078 },
79 plugins: [
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020080 new WebpackShellPluginNext({
81 onBuildEnd: {
82 scripts:
avm99963efce2102021-03-23 23:23:26 +010083 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020084 path.join(outputPath, 'manifest.json') + ' ' +
85 env.browser_target]
86 }
87 }),
88 new CopyWebpackPlugin({
89 patterns: [
90 {
91 from: path.join(__dirname, 'src/static'),
92 to: outputPath,
93 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020094 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020095 }
96 },
97 ]
98 }),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020099 new webpack.DefinePlugin({
100 'PRODUCTION': args.mode == 'production',
101 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200102 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200103 ],
104 // NOTE: Change to
105 // (args.mode == 'production' ? 'source-map' : 'inline-source-map')
106 // once https://crbug.com/212374 is fixed.
107 devtool: 'inline-source-map',
108 module: {
109 rules: [
110 {
avm999632485a3e2021-09-08 22:18:38 +0200111 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200112 type: 'json',
113 parser: {
114 parse: json5.parse,
115 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100116 use: [
117 preprocessorLoader,
118 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200119 },
avm999632485a3e2021-09-08 22:18:38 +0200120 {
121 test: /\.s[ac]ss$/i,
122 use: [
123 'style-loader',
124 'css-loader',
125 {
126 loader: 'sass-loader',
127 options: {
128 // Prefer `dart-sass`
129 implementation: require('sass'),
130 },
131 },
132 ],
133 },
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100134 {
135 test: /\.js$/i,
136 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100137 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100138 ],
139 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200140 ]
141 },
142 };
143};