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: |
| 45 | './src/platforms/communityConsole/entryPoints/main.ts', |
| 46 | communityConsoleStart: |
| 47 | './src/platforms/communityConsole/entryPoints/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 | a2f7951 | 2022-07-10 15:32:32 +0200 | [diff] [blame] | 50 | publicThreadStart: './src/contentScripts/publicThreadStart.js', |
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', |
| 147 | template: 'src/features/workflows/templates/workflows.html.ejs', |
| 148 | chunks: ['workflowManager'], |
| 149 | }), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 150 | new webpack.DefinePlugin({ |
| 151 | 'PRODUCTION': args.mode == 'production', |
| 152 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 153 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 154 | ], |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 155 | devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'), |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 156 | resolve: { |
| 157 | extensions: ['.tsx', '.ts', '.js', '.json', '.wasm'], |
| 158 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 159 | module: { |
| 160 | rules: [ |
| 161 | { |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 162 | test: /\.js$/i, |
| 163 | use: [ |
| 164 | preprocessorLoader, |
| 165 | ], |
| 166 | }, |
| 167 | { |
| 168 | test: /\.tsx?$/, |
| 169 | use: [ |
| 170 | 'ts-loader', |
| 171 | preprocessorLoader, |
| 172 | ], |
| 173 | exclude: /node_modules/, |
| 174 | }, |
| 175 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 176 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 177 | type: 'json', |
| 178 | parser: { |
| 179 | parse: json5.parse, |
| 180 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 181 | use: [ |
| 182 | preprocessorLoader, |
| 183 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 184 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 185 | { |
| 186 | test: /\.s[ac]ss$/i, |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 187 | oneOf: [ |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 188 | { |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 189 | resourceQuery: /string/, |
| 190 | use: [ |
| 191 | 'css-loader', |
| 192 | { |
| 193 | loader: 'sass-loader', |
| 194 | options: { |
| 195 | // Prefer `dart-sass` |
| 196 | implementation: require('sass'), |
| 197 | }, |
| 198 | }, |
| 199 | ], |
| 200 | }, |
| 201 | { |
Adrià Vilanova Martínez | ddd3004 | 2023-11-01 22:54:17 +0100 | [diff] [blame] | 202 | resourceQuery: /asCSSFile/, |
| 203 | use: [ |
| 204 | MiniCssExtractPlugin.loader, |
| 205 | 'css-loader', |
| 206 | { |
| 207 | loader: 'sass-loader', |
| 208 | options: { |
| 209 | // Prefer `dart-sass` |
| 210 | implementation: require('sass'), |
| 211 | }, |
| 212 | }, |
| 213 | ], |
| 214 | }, |
| 215 | { |
Adrià Vilanova Martínez | 5f5b3e0 | 2023-07-23 00:08:17 +0200 | [diff] [blame] | 216 | use: [ |
| 217 | 'style-loader', |
| 218 | 'css-loader', |
| 219 | { |
| 220 | loader: 'sass-loader', |
| 221 | options: { |
| 222 | // Prefer `dart-sass` |
| 223 | implementation: require('sass'), |
| 224 | }, |
| 225 | }, |
| 226 | ], |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 227 | }, |
| 228 | ], |
| 229 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 230 | ] |
| 231 | }, |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 232 | optimization: { |
| 233 | minimizer: [ |
| 234 | new TerserPlugin({ |
| 235 | terserOptions: { |
| 236 | format: { |
Adrià Vilanova Martínez | a03d03a | 2023-03-11 23:30:07 +0100 | [diff] [blame] | 237 | ascii_only: |
| 238 | true, // Due to https://iavm.xyz/b/twpowertools/145#c1 |
Adrià Vilanova Martínez | 6b56043 | 2022-10-23 20:26:17 +0200 | [diff] [blame] | 239 | }, |
| 240 | }, |
| 241 | }), |
| 242 | ], |
| 243 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 244 | }; |
| 245 | }; |