blob: ec705939c041970ba0a64c189c15cc8ee9eea152 [file] [log] [blame]
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +02001const path = require('path');
2const json5 = require('json5');
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02003const CleanTerminalPlugin = require('clean-terminal-webpack-plugin');
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +02004const CopyWebpackPlugin = require('copy-webpack-plugin');
5const WebpackShellPluginNext = require('webpack-shell-plugin-next');
6
7module.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ínez5bdc4732022-05-31 20:12:21 +020011 background: './src/background.ts',
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020012 options: './src/options/options.ts',
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020013 };
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ínez87e35052022-06-01 14:05:29 +020023 canary: !!env.canary,
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020024 },
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ínez87e35052022-06-01 14:05:29 +020047 from: path.join(
48 __dirname, 'src/icons', env.canary ? 'canary' : 'regular'),
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020049 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ínez5bdc4732022-05-31 20:12:21 +020064 new CleanTerminalPlugin(),
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020065 ],
66 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020067 resolve: {
68 extensions: ['.ts', '.tsx', '...'],
69 },
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020070 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ínez47d6fd02022-05-31 16:08:18 +020085 {loader: 'source-map-loader'},
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020086 preprocessorLoader,
87 ],
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020088 exclude: [
89 path.resolve(__dirname, 'node_modules'),
90 ],
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020091 },
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020092 {
93 test: /\.tsx?$/,
94 use: [
95 {loader: 'ts-loader'},
96 preprocessorLoader,
97 ]
98 },
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020099 ]
100 },
101 };
102};