blob: 239fc739b5ffad343070a0dc004a7ee7abdeffcb [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íneza197d862022-05-27 17:33:20 +020012 options: './src/options/options.js',
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',
23 },
24 },
25 };
26
27 return {
28 entry,
29 output: {
30 filename: '[name].bundle.js',
31 path: outputPath,
32 clean: true,
33 },
34 plugins: [
35 new WebpackShellPluginNext({
36 onBuildEnd: {
37 scripts:
38 ['genmanifest -template templates/manifest.gjson -dest ' +
39 path.join(outputPath, 'manifest.json') + ' ' +
40 env.browser_target]
41 }
42 }),
43 new CopyWebpackPlugin({
44 patterns: [
45 {
46 from: path.join(__dirname, 'src/icons'),
47 to: path.join(outputPath, 'icons'),
48 },
49 ]
50 }),
51 new CopyWebpackPlugin({
52 patterns: [
53 {
54 from: path.join(__dirname, 'src/static'),
55 to: outputPath,
56 globOptions: {
57 ignore: ['**/OWNERS', '**.md'],
58 }
59 },
60 ]
61 }),
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020062 new CleanTerminalPlugin(),
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020063 ],
64 devtool: (args.mode == 'production' ? 'source-map' : 'inline-source-map'),
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020065 resolve: {
66 extensions: ['.ts', '.tsx', '...'],
67 },
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020068 module: {
69 rules: [
70 {
71 test: /\.json5$/i,
72 type: 'json',
73 parser: {
74 parse: json5.parse,
75 },
76 use: [
77 preprocessorLoader,
78 ],
79 },
80 {
81 test: /\.js$/i,
82 use: [
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020083 {loader: 'source-map-loader'},
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020084 preprocessorLoader,
85 ],
86 },
Adrià Vilanova Martínez47d6fd02022-05-31 16:08:18 +020087 {
88 test: /\.tsx?$/,
89 use: [
90 {loader: 'ts-loader'},
91 preprocessorLoader,
92 ]
93 },
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +020094 ]
95 },
96 };
97};