blob: 0e620adfdd2fd79fed1556514bc0b7c786ce9c70 [file] [log] [blame]
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +02001const path = require('path');
2const json5 = require('json5');
3const CopyWebpackPlugin = require('copy-webpack-plugin');
4const WebpackShellPluginNext = require('webpack-shell-plugin-next');
5
6module.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'),
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020063 resolve: {
64 extensions: ['.ts', '.tsx', '...'],
65 },
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020066 module: {
67 rules: [
68 {
69 test: /\.json5$/i,
70 type: 'json',
71 parser: {
72 parse: json5.parse,
73 },
74 use: [
75 preprocessorLoader,
76 ],
77 },
78 {
79 test: /\.js$/i,
80 use: [
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020081 {loader: 'source-map-loader'},
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020082 preprocessorLoader,
83 ],
84 },
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020085 {
86 test: /\.tsx?$/,
87 use: [
88 {loader: 'ts-loader'},
89 preprocessorLoader,
90 ]
91 },
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020092 ]
93 },
94 };
95};