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'); |
| 5 | const WebpackShellPluginNext = require('webpack-shell-plugin-next'); |
| 6 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 7 | // Pontoon uses their own locale set. This array lets us convert those locales |
| 8 | // to the ones accepted by Chrome, Firefox, etc. |
| 9 | const localeOverrides = [ |
| 10 | { |
| 11 | pontoonLocale: 'pt-rBR', |
Adrià Vilanova Martínez | 7f9540d | 2022-02-17 22:05:14 +0100 | [diff] [blame] | 12 | webExtLocale: 'pt_BR', |
| 13 | }, |
| 14 | { |
| 15 | pontoonLocale: 'pt-rBR', |
| 16 | webExtLocale: 'pt_PT', |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 17 | }, |
| 18 | ]; |
| 19 | |
| 20 | const getCopyPluginsForOverridenLocales = outputPath => { |
| 21 | return localeOverrides.map(l => { |
| 22 | return new CopyWebpackPlugin({ |
| 23 | patterns: [ |
| 24 | { |
| 25 | from: path.join(__dirname, 'src/static/_locales/' + l.pontoonLocale), |
| 26 | to: path.join(outputPath, '_locales/' + l.webExtLocale), |
| 27 | globOptions: { |
| 28 | ignore: ['**/OWNERS', '**.md'], |
| 29 | } |
| 30 | }, |
| 31 | ] |
| 32 | }); |
| 33 | }); |
| 34 | }; |
| 35 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 36 | module.exports = (env, args) => { |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 37 | // NOTE: When adding an entry, add the corresponding source map file to |
| 38 | // web_accessible_resources in //templates/manifest.gjson. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 39 | let entry = { |
| 40 | // Content scripts |
| 41 | communityConsoleMain: './src/contentScripts/communityConsole/main.js', |
| 42 | communityConsoleStart: './src/contentScripts/communityConsole/start.js', |
| 43 | publicForum: './src/contentScripts/publicForum.js', |
| 44 | publicThread: './src/contentScripts/publicThread.js', |
Adrià Vilanova Martínez | a2f7951 | 2022-07-10 15:32:32 +0200 | [diff] [blame] | 45 | publicThreadStart: './src/contentScripts/publicThreadStart.js', |
avm99963 | 37601bc | 2022-02-21 10:36:45 +0100 | [diff] [blame] | 46 | publicProfile: './src/contentScripts/publicProfile.js', |
| 47 | publicProfileStart: './src/contentScripts/publicProfileStart.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 48 | profileIndicator: './src/contentScripts/profileIndicator.js', |
| 49 | |
| 50 | // Injected JS |
| 51 | profileIndicatorInject: './src/injections/profileIndicator.js', |
| 52 | batchLockInject: './src/injections/batchLock.js', |
Adrià Vilanova Martínez | 43ec2b9 | 2021-07-16 18:44:54 +0200 | [diff] [blame] | 53 | xhrInterceptorInject: './src/injections/xhrInterceptor.js', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 54 | extraInfoInject: './src/injections/extraInfo.js', |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 55 | workflowComponentsInject: './src/injections/workflowComponentsInject.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 56 | |
| 57 | // Options page |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 58 | optionsCommon: './src/options/optionsCommon.js', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 59 | |
Adrià Vilanova Martínez | f276ac7 | 2022-10-13 22:16:22 +0200 | [diff] [blame] | 60 | // Workflow manager |
| 61 | workflowManager: './src/workflows/manager/index.js', |
| 62 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 63 | // Common CSS |
| 64 | mdcStyles: './src/mdc/index.js', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | // Background script (or service worker for MV3) |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 68 | entry.bg = './src/bg.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 69 | |
| 70 | let outputPath = path.join(__dirname, 'dist', env.browser_target); |
| 71 | |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 72 | let overridenLocalePaths = |
| 73 | localeOverrides.map(l => '**/_locales/' + l.pontoonLocale); |
| 74 | |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 75 | let preprocessorLoader = { |
| 76 | loader: 'webpack-preprocessor-loader', |
| 77 | options: { |
| 78 | params: { |
| 79 | browser_target: env.browser_target, |
| 80 | production: args.mode == 'production', |
Adrià Vilanova Martínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 81 | canary: !!env.canary |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 82 | }, |
| 83 | }, |
| 84 | }; |
| 85 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 86 | return { |
| 87 | entry, |
| 88 | output: { |
| 89 | filename: '[name].bundle.js', |
| 90 | path: outputPath, |
Adrià Vilanova Martínez | 85424b6 | 2021-07-11 21:52:00 +0200 | [diff] [blame] | 91 | clean: true, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 92 | }, |
| 93 | plugins: [ |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 94 | new WebpackShellPluginNext({ |
| 95 | onBuildEnd: { |
| 96 | scripts: |
avm99963 | efce210 | 2021-03-23 23:23:26 +0100 | [diff] [blame] | 97 | ['genmanifest -template templates/manifest.gjson -dest ' + |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 98 | path.join(outputPath, 'manifest.json') + ' ' + |
| 99 | env.browser_target] |
| 100 | } |
| 101 | }), |
| 102 | new CopyWebpackPlugin({ |
| 103 | patterns: [ |
| 104 | { |
Adrià Vilanova Martínez | 9aa2931 | 2022-01-16 02:42:46 +0100 | [diff] [blame] | 105 | from: path.join( |
| 106 | __dirname, 'src/icons', env.canary ? 'canary' : 'regular'), |
| 107 | to: path.join(outputPath, 'icons'), |
| 108 | }, |
| 109 | ] |
| 110 | }), |
| 111 | new CopyWebpackPlugin({ |
| 112 | patterns: [ |
| 113 | { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 114 | from: path.join(__dirname, 'src/static'), |
| 115 | to: outputPath, |
| 116 | globOptions: { |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 117 | ignore: ['**/OWNERS', '**.md', ...overridenLocalePaths], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 118 | } |
| 119 | }, |
| 120 | ] |
| 121 | }), |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 122 | new webpack.DefinePlugin({ |
| 123 | 'PRODUCTION': args.mode == 'production', |
| 124 | }), |
Adrià Vilanova Martínez | 43d0a7b | 2021-08-28 01:21:17 +0200 | [diff] [blame] | 125 | ...getCopyPluginsForOverridenLocales(outputPath), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 126 | ], |
Adrià Vilanova Martínez | 2df2f24 | 2022-03-01 22:25:27 +0100 | [diff] [blame] | 127 | devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'), |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 128 | module: { |
| 129 | rules: [ |
| 130 | { |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 131 | test: /\.json5$/i, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 132 | type: 'json', |
| 133 | parser: { |
| 134 | parse: json5.parse, |
| 135 | }, |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 136 | use: [ |
| 137 | preprocessorLoader, |
| 138 | ], |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 139 | }, |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 140 | { |
| 141 | test: /\.s[ac]ss$/i, |
| 142 | use: [ |
Adrià Vilanova Martínez | b06c13d | 2022-06-20 17:30:14 +0000 | [diff] [blame] | 143 | 'style-loader', |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 144 | 'css-loader', |
| 145 | { |
| 146 | loader: 'sass-loader', |
| 147 | options: { |
| 148 | // Prefer `dart-sass` |
| 149 | implementation: require('sass'), |
| 150 | }, |
| 151 | }, |
| 152 | ], |
| 153 | }, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 154 | { |
| 155 | test: /\.js$/i, |
| 156 | use: [ |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 157 | preprocessorLoader, |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 158 | ], |
| 159 | }, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 160 | ] |
| 161 | }, |
| 162 | }; |
| 163 | }; |