blob: 780774926263b988713f1da91916f3fe2db9d5f8 [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ínez1e10d192021-12-31 16:01:13 +01005const {VueLoaderPlugin} = require('vue-loader');
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',
13 webExtLocale: 'pt', // This way it targets both 'pt_BR' and 'pt_PT'
14 },
15];
16
17const getCopyPluginsForOverridenLocales = outputPath => {
18 return localeOverrides.map(l => {
19 return new CopyWebpackPlugin({
20 patterns: [
21 {
22 from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale),
23 to: path.join(outputPath, '_locales/' + l.webExtLocale),
24 globOptions: {
25 ignore: ['**/OWNERS', '**.md'],
26 }
27 },
28 ]
29 });
30 });
31};
32
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020033module.exports = (env, args) => {
34 let entry = {
35 // Content scripts
36 communityConsoleMain: './src/contentScripts/communityConsole/main.js',
37 communityConsoleStart: './src/contentScripts/communityConsole/start.js',
38 publicForum: './src/contentScripts/publicForum.js',
39 publicThread: './src/contentScripts/publicThread.js',
40 profile: './src/contentScripts/profile.js',
41 profileIndicator: './src/contentScripts/profileIndicator.js',
42
43 // Injected JS
44 profileIndicatorInject: './src/injections/profileIndicator.js',
45 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020046 xhrInterceptorInject: './src/injections/xhrInterceptor.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010047 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020048
49 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020050 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020051
52 // Common CSS
53 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020054 };
55
56 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010057 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020058
59 let outputPath = path.join(__dirname, 'dist', env.browser_target);
60
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020061 let overridenLocalePaths =
62 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
63
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010064 let preprocessorLoader = {
65 loader: 'webpack-preprocessor-loader',
66 options: {
67 params: {
68 browser_target: env.browser_target,
69 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010070 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010071 },
72 },
73 };
74
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020075 return {
76 entry,
77 output: {
78 filename: '[name].bundle.js',
79 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +020080 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020081 },
82 plugins: [
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020083 new WebpackShellPluginNext({
84 onBuildEnd: {
85 scripts:
avm99963efce2102021-03-23 23:23:26 +010086 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020087 path.join(outputPath, 'manifest.json') + ' ' +
88 env.browser_target]
89 }
90 }),
91 new CopyWebpackPlugin({
92 patterns: [
93 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +010094 from: path.join(
95 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
96 to: path.join(outputPath, 'icons'),
97 },
98 ]
99 }),
100 new CopyWebpackPlugin({
101 patterns: [
102 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200103 from: path.join(__dirname, 'src/static'),
104 to: outputPath,
105 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200106 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200107 }
108 },
109 ]
110 }),
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100111 new VueLoaderPlugin(),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200112 new webpack.DefinePlugin({
113 'PRODUCTION': args.mode == 'production',
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100114 '__VUE_OPTIONS_API__': true,
115 '__VUE_PROD_DEVTOOLS__': args.mode != 'production',
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200116 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200117 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200118 ],
119 // NOTE: Change to
120 // (args.mode == 'production' ? 'source-map' : 'inline-source-map')
121 // once https://crbug.com/212374 is fixed.
122 devtool: 'inline-source-map',
123 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ínez1e10d192021-12-31 16:01:13 +0100138 'vue-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 {
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100150 test: /\.css$/i,
151 use: [
152 'vue-style-loader',
153 'css-loader',
154 ],
155 },
156 {
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100157 test: /\.js$/i,
158 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100159 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100160 ],
161 },
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100162 {
163 test: /\.vue$/i,
164 loader: 'vue-loader',
165 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200166 ]
167 },
168 };
169};