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