Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 1 | const path = require('path'); |
| 2 | const json5 = require('json5'); |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 3 | const CleanTerminalPlugin = require('clean-terminal-webpack-plugin'); |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 4 | const CopyWebpackPlugin = require('copy-webpack-plugin'); |
| 5 | const WebpackShellPluginNext = require('webpack-shell-plugin-next'); |
| 6 | |
| 7 | module.exports = (env, args) => { |
| 8 | // NOTE: When adding an entry, add the corresponding source map file to |
| 9 | // web_accessible_resources in //templates/manifest.gjson. |
| 10 | let entry = { |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 11 | background: './src/background.ts', |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 12 | options: './src/options/options.ts', |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | let outputPath = path.join(__dirname, 'dist', env.browser_target); |
| 16 | |
| 17 | let preprocessorLoader = { |
| 18 | loader: 'webpack-preprocessor-loader', |
| 19 | options: { |
| 20 | params: { |
| 21 | browser_target: env.browser_target, |
| 22 | production: args.mode == 'production', |
Adrià Vilanova Martínez | 87e3505 | 2022-06-01 14:05:29 +0200 | [diff] [blame] | 23 | canary: !!env.canary, |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 24 | }, |
| 25 | }, |
| 26 | }; |
| 27 | |
| 28 | return { |
| 29 | entry, |
| 30 | output: { |
| 31 | filename: '[name].bundle.js', |
| 32 | path: outputPath, |
| 33 | clean: true, |
| 34 | }, |
| 35 | plugins: [ |
| 36 | new WebpackShellPluginNext({ |
| 37 | onBuildEnd: { |
| 38 | scripts: |
| 39 | ['genmanifest -template templates/manifest.gjson -dest ' + |
| 40 | path.join(outputPath, 'manifest.json') + ' ' + |
| 41 | env.browser_target] |
| 42 | } |
| 43 | }), |
| 44 | new CopyWebpackPlugin({ |
| 45 | patterns: [ |
| 46 | { |
Adrià Vilanova Martínez | 87e3505 | 2022-06-01 14:05:29 +0200 | [diff] [blame] | 47 | from: path.join( |
| 48 | __dirname, 'src/icons', env.canary ? 'canary' : 'regular'), |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 49 | to: path.join(outputPath, 'icons'), |
| 50 | }, |
| 51 | ] |
| 52 | }), |
| 53 | new CopyWebpackPlugin({ |
| 54 | patterns: [ |
| 55 | { |
| 56 | from: path.join(__dirname, 'src/static'), |
| 57 | to: outputPath, |
| 58 | globOptions: { |
| 59 | ignore: ['**/OWNERS', '**.md'], |
| 60 | } |
| 61 | }, |
| 62 | ] |
| 63 | }), |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 64 | new CleanTerminalPlugin(), |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 65 | ], |
| 66 | devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'), |
Adrià Vilanova Martínez | 47d6fd0 | 2022-05-31 16:08:18 +0200 | [diff] [blame] | 67 | resolve: { |
| 68 | extensions: ['.ts', '.tsx', '...'], |
| 69 | }, |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 70 | module: { |
| 71 | rules: [ |
| 72 | { |
| 73 | test: /\.json5$/i, |
| 74 | type: 'json', |
| 75 | parser: { |
| 76 | parse: json5.parse, |
| 77 | }, |
| 78 | use: [ |
| 79 | preprocessorLoader, |
| 80 | ], |
| 81 | }, |
| 82 | { |
| 83 | test: /\.js$/i, |
| 84 | use: [ |
Adrià Vilanova Martínez | 47d6fd0 | 2022-05-31 16:08:18 +0200 | [diff] [blame] | 85 | {loader: 'source-map-loader'}, |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 86 | preprocessorLoader, |
| 87 | ], |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 88 | exclude: [ |
| 89 | path.resolve(__dirname, 'node_modules'), |
| 90 | ], |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 91 | }, |
Adrià Vilanova Martínez | 47d6fd0 | 2022-05-31 16:08:18 +0200 | [diff] [blame] | 92 | { |
| 93 | test: /\.tsx?$/, |
| 94 | use: [ |
| 95 | {loader: 'ts-loader'}, |
| 96 | preprocessorLoader, |
| 97 | ] |
| 98 | }, |
Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 99 | ] |
| 100 | }, |
| 101 | }; |
| 102 | }; |