generated from CS3219-AY2324S1/course-assessment-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.mjs
82 lines (76 loc) · 2.63 KB
/
webpack.config.mjs
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
const path = await import('path');
const { fileURLToPath } = await import('url');
const { readFileSync } = await import('fs');
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const nodeExternals = require('webpack-node-externals');
// import * as nodeExternals from 'webpack-node-externals';
// import * as WebpackObfuscator from 'webpack-obfuscator';
const dirname = path.dirname(fileURLToPath(import.meta.url))
/**
* https://gist.github.com/nerdyman/2f97b24ab826623bff9202750013f99e
* Resolve tsconfig.json paths to Webpack aliases
* @param {string} tsconfigPath - Path to tsconfig
* @param {string} webpackConfigBasePath - Path from tsconfig to Webpack config to create absolute aliases
* @return {object} - Webpack alias config
*/
function resolveTsconfigPathsToAlias(
tsconfigPath) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const tsconfig = JSON.parse(
readFileSync(
new URL(tsconfigPath, import.meta.url)
)
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { paths, baseUrl } = tsconfig.compilerOptions;
return Object.fromEntries(Object.entries(paths)
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
.filter(([, pathValues]) => pathValues.length > 0)
.map(([pathKey, pathValues]) => {
const key = pathKey.replace('/*', '');
const value = path.resolve(path.dirname(tsconfigPath),
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
baseUrl, pathValues[0].replace('/*', ''));
return [key, value];
}));
};
const config = (_env) => {
return {
mode: "production",
target: "node",
module: {
rules: [
{
use: [
{
loader: "ts-loader",
options: {
configFile: "tsconfig.webpack.json",
},
},
],
exclude: /node_modules/,
},
],
},
entry: "./src/server/prodServer.ts",
externals: [nodeExternals({
allowlist: ["amqplib", "https-browserify"]
})], // Ignore node_modules
output: {
path: path.resolve(dirname, ".server"),
filename: "server.bundle.js",
},
resolve: {
extensions: [".js", ".ts"],
alias: resolveTsconfigPathsToAlias( "./tsconfig.webpack.json")
},
// plugins: [
// // Obfuscate the code
// new WebpackObfuscator({
// rotateStringArray: true,
// }),
// ],
}};
export default config;