-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
151 lines (144 loc) · 5.16 KB
/
webpack.config.js
File metadata and controls
151 lines (144 loc) · 5.16 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
const path = require("node:path");
const webpack = require('webpack');
const { merge } = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const ZipPlugin = require('zip-webpack-plugin');
const packageJson = require('./package.json');
function createTampermonkeyHeader(pkg) {
return `// ==UserScript==
// @name ${pkg.name}
// @namespace https://logic-arrows.io/
// @version ${pkg.version}
// @description ${pkg.description}
// @author ${pkg.author}
// @match https://logic-arrows.io/*
// @grant none
// @run-at document-start
// ==/UserScript==
fetch("https://raw.githubusercontent.com/MerinPrime/graphopt/refs/heads/main/templates/newchrome/style.css")
.then(res => res.text())
.then(css => {
const style = document.createElement("style");
style.textContent = css;
document.head.appendChild(style);
});`;
}
module.exports = (env) => {
const isProduction = !!env.production;
const commonConfig = {
mode: isProduction ? 'production' : 'development',
devtool: isProduction ? 'nosources-source-map' : 'source-map',
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
performance: {
hints: false,
},
optimization: {
minimize: isProduction,
minimizer: isProduction ? [
new TerserPlugin({
terserOptions: {
format: {
comments: /==UserScript==|@name|@version|@author|@description|@match|@grant|@run-at|@namespace/,
},
compress: {
drop_console: false,
unsafe: true,
},
},
}),
] : [],
}
};
const newChromeConfig = {
output: {
filename: 'index.js',
path: path.resolve(__dirname, './dist/newchrome/'),
clean: true,
},
plugins: [
new CopyPlugin({
patterns: [
{
from: './templates/newchrome/manifest.json',
to: 'manifest.json',
transform: (content) => {
const manifest = JSON.parse(content.toString());
manifest.version = packageJson.version;
return JSON.stringify(manifest, null, 2);
},
},
{ from: './templates/newchrome/images', to: 'images' },
{ from: './templates/newchrome/style.css', to: 'style.css' },
],
}),
new ZipPlugin({
path: path.resolve(__dirname, './dist'),
filename: 'newchrome-dist.zip',
})
],
};
const oldChromeConfig = {
output: {
filename: 'index.js',
path: path.resolve(__dirname, './dist/oldchrome/'),
clean: true,
},
plugins: [
new CopyPlugin({
patterns: [
{
from: './templates/oldchrome/manifest.json',
to: 'manifest.json',
transform: (content) => {
const manifest = JSON.parse(content.toString());
manifest.version = packageJson.version;
return JSON.stringify(manifest, null, 2);
},
},
{ from: './templates/oldchrome/images', to: 'images' },
{ from: './templates/oldchrome/style.css', to: 'style.css' },
{ from: './templates/oldchrome/injector.js', to: 'injector.js' },
],
}),
new ZipPlugin({
path: path.resolve(__dirname, './dist'),
filename: 'oldchrome-dist.zip',
})
],
};
const tampermonkeyConfig = {
output: {
filename: 'tampermonkey.js',
path: path.resolve(__dirname, './dist/tampermonkey/'),
clean: true,
},
plugins: [
new webpack.BannerPlugin({
banner: createTampermonkeyHeader(packageJson),
raw: true
}),
new ZipPlugin({
path: path.resolve(__dirname, './dist'),
filename: 'tampermonkey-dist.zip',
})
]
};
return [
merge(commonConfig, newChromeConfig),
merge(commonConfig, oldChromeConfig),
merge(commonConfig, tampermonkeyConfig),
];
}