generated from aeksco/react-typescript-web-extension-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
153 lines (142 loc) · 3.79 KB
/
webpack.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const browsers = {
chrome: 'chrome',
firefox: 'firefox',
};
const manifestVersions = {
v2: 'v2',
v3: 'v3',
};
const extOutputDir = 'dist';
function getBrowserDistPaths() {
const paths = {};
Object.values(browsers).forEach((b) => {
paths[b] = `./${extOutputDir}/${b}`;
});
return paths;
}
const browserDistPaths = getBrowserDistPaths();
// console.log(browserDistPaths);
function getManifestVersionPaths(base) {
return Object.values(manifestVersions).map((v) => `${base}/${v}`);
}
function getAllOutputPaths() {
const paths = Object.entries(browserDistPaths).flatMap(([browser, path]) => {
if (browser === browsers.firefox) {
return getManifestVersionPaths(path);
}
return [path];
});
// console.log(paths);
return paths;
}
function getBrowserSrcManifestPath(browser, manifestVersion) {
const base = `./src/manifests/${browser}`;
return getManifestPath(base, browser, manifestVersion);
}
function getBrowserDestManifestPath(browser, manifestVersion) {
const base = browserDistPaths[browser];
return getManifestPath(base, browser, manifestVersion);
}
function getManifestPath(base, browser, manifestVersion) {
let temp = base;
if (browser === browsers.firefox) {
temp += `/${manifestVersion}`;
}
return `${temp}/manifest.json`;
}
function getFileManagerPluginCopyOpts() {
const allPaths = getAllOutputPaths();
const opts = allPaths.flatMap((p) => {
return [
{ source: './build/**/*', destination: p },
{ source: './src/assets/**/*', destination: p },
];
});
const manifestCopyOpts = Object.values(browsers).flatMap((b) => {
if (b !== browsers.firefox) {
return [
{
source: getBrowserSrcManifestPath(b),
destination: getBrowserDestManifestPath(b),
},
];
}
return Object.values(manifestVersions).map((v) => {
return {
source: getBrowserSrcManifestPath(b, v),
destination: getBrowserDestManifestPath(b, v),
};
});
});
const allCopyOpts = opts.concat(manifestCopyOpts);
// console.log(allCopyOpts);
return allCopyOpts;
}
module.exports = {
entry: {
backgroundPage: path.join(__dirname, 'src/backgroundPage.ts'),
contentScript: path.join(__dirname, 'src/contentScript.tsx'),
// popup: path.join(__dirname, 'src/popup/index.tsx'),
},
output: {
path: path.join(__dirname, 'build/js'),
filename: '[name].js',
clean: true,
},
module: {
rules: [
{
exclude: /node_modules/,
test: /\.tsx?$/,
use: 'ts-loader',
},
// Treat src/css/app.css as a global stylesheet
{
test: /\app.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
// Load .module.css files as CSS modules
{
test: /\.module.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
},
},
'postcss-loader',
],
},
],
},
// Setup @src path resolution for TypeScript files
resolve: {
extensions: ['.ts', '.tsx', '.js'],
alias: {
'@src': path.resolve(__dirname, 'src/'),
},
},
optimization: {
minimizer: [`...`, new CssMinimizerPlugin()],
},
plugins: [
new MiniCssExtractPlugin({
filename: '../css/[name].css',
}),
new FileManagerPlugin({
events: {
onEnd: {
delete: ['./dist'],
copy: getFileManagerPluginCopyOpts(),
},
},
}),
],
};