-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path_compile.js
106 lines (94 loc) · 2.87 KB
/
_compile.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
let { join } = require('path')
let { existsSync } = require('fs')
let { rm } = require('fs/promises')
let { build: esbuild } = require('esbuild')
let sourceMapStatement = `require('source-map-support/register');\n//# sourceMappingURL=index.js.map`
function getTsConfig (dir) {
let path = join(dir, 'tsconfig.json')
if (existsSync(path)) return path
return false
}
async function compileProject ({ inventory }) {
let { inv } = inventory
let { cwd } = inv._project
let start = Date.now()
let globalTsConfig = getTsConfig(cwd)
let ok = true
console.log(`Compiling TypeScript`)
async function go (lambda) {
if (lambda.config.runtime !== 'typescript') return
try {
await compileHandler({ inventory, lambda, globalTsConfig })
}
catch (err) {
ok = false
console.log(`esbuild error:`, err)
}
}
let compiles = Object.values(inv.lambdasBySrcDir).map(go)
await Promise.allSettled(compiles)
if (ok) console.log(`Compiled project in ${(Date.now() - start) / 1000}s`)
}
async function compileHandler (params) {
let { inventory, lambda, globalTsConfig } = params
let { deployStage: stage } = inventory.inv._arc
let { arc, cwd } = inventory.inv._project
let { build, src, handlerFile } = lambda
stage = stage || 'testing'
await rm(build, { recursive: true, force: true })
// Enumerate project TS settings
let configPath
let settings = {
sourcemaps: [ 'testing', 'staging' ],
// TODO publicSrc?
}
if (arc.typescript) {
arc.typescript.forEach(s => {
if (Array.isArray(s)) {
if (s[0] === 'sourcemaps') settings.sourcemaps = [ ...s.slice(1) ]
if (s[0] === 'esbuild-config') configPath = join(cwd, s.slice(1)[0])
}
})
}
// Construct esbuild options
// The following defaults cannot be changed
let options = {
entryPoints: [ join(src, 'index.ts') ],
bundle: true,
platform: 'node',
format: 'cjs',
outfile: handlerFile,
}
if (configPath) {
// eslint-disable-next-line
let config = require(configPath)
options = { ...config, ...options }
}
if (settings.sourcemaps.includes(stage)) {
options.sourcemap = 'external'
if (options.banner?.js) {
options.banner.js = options.banner.js + '\n' + sourceMapStatement
}
else options.banner = { js: sourceMapStatement }
if (stage !== 'testing') {
await esbuild({
entryPoints: [ join(cwd, 'node_modules', 'source-map-support', 'register') ],
bundle: true,
platform: 'node',
format: 'cjs',
outdir: join(build, 'node_modules', 'source-map-support'),
})
}
}
// Final config check
let localConfig = getTsConfig(src)
/**/ if (localConfig) options.tsconfig = localConfig
else if (globalTsConfig) options.tsconfig = globalTsConfig
// Run the build
await esbuild(options)
}
module.exports = {
compileHandler,
compileProject,
getTsConfig,
}