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