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', |
Adrià Vilanova Martínez | 7f9540d | 2022-02-17 22:05:14 +0100 | [diff] [blame] | 13 | webExtLocale: 'pt_BR', |
| 14 | }, |
| 15 | { |
| 16 | pontoonLocale: 'pt-rBR', |
| 17 | webExtLocale: 'pt_PT', |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 18 | }, |
| 19 | ]; |
| 20 | |
| 21 | const 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 37 | module.exports = (env, args) => { |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 38 | // NOTE: When adding an entry, add the corresponding source map file to |
| 39 | // web_accessible_resources in //templates/manifest.gjson. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 40 | 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', |
avm99963 | 37601bc | 2022-02-21 10:36:45 +0100 | [diff] [blame] | 46 | publicProfile: './src/contentScripts/publicProfile.js', |
| 47 | publicProfileStart: './src/contentScripts/publicProfileStart.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 48 | profileIndicator: './src/contentScripts/profileIndicator.js', |
| 49 | |
| 50 | // Injected JS |
| 51 | profileIndicatorInject: './src/injections/profileIndicator.js', |
| 52 | batchLockInject: './src/injections/batchLock.js', |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 53 | xhrInterceptorInject: './src/injections/xhrInterceptor.js', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 54 | extraInfoInject: './src/injections/extraInfo.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 55 | |
| 56 | // Options page |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 57 | optionsCommon: './src/options/optionsCommon.js', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 58 | |
| 59 | // Common CSS |
| 60 | mdcStyles: './src/mdc/index.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // Background script (or service worker for MV3) |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 64 | entry.bg = './src/bg.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 65 | |
| 66 | let outputPath = path.join(__dirname, 'dist', env.browser_target); |
| 67 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 68 | let overridenLocalePaths = |
| 69 | localeOverrides.map(l => '**/_locales/' + l.pontoonLocale); |
| 70 | |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 71 | 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ínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 77 | canary: !!env.canary |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 78 | }, |
| 79 | }, |
| 80 | }; |
| 81 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 82 | return { |
| 83 | entry, |
| 84 | output: { |
| 85 | filename: '[name].bundle.js', |
| 86 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 87 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 88 | }, |
| 89 | plugins: [ |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 90 | new WebpackShellPluginNext({ |
| 91 | onBuildEnd: { |
| 92 | scripts: |
avm99963 | efce210 | 2021-03-23 23:23:26 +0100 | [diff] [blame] | 93 | ['genmanifest -template templates/manifest.gjson -dest ' + |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 94 | path.join(outputPath, 'manifest.json') + ' ' + |
| 95 | env.browser_target] |
| 96 | } |
| 97 | }), |
| 98 | new CopyWebpackPlugin({ |
| 99 | patterns: [ |
| 100 | { |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 101 | 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 110 | from: path.join(__dirname, 'src/static'), |
| 111 | to: outputPath, |
| 112 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 113 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 114 | } |
| 115 | }, |
| 116 | ] |
| 117 | }), |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 118 | new VueLoaderPlugin(), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 119 | new webpack.DefinePlugin({ |
| 120 | 'PRODUCTION': args.mode == 'production', |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 121 | '__VUE_OPTIONS_API__': true, |
| 122 | '__VUE_PROD_DEVTOOLS__': args.mode != 'production', |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 123 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 124 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 125 | ], |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 126 | devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 127 | module: { |
| 128 | rules: [ |
| 129 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 130 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 131 | type: 'json', |
| 132 | parser: { |
| 133 | parse: json5.parse, |
| 134 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 135 | use: [ |
| 136 | preprocessorLoader, |
| 137 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 138 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 139 | { |
| 140 | test: /\.s[ac]ss$/i, |
| 141 | use: [ |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 142 | 'vue-style-loader', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 143 | 'css-loader', |
| 144 | { |
| 145 | loader: 'sass-loader', |
| 146 | options: { |
| 147 | // Prefer `dart-sass` |
| 148 | implementation: require('sass'), |
| 149 | }, |
| 150 | }, |
| 151 | ], |
| 152 | }, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 153 | { |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 154 | test: /\.css$/i, |
| 155 | use: [ |
| 156 | 'vue-style-loader', |
| 157 | 'css-loader', |
| 158 | ], |
| 159 | }, |
| 160 | { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 161 | test: /\.js$/i, |
| 162 | use: [ |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 163 | preprocessorLoader, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 164 | ], |
| 165 | }, |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 166 | { |
| 167 | test: /\.vue$/i, |
| 168 | loader: 'vue-loader', |
| 169 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 170 | ] |
| 171 | }, |
| 172 | }; |
| 173 | }; |