/
registerEsbuild.js
64 lines (53 loc) · 1.67 KB
/
registerEsbuild.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
'use strict';
const path = require('path');
const hook = require('node-hook');
const esbuild = require('esbuild');
const moduleAlias = require('module-alias');
const sourceMapSupport = require('source-map-support');
const nodeMajorVersion = Number(process.version.match(/(?<=^v)\d+/));
// for node >= 12, we use the package.json "export" property to
// map imports to dist (except package.json)
// so "sequelize/lib/errors" is actually mapped to "sequelize/dist/errors/index.js"
// (see package.json).
if (nodeMajorVersion < 12) {
const jsonFile = path.join(__dirname, '..', 'package.json');
moduleAlias.addAlias('sequelize/package.json', jsonFile);
const distDir = path.join(__dirname, '../dist');
// make imports from `sequelize/` go to `../dist/`
moduleAlias.addAlias('sequelize', distDir);
}
const maps = {};
// This logic is sourced from https://github.com/babel/babel/blob/39ba1ff300a5c9448ccd40a50a017e7f24e5cd56/packages/babel-register/src/node.js#L15-L31
function installSourceMapSupport() {
sourceMapSupport.install({
handleUncaughtExceptions: false,
environment: 'node',
retrieveSourceMap(source) {
const map = maps && maps[source];
if (map) {
return {
url: null,
map
};
}
return null;
}
});
}
function compileFor(loader) {
return (source, sourcefile) => {
const { code, map } = esbuild.transformSync(source, {
sourcemap: true,
target: 'node10',
format: 'cjs',
sourcefile,
loader
});
if (Object.keys(maps).length === 0) {
installSourceMapSupport();
}
maps[sourcefile] = map;
return code;
};
}
hook.hook('.ts', compileFor('ts'));