@halooj/register 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +146 -0
- package/package.json +20 -0
package/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/* eslint-disable node/no-deprecated-api */
|
|
2
|
+
const zlib = require('zlib');
|
|
3
|
+
const sourceMapArg = process.env.LOADER_SOURCEMAP_ONLY
|
|
4
|
+
|| process.argv.find((i) => i.startsWith('--sourcemap-only='))
|
|
5
|
+
|| '';
|
|
6
|
+
const sourceMapOnly = sourceMapArg ? sourceMapArg.split('=')[1].split(',').filter((i) => i) : false;
|
|
7
|
+
const map = new Proxy(Object.create(null), {
|
|
8
|
+
get(target, key) {
|
|
9
|
+
if (!target[key]) return null;
|
|
10
|
+
return zlib.inflateSync(target[key]).toString();
|
|
11
|
+
},
|
|
12
|
+
set(target, key, value) {
|
|
13
|
+
if (typeof value !== 'string') return false;
|
|
14
|
+
if (sourceMapOnly && key.includes('node_modules') && !sourceMapOnly.some((entry) => key.includes(entry))) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
target[key] = zlib.deflateSync(value, { level: 9 });
|
|
18
|
+
return true;
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
require('source-map-support').install({
|
|
22
|
+
handleUncaughtExceptions: false,
|
|
23
|
+
environment: 'node',
|
|
24
|
+
retrieveSourceMap(file) {
|
|
25
|
+
const data = map[file];
|
|
26
|
+
if (!data) return null;
|
|
27
|
+
return {
|
|
28
|
+
url: file,
|
|
29
|
+
map: data,
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
const path = require('path');
|
|
34
|
+
const vm = require('vm');
|
|
35
|
+
const fs = require('fs');
|
|
36
|
+
const esbuild = require('esbuild');
|
|
37
|
+
|
|
38
|
+
const major = +process.version.split('.')[0].split('v')[1];
|
|
39
|
+
const minor = +process.version.split('.')[1];
|
|
40
|
+
if (major < 18) {
|
|
41
|
+
console.error('NodeJS <18 is no longer supported');
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
process.env.NODE_APP_INSTANCE ||= '0';
|
|
45
|
+
|
|
46
|
+
const remove = [
|
|
47
|
+
// by esbuild
|
|
48
|
+
/(const|let|var)\s+__filename\s*=\s*fileURLToPath\s*\(\s*import\.meta\.url\s*\)\s*;?/g,
|
|
49
|
+
/(const|let|var)\s+__dirname\s*=\s*(path\.)?dirname\s*\(\s*__filename\s*\)\s*;?/g,
|
|
50
|
+
// by tsdown
|
|
51
|
+
/(const|let|var)\s+getFilename\s*=\s*\(\s*\)\s*=>\s*fileURLToPath\s*\(\s*import\.meta\.url\s*\)\s*;?/g,
|
|
52
|
+
/(const|let|var)\s+__filename\s*=\s*(?:(\/\*\s*@__PURE__\s*\*\/)\s*)?getFilename\s*\(\s*\)\s*;?/g,
|
|
53
|
+
];
|
|
54
|
+
function tryTransform(filename, content, tsx = true) {
|
|
55
|
+
for (const regex of remove) content = content.replace(regex, '');
|
|
56
|
+
return esbuild.transformSync(content, {
|
|
57
|
+
tsconfigRaw: '{"compilerOptions":{"experimentalDecorators":true}}',
|
|
58
|
+
sourcefile: filename,
|
|
59
|
+
sourcemap: 'both',
|
|
60
|
+
format: 'cjs',
|
|
61
|
+
loader: tsx ? 'tsx' : 'ts',
|
|
62
|
+
target: `node${major}.${minor}`,
|
|
63
|
+
jsx: 'transform',
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function transform(filename, tsx = true) {
|
|
68
|
+
const code = fs.readFileSync(filename, 'utf-8');
|
|
69
|
+
let result;
|
|
70
|
+
try {
|
|
71
|
+
result = tryTransform(filename, code, tsx);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
if (!e.message.includes('Top-level await')) throw e;
|
|
74
|
+
result = tryTransform(filename, code.replace(/await import *\(/g, 'require('), tsx);
|
|
75
|
+
console.warn('transforming top-level await to require for file ', filename);
|
|
76
|
+
}
|
|
77
|
+
if (result.warnings.length) console.warn(result.warnings);
|
|
78
|
+
map[filename] = result.map;
|
|
79
|
+
if (process.env.LOADER_DUMP_CODE && filename.endsWith(`/${process.env.LOADER_DUMP_CODE}`)) {
|
|
80
|
+
console.log(`-----${filename}-----`);
|
|
81
|
+
console.log(result.code.split('/# sourceMappingURL=')[0]);
|
|
82
|
+
}
|
|
83
|
+
return result.code;
|
|
84
|
+
}
|
|
85
|
+
const _script = new vm.Script('"Hydro"', { produceCachedData: true });
|
|
86
|
+
const bytecode = (_script.createCachedData && _script.createCachedData.call)
|
|
87
|
+
? _script.createCachedData()
|
|
88
|
+
: _script.cachedData;
|
|
89
|
+
require.extensions['.js'] = function loader(module, filename) {
|
|
90
|
+
if (major < 14) {
|
|
91
|
+
return module._compile(transform(filename), filename);
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
let content = fs.readFileSync(filename, 'utf-8');
|
|
95
|
+
const lastLine = content.trim().split('\n').pop();
|
|
96
|
+
if (lastLine.startsWith('//# sourceMappingURL=data:application/json;base64,')) {
|
|
97
|
+
const info = lastLine.split('//# sourceMappingURL=data:application/json;base64,')[1];
|
|
98
|
+
const payload = Buffer.from(info, 'base64').toString();
|
|
99
|
+
map[filename] = payload;
|
|
100
|
+
content = content.split('//# sourceMappingURL')[0];
|
|
101
|
+
}
|
|
102
|
+
return module._compile(content, filename);
|
|
103
|
+
} catch (e) { // ESM
|
|
104
|
+
return module._compile(transform(filename), filename);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
require.extensions['.ts'] = function loader(module, filename) {
|
|
108
|
+
return module._compile(transform(filename, false), filename);
|
|
109
|
+
};
|
|
110
|
+
require.extensions['.tsx'] = require.extensions['.jsx'] = function loader(module, filename) {
|
|
111
|
+
return module._compile(transform(filename), filename);
|
|
112
|
+
};
|
|
113
|
+
require.extensions['.jsc'] = function loader(module, filename) {
|
|
114
|
+
const buf = fs.readFileSync(filename);
|
|
115
|
+
bytecode.subarray(12, 16).copy(buf, 12);
|
|
116
|
+
if (![12, 13, 14, 15, 16, 17].filter((i) => process.version.startsWith(`v${i}`)).length) {
|
|
117
|
+
bytecode.subarray(16, 20).copy(buf, 16);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const length = buf.subarray(8, 12).reduce((sum, number, power) => sum += number * (256 ** power), 0);
|
|
121
|
+
let dummyCode = '';
|
|
122
|
+
if (length > 1) dummyCode = `"${'\u200B'.repeat(length - 2)}"`;
|
|
123
|
+
const script = new vm.Script(dummyCode, {
|
|
124
|
+
filename,
|
|
125
|
+
lineOffset: 0,
|
|
126
|
+
displayErrors: true,
|
|
127
|
+
cachedData: buf,
|
|
128
|
+
});
|
|
129
|
+
if (script.cachedDataRejected) throw new Error(`cacheDataRejected on ${filename}`);
|
|
130
|
+
const compiledWrapper = script.runInThisContext({
|
|
131
|
+
filename,
|
|
132
|
+
lineOffset: 0,
|
|
133
|
+
columnOffset: 0,
|
|
134
|
+
displayErrors: true,
|
|
135
|
+
});
|
|
136
|
+
const dirname = path.dirname(filename);
|
|
137
|
+
const args = [module.exports, require, module, filename, dirname, process, global];
|
|
138
|
+
return compiledWrapper.apply(module.exports, args);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const debug = process.argv.find((i) => i.startsWith('--debug'));
|
|
142
|
+
if (debug && !['0', 'false', 'off', 'disabled', 'no'].includes(debug.split('=')[1]?.toLowerCase())) {
|
|
143
|
+
console.log('Debug mode enabled');
|
|
144
|
+
process.env.NODE_ENV = 'development';
|
|
145
|
+
process.env.DEV = 'on';
|
|
146
|
+
} else process.env.NODE_ENV ||= 'production';
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@halooj/register",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": "https://github.com/hydro-dev/Hydro",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"esbuild": "0.25.2",
|
|
12
|
+
"source-map-support": "^0.5.21"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/source-map-support": "^0.5.10"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
}
|
|
20
|
+
}
|