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 | 39f48f8 | 2024-09-29 17:55:04 +0200 | [diff] [blame] | 5 | const HtmlWebpackPlugin = require('html-webpack-plugin'); |
Adrià Vilanova Martínez | ddd3004 | 2023-11-01 22:54:17 +0100 | [diff] [blame] | 6 | const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 7 | const TerserPlugin = require('terser-webpack-plugin'); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 8 | const WebpackShellPluginNext = require('webpack-shell-plugin-next'); |
| 9 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 10 | // Pontoon uses their own locale set. This array lets us convert those locales |
| 11 | // to the ones accepted by Chrome, Firefox, etc. |
| 12 | const localeOverrides = [ |
| 13 | { |
| 14 | pontoonLocale: 'pt-rBR', |
Adrià Vilanova Martínez | 7f9540d | 2022-02-17 22:05:14 +0100 | [diff] [blame] | 15 | webExtLocale: 'pt_BR', |
| 16 | }, |
| 17 | { |
| 18 | pontoonLocale: 'pt-rBR', |
| 19 | webExtLocale: 'pt_PT', |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 20 | }, |
| 21 | ]; |
| 22 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 23 | module.exports = (env, args) => { |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 24 | // NOTE: When adding an entry, add the corresponding source map file to |
| 25 | // web_accessible_resources in //templates/manifest.gjson. |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 26 | const entry = { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 27 | // Content scripts |
Adrià Vilanova Martínez | 39f48f8 | 2024-09-29 17:55:04 +0200 | [diff] [blame] | 28 | communityConsoleMain: |
Adrià Vilanova Martínez | f92172f | 2024-10-19 15:55:15 +0200 | [diff] [blame] | 29 | './src/entryPoints/communityConsole/contentScripts/main.ts', |
Adrià Vilanova Martínez | 39f48f8 | 2024-09-29 17:55:04 +0200 | [diff] [blame] | 30 | communityConsoleStart: |
Adrià Vilanova Martínez | f92172f | 2024-10-19 15:55:15 +0200 | [diff] [blame] | 31 | './src/entryPoints/communityConsole/contentScripts/start.ts', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 32 | publicForum: './src/contentScripts/publicForum.js', |
| 33 | publicThread: './src/contentScripts/publicThread.js', |
Adrià Vilanova Martínez | 80f6c9f | 2024-11-10 01:09:48 +0100 | [diff] [blame] | 34 | publicThreadStart: './src/entryPoints/twBasic/thread/start.ts', |
Adrià Vilanova Martínez | 04ae103 | 2024-02-07 23:42:25 +0100 | [diff] [blame] | 35 | publicGuide: './src/contentScripts/publicGuide.js', |
| 36 | publicGuideStart: './src/contentScripts/publicGuideStart.js', |
avm99963 | 37601bc | 2022-02-21 10:36:45 +0100 | [diff] [blame] | 37 | publicProfile: './src/contentScripts/publicProfile.js', |
| 38 | publicProfileStart: './src/contentScripts/publicProfileStart.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 39 | profileIndicator: './src/contentScripts/profileIndicator.js', |
| 40 | |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 41 | // Programatically injected content scripts |
| 42 | handleInstall: './src/contentScripts/communityConsole/handleInstall.js', |
| 43 | handleUpdate: './src/contentScripts/communityConsole/handleUpdate.js', |
| 44 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 45 | // Injected JS |
| 46 | profileIndicatorInject: './src/injections/profileIndicator.js', |
| 47 | batchLockInject: './src/injections/batchLock.js', |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 48 | xhrInterceptorInject: './src/entryPoints/communityConsole/injections/xhrProxy.ts', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 49 | extraInfoInject: './src/injections/extraInfo.js', |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 50 | litComponentsInject: './src/injections/litComponentsInject.js', |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 51 | updateHandlerLitComponents: |
| 52 | './src/injections/updateHandlerLitComponents.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 53 | |
| 54 | // Options page |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 55 | optionsCommon: './src/options/optionsCommon.js', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 56 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 57 | // Workflow manager |
Adrià Vilanova Martínez | c78c3ad | 2024-05-18 00:35:51 +0200 | [diff] [blame] | 58 | workflowManager: './src/features/workflows/core/manager/index.js', |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 59 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 60 | // Common CSS |
| 61 | mdcStyles: './src/mdc/index.js', |
Adrià Vilanova Martínez | ddd3004 | 2023-11-01 22:54:17 +0100 | [diff] [blame] | 62 | |
| 63 | // Compiled Sass |
Adrià Vilanova Martínez | 917797e | 2024-05-25 22:41:25 +0200 | [diff] [blame] | 64 | ccDarkTheme: './src/features/ccDarkTheme/core/styles/main.scss?asCSSFile', |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 65 | |
| 66 | // Background script (or service worker for MV3) |
| 67 | bg: './src/bg.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 70 | // NOTE: When adding an entry, add the corresponding source map file to |
| 71 | // web_accessible_resources in //templates/manifest.gjson. |
| 72 | const styles = [ |
| 73 | { |
| 74 | origin: './src/features/enhancedAnnouncementsDot/ui/styles.css', |
| 75 | destination: 'css/enhanced_announcements_dot.css', |
| 76 | }, |
| 77 | { |
| 78 | origin: './src/features/fixedToolbar/ui/styles.css', |
| 79 | destination: 'css/fixed_toolbar.css', |
| 80 | }, |
| 81 | { |
| 82 | origin: './src/features/imageMaxHeight/ui/styles.css', |
| 83 | destination: 'css/image_max_height.css', |
| 84 | }, |
| 85 | { |
| 86 | origin: './src/features/increaseContrast/ui/styles.css', |
| 87 | destination: 'css/increase_contrast.css', |
| 88 | }, |
| 89 | { |
| 90 | origin: './src/features/repositionExpandThread/ui/styles.css', |
| 91 | destination: 'css/reposition_expand_thread.css', |
| 92 | }, |
| 93 | { |
| 94 | origin: './src/features/stickySidebarHeaders/ui/styles.css', |
| 95 | destination: 'css/sticky_sidebar_headers.css', |
| 96 | }, |
| 97 | ]; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 98 | |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 99 | const outputPath = path.join(__dirname, 'dist', env.browser_target); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 100 | |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 101 | const overridenLocalePaths = |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 102 | localeOverrides.map(l => '**/_locales/' + l.pontoonLocale); |
| 103 | |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 104 | let preprocessorLoader = { |
| 105 | loader: 'webpack-preprocessor-loader', |
| 106 | options: { |
| 107 | params: { |
| 108 | browser_target: env.browser_target, |
| 109 | production: args.mode == 'production', |
Adrià Vilanova Martínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 110 | canary: !!env.canary |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 111 | }, |
| 112 | }, |
| 113 | }; |
| 114 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 115 | return { |
| 116 | entry, |
| 117 | output: { |
| 118 | filename: '[name].bundle.js', |
| 119 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 120 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 121 | }, |
| 122 | plugins: [ |
Adrià Vilanova Martínez | ddd3004 | 2023-11-01 22:54:17 +0100 | [diff] [blame] | 123 | new MiniCssExtractPlugin({ |
| 124 | filename: '[name].bundle.css', |
| 125 | }), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 126 | new WebpackShellPluginNext({ |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 127 | onBuildStart: { |
| 128 | scripts: ['make lit_localize_build'], |
| 129 | blocking: true, |
| 130 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 131 | onBuildEnd: { |
| 132 | scripts: |
avm99963 | efce210 | 2021-03-23 23:23:26 +0100 | [diff] [blame] | 133 | ['genmanifest -template templates/manifest.gjson -dest ' + |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 134 | path.join(outputPath, 'manifest.json') + ' ' + |
| 135 | env.browser_target] |
| 136 | } |
| 137 | }), |
| 138 | new CopyWebpackPlugin({ |
| 139 | patterns: [ |
| 140 | { |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 141 | from: path.join( |
| 142 | __dirname, 'src/icons', env.canary ? 'canary' : 'regular'), |
| 143 | to: path.join(outputPath, 'icons'), |
| 144 | }, |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 145 | { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 146 | from: path.join(__dirname, 'src/static'), |
| 147 | to: outputPath, |
| 148 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 149 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 150 | } |
| 151 | }, |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 152 | ...getStylesCopyPatterns(styles), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 153 | ] |
| 154 | }), |
Adrià Vilanova Martínez | 39f48f8 | 2024-09-29 17:55:04 +0200 | [diff] [blame] | 155 | new HtmlWebpackPlugin({ |
| 156 | filename: 'workflows.html', |
Adrià Vilanova Martínez | ed1aba3 | 2024-10-26 21:23:12 +0200 | [diff] [blame] | 157 | template: |
| 158 | 'src/features/workflows/presentation/templates/workflows.html.ejs', |
Adrià Vilanova Martínez | 39f48f8 | 2024-09-29 17:55:04 +0200 | [diff] [blame] | 159 | chunks: ['workflowManager'], |
| 160 | }), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 161 | new webpack.DefinePlugin({ |
| 162 | 'PRODUCTION': args.mode == 'production', |
| 163 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 164 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 165 | ], |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 166 | devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'), |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 167 | resolve: { |
| 168 | extensions: ['.tsx', '.ts', '.js', '.json', '.wasm'], |
| 169 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 170 | module: { |
| 171 | rules: [ |
| 172 | { |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 173 | test: /\.js$/i, |
| 174 | use: [ |
| 175 | preprocessorLoader, |
| 176 | ], |
| 177 | }, |
| 178 | { |
| 179 | test: /\.tsx?$/, |
| 180 | use: [ |
| 181 | 'ts-loader', |
| 182 | preprocessorLoader, |
| 183 | ], |
| 184 | exclude: /node_modules/, |
| 185 | }, |
| 186 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 187 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 188 | type: 'json', |
| 189 | parser: { |
| 190 | parse: json5.parse, |
| 191 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 192 | use: [ |
| 193 | preprocessorLoader, |
| 194 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 195 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 196 | { |
| 197 | test: /\.s[ac]ss$/i, |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 198 | oneOf: [ |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 199 | { |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 200 | resourceQuery: /string/, |
| 201 | use: [ |
| 202 | 'css-loader', |
| 203 | { |
| 204 | loader: 'sass-loader', |
| 205 | options: { |
| 206 | // Prefer `dart-sass` |
| 207 | implementation: require('sass'), |
| 208 | }, |
| 209 | }, |
| 210 | ], |
| 211 | }, |
| 212 | { |
Adrià Vilanova Martínez | ddd3004 | 2023-11-01 22:54:17 +0100 | [diff] [blame] | 213 | resourceQuery: /asCSSFile/, |
| 214 | use: [ |
| 215 | MiniCssExtractPlugin.loader, |
| 216 | 'css-loader', |
| 217 | { |
| 218 | loader: 'sass-loader', |
| 219 | options: { |
| 220 | // Prefer `dart-sass` |
| 221 | implementation: require('sass'), |
| 222 | }, |
| 223 | }, |
| 224 | ], |
| 225 | }, |
| 226 | { |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 227 | use: [ |
| 228 | 'style-loader', |
| 229 | 'css-loader', |
| 230 | { |
| 231 | loader: 'sass-loader', |
| 232 | options: { |
| 233 | // Prefer `dart-sass` |
| 234 | implementation: require('sass'), |
| 235 | }, |
| 236 | }, |
| 237 | ], |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 238 | }, |
| 239 | ], |
| 240 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 241 | ] |
| 242 | }, |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 243 | optimization: { |
| 244 | minimizer: [ |
| 245 | new TerserPlugin({ |
| 246 | terserOptions: { |
| 247 | format: { |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 248 | ascii_only: |
| 249 | true, // Due to https://iavm.xyz/b/twpowertools/145#c1 |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 250 | }, |
| 251 | }, |
| 252 | }), |
| 253 | ], |
| 254 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 255 | }; |
| 256 | }; |
Adrià Vilanova Martínez | 3b585fd | 2024-11-10 19:17:31 +0100 | [diff] [blame] | 257 | |
| 258 | const getCopyPluginsForOverridenLocales = outputPath => { |
| 259 | return localeOverrides.map(l => { |
| 260 | return new CopyWebpackPlugin({ |
| 261 | patterns: [ |
| 262 | { |
| 263 | from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale), |
| 264 | to: path.join(outputPath, '_locales/' + l.webExtLocale), |
| 265 | globOptions: { |
| 266 | ignore: ['**/OWNERS', '**.md'], |
| 267 | } |
| 268 | }, |
| 269 | ] |
| 270 | }); |
| 271 | }); |
| 272 | }; |
| 273 | |
| 274 | const getStylesCopyPatterns = styles => { |
| 275 | return styles.map(s => { |
| 276 | return { |
| 277 | from: path.join(__dirname, s.origin), |
| 278 | to: s.destination, |
| 279 | }; |
| 280 | }); |
| 281 | } |