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', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 49 | |
| 50 | // Common CSS |
| 51 | mdcStyles: './src/mdc/index.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // Background script (or service worker for MV3) |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 55 | entry.bg = './src/bg.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 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 | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 62 | 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 72 | return { |
| 73 | entry, |
| 74 | output: { |
| 75 | filename: '[name].bundle.js', |
| 76 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 77 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 78 | }, |
| 79 | plugins: [ |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 80 | new WebpackShellPluginNext({ |
| 81 | onBuildEnd: { |
| 82 | scripts: |
avm99963 | efce210 | 2021-03-23 23:23:26 +0100 | [diff] [blame] | 83 | ['genmanifest -template templates/manifest.gjson -dest ' + |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 84 | path.join(outputPath, 'manifest.json') + ' ' + |
| 85 | env.browser_target] |
| 86 | } |
| 87 | }), |
| 88 | new CopyWebpackPlugin({ |
| 89 | patterns: [ |
| 90 | { |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 91 | from: path.join( |
| 92 | __dirname, 'src/icons', env.canary ? 'canary' : 'regular'), |
| 93 | to: path.join(outputPath, 'icons'), |
| 94 | }, |
| 95 | ] |
| 96 | }), |
| 97 | new CopyWebpackPlugin({ |
| 98 | patterns: [ |
| 99 | { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 100 | from: path.join(__dirname, 'src/static'), |
| 101 | to: outputPath, |
| 102 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 103 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 104 | } |
| 105 | }, |
| 106 | ] |
| 107 | }), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 108 | new webpack.DefinePlugin({ |
| 109 | 'PRODUCTION': args.mode == 'production', |
| 110 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 111 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 112 | ], |
| 113 | // NOTE: Change to |
| 114 | // (args.mode == 'production' ? 'source-map' : 'inline-source-map') |
| 115 | // once https://crbug.com/212374 is fixed. |
| 116 | devtool: 'inline-source-map', |
| 117 | module: { |
| 118 | rules: [ |
| 119 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 120 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 121 | type: 'json', |
| 122 | parser: { |
| 123 | parse: json5.parse, |
| 124 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 125 | use: [ |
| 126 | preprocessorLoader, |
| 127 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 128 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 129 | { |
| 130 | test: /\.s[ac]ss$/i, |
| 131 | use: [ |
| 132 | 'style-loader', |
| 133 | 'css-loader', |
| 134 | { |
| 135 | loader: 'sass-loader', |
| 136 | options: { |
| 137 | // Prefer `dart-sass` |
| 138 | implementation: require('sass'), |
| 139 | }, |
| 140 | }, |
| 141 | ], |
| 142 | }, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 143 | { |
| 144 | test: /\.js$/i, |
| 145 | use: [ |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 146 | preprocessorLoader, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 147 | ], |
| 148 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 149 | ] |
| 150 | }, |
| 151 | }; |
| 152 | }; |