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