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 | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 5 | const TerserPlugin = require('terser-webpack-plugin'); |
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', |
Adrià Vilanova Martínez | a2f7951 | 2022-07-10 15:32:32 +0200 | [diff] [blame] | 46 | publicThreadStart: './src/contentScripts/publicThreadStart.js', |
avm99963 | 37601bc | 2022-02-21 10:36:45 +0100 | [diff] [blame] | 47 | publicProfile: './src/contentScripts/publicProfile.js', |
| 48 | publicProfileStart: './src/contentScripts/publicProfileStart.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 49 | profileIndicator: './src/contentScripts/profileIndicator.js', |
| 50 | |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 51 | // Programatically injected content scripts |
| 52 | handleInstall: './src/contentScripts/communityConsole/handleInstall.js', |
| 53 | handleUpdate: './src/contentScripts/communityConsole/handleUpdate.js', |
| 54 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 55 | // Injected JS |
| 56 | profileIndicatorInject: './src/injections/profileIndicator.js', |
| 57 | batchLockInject: './src/injections/batchLock.js', |
Adrià Vilanova Martínez | 4757ae8 | 2022-12-28 16:41:37 +0100 | [diff] [blame] | 58 | xhrInterceptorInject: './src/injections/xhrProxy.js', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 59 | extraInfoInject: './src/injections/extraInfo.js', |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 60 | litComponentsInject: './src/injections/litComponentsInject.js', |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 61 | updateHandlerLitComponents: |
| 62 | './src/injections/updateHandlerLitComponents.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 63 | |
| 64 | // Options page |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 65 | optionsCommon: './src/options/optionsCommon.js', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 66 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 67 | // Workflow manager |
| 68 | workflowManager: './src/workflows/manager/index.js', |
| 69 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 70 | // Common CSS |
| 71 | mdcStyles: './src/mdc/index.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Background script (or service worker for MV3) |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 75 | entry.bg = './src/bg.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 76 | |
| 77 | let outputPath = path.join(__dirname, 'dist', env.browser_target); |
| 78 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 79 | let overridenLocalePaths = |
| 80 | localeOverrides.map(l => '**/_locales/' + l.pontoonLocale); |
| 81 | |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 82 | let preprocessorLoader = { |
| 83 | loader: 'webpack-preprocessor-loader', |
| 84 | options: { |
| 85 | params: { |
| 86 | browser_target: env.browser_target, |
| 87 | production: args.mode == 'production', |
Adrià Vilanova Martínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 88 | canary: !!env.canary |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 89 | }, |
| 90 | }, |
| 91 | }; |
| 92 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 93 | return { |
| 94 | entry, |
| 95 | output: { |
| 96 | filename: '[name].bundle.js', |
| 97 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 98 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 99 | }, |
| 100 | plugins: [ |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 101 | new WebpackShellPluginNext({ |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 102 | onBuildStart: { |
| 103 | scripts: ['make lit_localize_build'], |
| 104 | blocking: true, |
| 105 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 106 | onBuildEnd: { |
| 107 | scripts: |
avm99963 | efce210 | 2021-03-23 23:23:26 +0100 | [diff] [blame] | 108 | ['genmanifest -template templates/manifest.gjson -dest ' + |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 109 | path.join(outputPath, 'manifest.json') + ' ' + |
| 110 | env.browser_target] |
| 111 | } |
| 112 | }), |
| 113 | new CopyWebpackPlugin({ |
| 114 | patterns: [ |
| 115 | { |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 116 | from: path.join( |
| 117 | __dirname, 'src/icons', env.canary ? 'canary' : 'regular'), |
| 118 | to: path.join(outputPath, 'icons'), |
| 119 | }, |
| 120 | ] |
| 121 | }), |
| 122 | new CopyWebpackPlugin({ |
| 123 | patterns: [ |
| 124 | { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 125 | from: path.join(__dirname, 'src/static'), |
| 126 | to: outputPath, |
| 127 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 128 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 129 | } |
| 130 | }, |
| 131 | ] |
| 132 | }), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 133 | new webpack.DefinePlugin({ |
| 134 | 'PRODUCTION': args.mode == 'production', |
| 135 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 136 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 137 | ], |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 138 | devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 139 | module: { |
| 140 | rules: [ |
| 141 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 142 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 143 | type: 'json', |
| 144 | parser: { |
| 145 | parse: json5.parse, |
| 146 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 147 | use: [ |
| 148 | preprocessorLoader, |
| 149 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 150 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 151 | { |
| 152 | test: /\.s[ac]ss$/i, |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 153 | oneOf: [ |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 154 | { |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 155 | resourceQuery: /string/, |
| 156 | use: [ |
| 157 | 'css-loader', |
| 158 | { |
| 159 | loader: 'sass-loader', |
| 160 | options: { |
| 161 | // Prefer `dart-sass` |
| 162 | implementation: require('sass'), |
| 163 | }, |
| 164 | }, |
| 165 | ], |
| 166 | }, |
| 167 | { |
| 168 | use: [ |
| 169 | 'style-loader', |
| 170 | 'css-loader', |
| 171 | { |
| 172 | loader: 'sass-loader', |
| 173 | options: { |
| 174 | // Prefer `dart-sass` |
| 175 | implementation: require('sass'), |
| 176 | }, |
| 177 | }, |
| 178 | ], |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 179 | }, |
| 180 | ], |
| 181 | }, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 182 | { |
| 183 | test: /\.js$/i, |
| 184 | use: [ |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 185 | preprocessorLoader, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 186 | ], |
| 187 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 188 | ] |
| 189 | }, |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 190 | optimization: { |
| 191 | minimizer: [ |
| 192 | new TerserPlugin({ |
| 193 | terserOptions: { |
| 194 | format: { |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 195 | ascii_only: |
| 196 | true, // Due to https://iavm.xyz/b/twpowertools/145#c1 |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 197 | }, |
| 198 | }, |
| 199 | }), |
| 200 | ], |
| 201 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 202 | }; |
| 203 | }; |