-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
107 lines (59 loc) · 1.62 KB
/
webpack.config.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
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const nodePath = require("path");
const fs = require("fs");
const webpackOptions = {
mode: "production",
entry: "./src",
output: {
path: nodePath.join(process.cwd(), "lib"),
filename: "NRS.js",
libraryTarget: "commonjs",
},
target: 'node',
externals: {
"_http_agent": "_http_agent",
"_http_client": "_http_client",
"_tls_wrap": "_tls_wrap",
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: function (_, { value }) {
if(value.match(/NODE-RULES-SYSTEM-SIGNATURE/)) return true;
return false;
},
},
},
}),
],
},
};
if( process.argv[1].endsWith( "webpack-cli" ) ) {
module.exports = webpackOptions;
} else {
webpack(webpackOptions, (err, stats) => {
if (!err) err = stats.toJson().errors[0];
if (err) {
throw new Error(err);
}
console.log(stats.toString({colors: true}));
const content = fs.readFileSync( nodePath.join(process.cwd(), "lib/NRS.js") ).toString();
const NRS_PRIMORDIALS = `let a=require("http")["NRS_PRIMORDIALS"];`;
const FREEZE = `a.ObjectFreeze(e[n]);a.ObjectDefineProperty(t,n,{enumerable:!0,value:e[n]})`;
const finalContent = content.replace(
/!function\(t,e\){for\(var n in e\)t\[n\]=e\[n\];/,
`!function(t,e){${NRS_PRIMORDIALS}for(var n in e){${FREEZE}}`
);
fs.writeFile( nodePath.join(process.cwd(), "lib/NRS.js"), finalContent, err => {
if(err) {
throw new Error(err);
}
console.log("DONE!");
process.exit(0);
});
});
}