Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 1 | const path = require('path'); |
| 2 | const json5 = require('json5'); |
| 3 | const CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 4 | const WebpackShellPluginNext = require('webpack-shell-plugin-next'); |
| 5 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 6 | // Pontoon uses their own locale set. This array lets us convert those locales |
| 7 | // to the ones accepted by Chrome, Firefox, etc. |
| 8 | const localeOverrides = [ |
| 9 | { |
| 10 | pontoonLocale: 'pt-rBR', |
| 11 | webExtLocale: 'pt', // This way it targets both 'pt_BR' and 'pt_PT' |
| 12 | }, |
| 13 | ]; |
| 14 | |
| 15 | const getCopyPluginsForOverridenLocales = outputPath => { |
| 16 | return localeOverrides.map(l => { |
| 17 | return new CopyWebpackPlugin({ |
| 18 | patterns: [ |
| 19 | { |
| 20 | from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale), |
| 21 | to: path.join(outputPath, '_locales/' + l.webExtLocale), |
| 22 | globOptions: { |
| 23 | ignore: ['**/OWNERS', '**.md'], |
| 24 | } |
| 25 | }, |
| 26 | ] |
| 27 | }); |
| 28 | }); |
| 29 | }; |
| 30 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 31 | module.exports = (env, args) => { |
| 32 | let entry = { |
| 33 | // Content scripts |
| 34 | communityConsoleMain: './src/contentScripts/communityConsole/main.js', |
| 35 | communityConsoleStart: './src/contentScripts/communityConsole/start.js', |
| 36 | publicForum: './src/contentScripts/publicForum.js', |
| 37 | publicThread: './src/contentScripts/publicThread.js', |
| 38 | profile: './src/contentScripts/profile.js', |
| 39 | profileIndicator: './src/contentScripts/profileIndicator.js', |
| 40 | |
| 41 | // Injected JS |
| 42 | profileIndicatorInject: './src/injections/profileIndicator.js', |
| 43 | batchLockInject: './src/injections/batchLock.js', |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 44 | xhrInterceptorInject: './src/injections/xhrInterceptor.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 45 | |
| 46 | // Options page |
| 47 | optionsCommon: './src/optionsCommon.js', |
| 48 | }; |
| 49 | |
| 50 | // Background script (or service worker for MV3) |
| 51 | if (env.browser_target == 'chromium_mv3') |
| 52 | entry.sw = './src/sw.js'; |
| 53 | else |
| 54 | entry.background = './src/background.js'; |
| 55 | |
| 56 | let outputPath = path.join(__dirname, 'dist', env.browser_target); |
| 57 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 58 | let overridenLocalePaths = |
| 59 | localeOverrides.map(l => '**/_locales/' + l.pontoonLocale); |
| 60 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 61 | return { |
| 62 | entry, |
| 63 | output: { |
| 64 | filename: '[name].bundle.js', |
| 65 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 66 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 67 | }, |
| 68 | plugins: [ |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 69 | new WebpackShellPluginNext({ |
| 70 | onBuildEnd: { |
| 71 | scripts: |
| 72 | ['go run tools/generateManifest.go -dest=' + |
| 73 | path.join(outputPath, 'manifest.json') + ' ' + |
| 74 | env.browser_target] |
| 75 | } |
| 76 | }), |
| 77 | new CopyWebpackPlugin({ |
| 78 | patterns: [ |
| 79 | { |
| 80 | from: path.join(__dirname, 'src/static'), |
| 81 | to: outputPath, |
| 82 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 83 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 84 | } |
| 85 | }, |
| 86 | ] |
| 87 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 88 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 89 | ], |
| 90 | // NOTE: Change to |
| 91 | // (args.mode == 'production' ? 'source-map' : 'inline-source-map') |
| 92 | // once https://crbug.com/212374 is fixed. |
| 93 | devtool: 'inline-source-map', |
| 94 | module: { |
| 95 | rules: [ |
| 96 | { |
| 97 | test: /\.json5$/, |
| 98 | type: 'json', |
| 99 | parser: { |
| 100 | parse: json5.parse, |
| 101 | }, |
| 102 | }, |
| 103 | ] |
| 104 | }, |
| 105 | }; |
| 106 | }; |