-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwebpack.config.js
More file actions
164 lines (157 loc) · 5.95 KB
/
webpack.config.js
File metadata and controls
164 lines (157 loc) · 5.95 KB
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
154
155
156
157
158
159
160
161
162
163
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { TsconfigPathsPlugin } = require('tsconfig-paths-webpack-plugin');
const RemoteDownloadFileWebpackPlugin = require('./webpack/RemoteDownloadFileWebpackPlugin');
const manifest = require('./webpack/manifest');
function getEnv() {
const ENV = (process.env.ENV = process.env.NODE_ENV);
const manifestVersion = process.env.MANIFEST_VERSION == 3 ? 3 : 2;
const browser = process.env.BROWSER ?? "chrome";
return { ENV, manifestVersion, browser };
};
/**
* Webpack configuration function
* @returns {import('webpack').Configuration}
*/
module.exports = () => {
if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = "development";
}
const { ENV, browser } = getEnv();
/**
* @type {import('webpack').Configuration}
*/
return {
entry: {
background: './src/scripts/background.ts',
'content/content-message-handler': './src/scripts/content/content-message-handler.ts',
content: './src/scripts/content.js',
main: './src/scripts/main.ts',
options: './src/scripts/options.js',
popup: './src/scripts/popup.js',
theme: './src/scripts/theme.js',
},
output: {
path: path.resolve(__dirname, 'dist', browser),
filename: 'scripts/[name].js',
clean: true,
},
cache:
ENV !== "development"
? false
: {
type: "filesystem",
name: "main-cache",
},
mode: ENV,
devtool:
ENV !== 'development'
? 'source-map'
: 'inline-source-map', // Changed from 'eval' to 'inline-source-map' for CSP compatibility
module: {
rules: [
{
test: /\.[jt]sx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
transpileOnly: true,
},
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
plugins: [new TsconfigPathsPlugin()],
},
plugins: [
new webpack.DefinePlugin({
"process.env": {
ENV: JSON.stringify(ENV),
},
}),
new ForkTsCheckerWebpackPlugin(),
new HtmlWebpackPlugin({
template: './src/options.html',
filename: 'options.html',
chunks: [
'theme',
'options'
],
}),
new HtmlWebpackPlugin({
template: './src/popup.html',
filename: 'popup.html',
chunks: [
'theme',
'popup'
],
}),
new CopyWebpackPlugin({
patterns: [
{ from: `./src/manifest.json`, to: 'manifest.json', transform: manifest.transform(browser) },
{ from: './src/images', to: 'images' },
{ from: './src/css', to: 'css' },
{ from: './src/sounds', to: 'sounds' },
],
}),
new CopyWebpackPlugin({
patterns: [
{
from: './src/third_party/tsitu/bm-menu.min.js',
to: 'third_party/tsitu/bm-menu.min.js',
transform(content, path) {
// Replace EXTENSION_URL with the appropriate extension URL iff Chrome
// Firefox will be replaced at runtime due to unique internal extension ID
if (browser == 'firefox') {
return content;
}
// The reason we replace for chrome at compile time is the extension review for MV3 rejects
// it otherwise. Even when we were replacing at runtime.
return content.toString().replace(/EXTENSION_URL/g, 'chrome-extension://ghfmjkamilolkalibpmokjigalmncfek');
},
},
],
}),
new RemoteDownloadFileWebpackPlugin([
{
urlPrefix:'https://raw.githubusercontent.com/MHCommunity/mh-dark-mode/main/css/',
pathPrefix: 'third_party/potatosalad/css/',
files: [
'giftbox.css',
'inbox.css',
'inventory.css',
'main.css',
'marketplace.css',
'messagebox.css',
'profile.css',
'scoreboard.css',
'shop.css',
'team.css',
'trap.css',
'treasuremap.css',
'camp/camp.css',
'camp/hud.css',
'camp/journal.css',
],
},
{
urlPrefix: 'https://cdn.jsdelivr.net/gh/tsitu/MH-Tools@master/src/bookmarklet/',
pathPrefix: 'third_party/tsitu/',
files: [
'bm-analyzer.min.js',
'bm-crafting.min.js',
'bm-cre.min.js',
'bm-crown.min.js',
'bm-map.min.js',
'bm-setup-fields.min.js',
'bm-setup-items.min.js',
],
},
])
],
};
}