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'); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 5 | const {VueLoaderPlugin} = require('vue-loader'); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 6 | const WebpackShellPluginNext = require('webpack-shell-plugin-next'); |
| 7 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 8 | // Pontoon uses their own locale set. This array lets us convert those locales |
| 9 | // to the ones accepted by Chrome, Firefox, etc. |
| 10 | const localeOverrides = [ |
| 11 | { |
| 12 | pontoonLocale: 'pt-rBR', |
| 13 | webExtLocale: 'pt', // This way it targets both 'pt_BR' and 'pt_PT' |
| 14 | }, |
| 15 | ]; |
| 16 | |
| 17 | const 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 33 | module.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ínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 46 | xhrInterceptorInject: './src/injections/xhrInterceptor.js', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 47 | extraInfoInject: './src/injections/extraInfo.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 48 | |
| 49 | // Options page |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 50 | optionsCommon: './src/options/optionsCommon.js', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 51 | |
| 52 | // Common CSS |
| 53 | mdcStyles: './src/mdc/index.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // Background script (or service worker for MV3) |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 57 | entry.bg = './src/bg.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 58 | |
| 59 | let outputPath = path.join(__dirname, 'dist', env.browser_target); |
| 60 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 61 | let overridenLocalePaths = |
| 62 | localeOverrides.map(l => '**/_locales/' + l.pontoonLocale); |
| 63 | |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 64 | 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ínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 70 | canary: !!env.canary |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 71 | }, |
| 72 | }, |
| 73 | }; |
| 74 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 75 | return { |
| 76 | entry, |
| 77 | output: { |
| 78 | filename: '[name].bundle.js', |
| 79 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 80 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 81 | }, |
| 82 | plugins: [ |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 83 | new WebpackShellPluginNext({ |
| 84 | onBuildEnd: { |
| 85 | scripts: |
avm99963 | efce210 | 2021-03-23 23:23:26 +0100 | [diff] [blame] | 86 | ['genmanifest -template templates/manifest.gjson -dest ' + |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 87 | path.join(outputPath, 'manifest.json') + ' ' + |
| 88 | env.browser_target] |
| 89 | } |
| 90 | }), |
| 91 | new CopyWebpackPlugin({ |
| 92 | patterns: [ |
| 93 | { |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 94 | 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 103 | from: path.join(__dirname, 'src/static'), |
| 104 | to: outputPath, |
| 105 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 106 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 107 | } |
| 108 | }, |
| 109 | ] |
| 110 | }), |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 111 | new VueLoaderPlugin(), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 112 | new webpack.DefinePlugin({ |
| 113 | 'PRODUCTION': args.mode == 'production', |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 114 | '__VUE_OPTIONS_API__': true, |
| 115 | '__VUE_PROD_DEVTOOLS__': args.mode != 'production', |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 116 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 117 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 118 | ], |
| 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 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 126 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 127 | type: 'json', |
| 128 | parser: { |
| 129 | parse: json5.parse, |
| 130 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 131 | use: [ |
| 132 | preprocessorLoader, |
| 133 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 134 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 135 | { |
| 136 | test: /\.s[ac]ss$/i, |
| 137 | use: [ |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 138 | 'vue-style-loader', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 139 | 'css-loader', |
| 140 | { |
| 141 | loader: 'sass-loader', |
| 142 | options: { |
| 143 | // Prefer `dart-sass` |
| 144 | implementation: require('sass'), |
| 145 | }, |
| 146 | }, |
| 147 | ], |
| 148 | }, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 149 | { |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 150 | test: /\.css$/i, |
| 151 | use: [ |
| 152 | 'vue-style-loader', |
| 153 | 'css-loader', |
| 154 | ], |
| 155 | }, |
| 156 | { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 157 | test: /\.js$/i, |
| 158 | use: [ |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 159 | preprocessorLoader, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 160 | ], |
| 161 | }, |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 162 | { |
| 163 | test: /\.vue$/i, |
| 164 | loader: 'vue-loader', |
| 165 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 166 | ] |
| 167 | }, |
| 168 | }; |
| 169 | }; |