-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
esbuild.js
98 lines (95 loc) · 2.71 KB
/
esbuild.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
const cp = require('child_process')
let revision = 'master'
if (process.env.NODE_ENV !== 'development') {
try {
let res = cp.execSync(`git log -1 --date=iso --pretty=format:'"%h","%ad"'`, {encoding: 'utf8'})
revision = res.replaceAll('"', '').replace(',', ' ')
} catch (e) {
// ignore
}
}
let entryPlugin = {
name: 'entry',
setup(build) {
build.onResolve({filter: /^index\.js$/}, args => {
return {
path: args.path,
namespace: 'entry-ns'
}
})
build.onLoad({filter: /.*/, namespace: 'entry-ns'}, () => {
let contents = `'use strict'
if (global.__isMain) {
Object.defineProperty(console, 'log', {
value() {
if (logger) logger.info(...arguments)
}
})
const { createLogger } = require('./src/logger/index')
const logger = createLogger('server')
process.on('uncaughtException', function(err) {
let msg = 'Uncaught exception: ' + err.message
console.error(msg)
logger.error('uncaughtException', err.stack)
})
process.on('unhandledRejection', function(reason, p) {
if (reason instanceof Error) {
if (typeof reason.code === 'number') {
let msg = 'Unhandled response error ' + reason.code + ' from language server: ' + reason.message
if (reason.data != null) {
console.error(msg, reason.data)
} else {
console.error(msg)
}
} else {
console.error('UnhandledRejection: ' + reason.message + '\\n' + reason.stack)
}
} else {
console.error('UnhandledRejection: ' + reason)
}
logger.error('unhandledRejection ', p, reason)
})
const attach = require('./src/attach').default
attach({ reader: process.stdin, writer: process.stdout })
} else {
const exports = require('./src/index')
const logger = require('./src/logger').logger
const attach = require('./src/attach').default
module.exports = {attach, exports, logger, loadExtension: (filepath, active) => {
return exports.extensions.manager.load(filepath, active)
}}
}`
return {
contents,
resolveDir: __dirname
}
})
}
}
async function start() {
await require('esbuild').build({
entryPoints: ['index.js'],
bundle: true,
sourcemap: process.env.NODE_ENV === 'development',
define: {
REVISION: '"' + revision + '"',
ESBUILD: 'true',
'process.env.COC_NVIM': '"1"',
'global.__TEST__': 'false'
},
mainFields: ['module', 'main'],
platform: 'node',
treeShaking: true,
target: 'node16.18',
plugins: [entryPlugin],
banner: {
js: `"use strict";
global.__starttime = Date.now();
global.__isMain = require.main === module;`
},
outfile: 'build/index.js'
})
}
start().catch(e => {
console.error(e)
})