blob: 9d28abbcb9b21177b689f3e75eb458eb44588298 [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',
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',
avm9996337601bc2022-02-21 10:36:45 +010046 publicProfile: './src/contentScripts/publicProfile.js',
47 publicProfileStart: './src/contentScripts/publicProfileStart.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020048 profileIndicator: './src/contentScripts/profileIndicator.js',
49
50 // Injected JS
51 profileIndicatorInject: './src/injections/profileIndicator.js',
52 batchLockInject: './src/injections/batchLock.js',
Adrià Vilanova Martínez43ec2b92021-07-16 18:44:54 +020053 xhrInterceptorInject: './src/injections/xhrInterceptor.js',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010054 extraInfoInject: './src/injections/extraInfo.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020055
56 // Options page
Adrià Vilanova Martínez09f35be2021-09-06 19:50:09 +020057 optionsCommon: './src/options/optionsCommon.js',
avm999632485a3e2021-09-08 22:18:38 +020058
59 // Common CSS
60 mdcStyles: './src/mdc/index.js',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020061 };
62
63 // Background script (or service worker for MV3)
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010064 entry.bg = './src/bg.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020065
66 let outputPath = path.join(__dirname, 'dist', env.browser_target);
67
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +020068 let overridenLocalePaths =
69 localeOverrides.map(l => '**/_locales/' + l.pontoonLocale);
70
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010071 let preprocessorLoader = {
72 loader: 'webpack-preprocessor-loader',
73 options: {
74 params: {
75 browser_target: env.browser_target,
76 production: args.mode == 'production',
Adrià Vilanova Martínez0335b512022-01-21 13:34:59 +010077 canary: !!env.canary
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010078 },
79 },
80 };
81
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020082 return {
83 entry,
84 output: {
85 filename: '[name].bundle.js',
86 path: outputPath,
Adrià Vilanova Martínez85424b62021-07-11 21:52:00 +020087 clean: true,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020088 },
89 plugins: [
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020090 new WebpackShellPluginNext({
91 onBuildEnd: {
92 scripts:
avm99963efce2102021-03-23 23:23:26 +010093 ['genmanifest -template templates/manifest.gjson -dest ' +
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020094 path.join(outputPath, 'manifest.json') + ' ' +
95 env.browser_target]
96 }
97 }),
98 new CopyWebpackPlugin({
99 patterns: [
100 {
Adrià Vilanova Martínez9aa29312022-01-16 02:42:46 +0100101 from: path.join(
102 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
103 to: path.join(outputPath, 'icons'),
104 },
105 ]
106 }),
107 new CopyWebpackPlugin({
108 patterns: [
109 {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200110 from: path.join(__dirname, 'src/static'),
111 to: outputPath,
112 globOptions: {
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200113 ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200114 }
115 },
116 ]
117 }),
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100118 new VueLoaderPlugin(),
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200119 new webpack.DefinePlugin({
120 'PRODUCTION': args.mode == 'production',
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100121 '__VUE_OPTIONS_API__': true,
122 '__VUE_PROD_DEVTOOLS__': args.mode != 'production',
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +0200123 }),
Adrià Vilanova Martínez43d0a7b2021-08-28 01:21:17 +0200124 ...getCopyPluginsForOverridenLocales(outputPath),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200125 ],
Adrià Vilanova Martínez2df2f242022-03-01 22:25:27 +0100126 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200127 module: {
128 rules: [
129 {
avm999632485a3e2021-09-08 22:18:38 +0200130 test: /\.json5$/i,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131 type: 'json',
132 parser: {
133 parse: json5.parse,
134 },
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100135 use: [
136 preprocessorLoader,
137 ],
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 },
avm999632485a3e2021-09-08 22:18:38 +0200139 {
140 test: /\.s[ac]ss$/i,
141 use: [
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100142 'vue-style-loader',
avm999632485a3e2021-09-08 22:18:38 +0200143 'css-loader',
144 {
145 loader: 'sass-loader',
146 options: {
147 // Prefer `dart-sass`
148 implementation: require('sass'),
149 },
150 },
151 ],
152 },
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100153 {
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100154 test: /\.css$/i,
155 use: [
156 'vue-style-loader',
157 'css-loader',
158 ],
159 },
160 {
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100161 test: /\.js$/i,
162 use: [
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +0100163 preprocessorLoader,
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100164 ],
165 },
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100166 {
167 test: /\.vue$/i,
168 loader: 'vue-loader',
169 },
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200170 ]
171 },
172 };
173};