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