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