generated from pboymt/userscript-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
104 lines (96 loc) · 2.89 KB
/
webpack.config.ts
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
import * as dotenv from "dotenv";
import path from "path";
import TerserPlugin from "terser-webpack-plugin";
import { BannerPlugin, Configuration, DefinePlugin } from "webpack";
import * as packageJson from "./package.json";
import { generateHeader, GeneratePathToUserscriptPlugin } from "./plugins/userscript.plugin";
import { Environment } from "./src/environment/environment.model";
const env = process.env.ENV as Environment || "development";
const envContents = dotenv.config({ path: `.env.${env}`, override: true });
let envContentsString: string;
try {
envContentsString = JSON.stringify(envContents.parsed);
} catch (error) {
throw new Error(`Error while loading .env.${env} file: ${error}, contents: ${envContents.parsed}`);
}
const configCommon: Configuration = {
mode: "none",
output: {
path: path.resolve(__dirname, "userscript"),
filename: "[filename].js",
},
resolve: {
extensions: [".ts", ".js"],
},
externals: {
...Object.fromEntries(Object.keys(packageJson.dependencies).map(key => [key, key]))
// "reflect-metadata": "reflect-metadata",
// "tsyringe": "tsyringe",
},
plugins: [
new BannerPlugin({
banner: generateHeader,
raw: true,
}),
new DefinePlugin({
"process.env.scriptEnvs": envContentsString
})
]
};
const configUserscript: Configuration = {
...configCommon,
entry: {
bundle: { import: "./src/index.ts", filename: "index.user.js" }
},
module: {
rules: [
{
test: /\.ts$/,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.(s(a|c)ss)$/,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader",
],
},
{
test: /\.html$/i,
loader: "html-loader",
options: {
esModule: true,
},
},
],
},
optimization: {
minimize: false,
minimizer: [new TerserPlugin({
// minify: TerserPlugin.swcMinify,
terserOptions: {
format: {
comments: false,
},
compress: false,
mangle: false,
},
extractComments: false,
})],
}
};
const configDev: Configuration = {
...configCommon,
entry: {
devBundle: { import: "./src/.empty", filename: "index.hot-reload.user.js" }
},
plugins: configCommon.plugins?.concat([
new GeneratePathToUserscriptPlugin()
]),
};
export default [configUserscript, configDev];