-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathexe.ts
More file actions
262 lines (233 loc) · 7.8 KB
/
Copy pathexe.ts
File metadata and controls
262 lines (233 loc) · 7.8 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { bold, dim, red } from 'ansis'
import { createDebug } from 'obug'
import { RE_DTS } from 'rolldown-plugin-dts/internal'
import satisfies from 'semver/functions/satisfies.js'
import { x } from 'tinyexec'
import { formatBytes } from '../utils/format.ts'
import { fsRemove, fsStat } from '../utils/fs.ts'
import { importWithError, typeAssert } from '../utils/general.ts'
import type { ResolvedConfig, RolldownChunk } from '../config/types.ts'
import type { ExeExtensionOptions, ExeTarget } from '@tsdown/exe'
export interface ExeOptions extends ExeExtensionOptions {
seaConfig?: Omit<SeaConfig, 'main' | 'output' | 'mainFormat'>
/**
* Output file name without any suffix or extension.
* For example, do not include `.exe`, platform suffixes, or architecture suffixes.
*/
fileName?: string | ((chunk: RolldownChunk) => string)
/**
* Output directory for executables.
* @default 'build'
*/
outDir?: string
}
/**
* See also [Node.js SEA Documentation](https://nodejs.org/api/single-executable-applications.html#generating-single-executable-applications-with---build-sea)
*
* Note some default values are different from Node.js defaults to optimize for typical use cases (e.g. disabling experimental warning, enabling code cache). These can be overridden.
*/
export interface SeaConfig {
main?: string
/** Optional, if not specified, uses the current Node.js binary */
executable?: string
output?: string
mainFormat?: 'commonjs' | 'module'
/** @default true */
disableExperimentalSEAWarning?: boolean
/** @default false */
useSnapshot?: boolean
/** @default false */
useCodeCache?: boolean
execArgv?: string[]
/** @default "env" */
execArgvExtension?: 'none' | 'env' | 'cli'
assets?: Record<string, string>
}
const debug = createDebug('tsdown:exe')
export function validateSea({
dts,
entry,
logger,
nameLabel,
}: Omit<ResolvedConfig, 'clean' | 'format'>): void {
if (process.versions.bun || process.versions.deno) {
throw new Error(
'The `exe` option is not supported in Bun and Deno environments.',
)
}
if (!satisfies(process.version, '>=25.7.0')) {
throw new Error(
`Node.js version ${process.version} does not support \`exe\` option. Please upgrade to Node.js 25.7.0 or later.`,
)
}
if (Object.keys(entry).length > 1) {
throw new Error(
`The \`exe\` feature currently only supports single entry points. Found entries:\n${JSON.stringify(entry, undefined, 2)}`,
)
}
if (dts) {
logger.warn(
nameLabel,
`Generating .d.ts files with \`exe\` option is not recommended since they won't be included in the executable. Consider separating your library and executable targets if you need type declarations.`,
)
}
logger.info(
nameLabel,
'`exe` option is experimental and may change in future releases.',
)
}
export async function buildExe(
config: ResolvedConfig,
chunks: RolldownChunk[],
): Promise<void> {
if (!config.exe) return
// Exclude dts chunks since SEA only supports a single entry point and dts chunks are not needed for the executable
const filteredChunks = chunks.filter((chunk) => !RE_DTS.test(chunk.fileName))
// Validate single chunk
if (filteredChunks.length > 1) {
throw new Error(
`The 'exe' feature currently only supports single-chunk outputs. Found ${filteredChunks.length} chunks.\n` +
`Chunks:\n${filteredChunks.map((c) => `- ${c.fileName}`).join('\n')}`,
)
}
const chunk = filteredChunks[0]
debug('Building executable with SEA for chunk:', chunk.fileName)
const bundledFile = path.join(config.outDir, chunk.fileName)
const { targets } = config.exe
if (targets?.length) {
if (config.exe.seaConfig?.executable) {
config.logger.warn(
config.nameLabel,
'`seaConfig.executable` is ignored when `targets` is specified.',
)
}
const { resolveNodeBinary, getTargetSuffix } =
await importWithError<typeof import('@tsdown/exe')>('@tsdown/exe')
for (const target of targets) {
const nodeBinaryPath = await resolveNodeBinary(target, config.logger)
const suffix = getTargetSuffix(target)
const outputFile = resolveOutputFileName(
config.exe,
chunk,
bundledFile,
target,
suffix,
)
await buildSingleExe(
config,
bundledFile,
outputFile,
nodeBinaryPath,
target,
)
}
} else {
const outputFile = resolveOutputFileName(config.exe, chunk, bundledFile)
await buildSingleExe(config, bundledFile, outputFile)
}
}
function resolveOutputFileName(
exe: ExeOptions,
chunk: RolldownChunk,
bundledFile: string,
target?: ExeTarget,
suffix?: string,
): string {
let baseName: string
if (exe.fileName) {
baseName =
typeof exe.fileName === 'function' ? exe.fileName(chunk) : exe.fileName
} else {
baseName = path.basename(bundledFile, path.extname(bundledFile))
}
if (suffix) {
baseName += suffix
}
if (
target?.platform ? target.platform === 'win' : process.platform === 'win32'
) {
baseName += '.exe'
}
return baseName
}
async function buildSingleExe(
config: ResolvedConfig,
bundledFile: string,
outputFile: string,
executable?: string,
target?: ExeTarget,
): Promise<void> {
typeAssert(config.exe)
const exe = config.exe
const exeOutDir = path.resolve(config.cwd, exe.outDir || 'build')
await mkdir(exeOutDir, { recursive: true })
const outputPath = path.join(exeOutDir, outputFile)
debug('Building SEA executable: %s -> %s', bundledFile, outputPath)
const t = performance.now()
// Create temp directory for sea-config.json
const tempDir = await mkdtemp(path.join(tmpdir(), 'tsdown-sea-'))
try {
const seaConfig: SeaConfig = {
disableExperimentalSEAWarning: true,
...exe.seaConfig,
main: bundledFile,
output: outputPath,
mainFormat: config.format === 'es' ? 'module' : 'commonjs',
}
if (executable) {
seaConfig.executable = executable
}
const seaConfigPath = path.join(tempDir, 'sea-config.json')
await writeFile(seaConfigPath, JSON.stringify(seaConfig))
debug('Wrote sea-config.json: %O -> %s', seaConfig, seaConfigPath)
// Always use host node for --build-sea; the executable field controls the target binary
debug('Running: %s --build-sea %s', process.execPath, seaConfigPath)
await x(process.execPath, ['--build-sea', seaConfigPath], {
nodeOptions: { stdio: ['ignore', 'ignore', 'inherit'] },
throwOnError: true,
})
} finally {
if (debug.enabled) {
debug('Preserving temp directory for debugging: %s', tempDir)
} else {
await fsRemove(tempDir)
}
}
// Ad-hoc codesign on macOS host for darwin-targeted executables
if ((target?.platform || process.platform) === 'darwin') {
try {
await x('codesign', ['--sign', '-', outputPath], {
nodeOptions: { stdio: 'inherit' },
throwOnError: true,
})
} catch {
config.logger.warn(
config.nameLabel,
`Failed to code-sign the executable. ${
process.platform === 'darwin'
? `You can sign it manually using:\n codesign --sign - "${outputPath}"`
: `Automatic code signing is not supported on ${process.platform}.`
}`,
)
}
}
// Report exe binary size
const stat = await fsStat(outputPath)
if (stat) {
const sizeText = formatBytes(stat.size)
config.logger.info(
config.nameLabel,
bold(path.relative(config.cwd, outputPath)),
` ${dim(sizeText!)}`,
)
}
config.logger.success(
config.nameLabel,
`Built executable: ${red(path.relative(config.cwd, outputPath))}`,
dim`(${Math.round(performance.now() - t)}ms)`,
)
}