forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild-transformer.js
67 lines (55 loc) · 1.5 KB
/
esbuild-transformer.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
const fs = require('fs')
const esbuild = require('esbuild')
const getCacheKeyFunction = require('@jest/create-cache-key-function').default
const nodeVersion = process.version.match(/v(\d+)/)[1] || '14'
const cacheKeyFunction = getCacheKeyFunction([], ['v1', nodeVersion])
function needsTranspilation(contents, filename) {
if (filename.endsWith('.ts') === true) {
return true
}
if (filename.endsWith('.mjs') === true) {
return true
}
if (contents.includes('require(') === true) {
return false
}
if (contents.includes('module.exports') === true) {
return false
}
if (contents.includes('exports.') === true) {
return false
}
return true
}
const transformer = {
getCacheKey(contents, filename, ...rest) {
return cacheKeyFunction(filename, fs.statSync(filename).mtimeMs.toString(), ...rest)
},
process(_content, filename, { transformerConfig }) {
if (needsTranspilation(_content, filename) === true) {
return esbuild.transformSync(_content, {
sourcesContent: true,
minify: false,
sourcefile: filename,
loader: 'ts',
format: 'cjs',
platform: 'node',
target: `node${nodeVersion}`,
keepNames: true,
logLevel: 'error',
sourcemap: true,
...transformerConfig,
})
}
try {
return {
code: _content,
map: fs.readFileSync(`${filename}.map`, 'utf8'),
}
} catch (e) {}
return {
code: _content,
}
},
}
module.exports = transformer