-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesimport.mjs
More file actions
executable file
·589 lines (548 loc) · 16.9 KB
/
esimport.mjs
File metadata and controls
executable file
·589 lines (548 loc) · 16.9 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env node
/**
* Compile a project into static JavaScript modules and generate a browser importmap.
*
* Usage:
* esimport <package-root> <output-dir>
*/
import * as esbuild from 'esbuild'
import process from 'node:process'
import path from 'node:path'
import url from 'node:url'
import fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { glob } from 'glob'
import { minimatch } from 'minimatch'
import * as commander from 'commander'
import esimportPkgInfo from './package.json' with { type: 'json' }
import crypto from 'node:crypto'
import handler from 'serve-handler'
import * as http from 'node:http'
/**
* Yield integrity hashes for the given data using each of the specified algorithms.
*
* @param data {string|Buffer} - The data to hash.
* @param algorithms {string[]} - The hashing algorithms to use.
* @yields {string} - A base64-encoded hash string with algorithm prefix.
*/
export function* integrityHashes(data, algorithms = ['sha256', 'sha384', 'sha512']) {
for (const algorithm of algorithms) {
const hash = crypto.createHash(algorithm).update(data).digest('base64')
yield `${algorithm.toLowerCase().replace(/-/, '')}-${hash}`
}
}
/**
* The given path is inside another given parent path.
*
* @param parent {string} - Absolute parent path.
* @param child {string} - Absolute child path.
* @return {boolean} - True, if child inside the parent path.
*/
export function isParentDir(parent, child) {
const relative = path.relative(parent, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}
/**
* Invert an object's keys and values.
* @param obj {Object.<string,string>} - The object to invert.
* @return {Object.<string,string>} - The inverse of the given object.
*/
export function invertObject(obj) {
const defaultDict = new Proxy(
{},
{
get: (target, name) => (name in target ? target[name] : []),
},
)
for (const [key, value] of Object.entries(obj)) {
defaultDict[value] = defaultDict[value].concat([key])
}
return defaultDict
}
/**
* Recursively resolve the import paths from a package's entry point.
*
* See also: https://nodejs.org/api/packages.html#package-entry-points
*
* @param entryPoint {object|string|string[]} - The entry points (import|require|default).
*/
export function resolveImport(entryPoint) {
if (typeof entryPoint === 'string' || entryPoint === null) {
return entryPoint
} else if (Array.isArray(entryPoint)) {
return resolveImport(entryPoint.filter((e) => typeof e === 'object')[0])
}
for (const key of ['browser', 'import', 'default']) {
if (entryPoint.hasOwnProperty(key) && entryPoint[key] !== undefined) {
return resolveImport(entryPoint[key])
}
}
throw new Error(`No valid entry point found: ${entryPoint}`)
}
/**
* Resolve an import path from a filesystem path and a subpath pattern.
*
* See also: https://nodejs.org/api/packages.html#subpath-patterns
*
* @param {string} filePath - The relative filesystem path.
* @param {string} pathPattern - The pattern to match the entry point.
* @param {string} entryPointPattern - The pattern to resolve the import path.
*/
export function path2EntryPoint(filePath, pathPattern, entryPointPattern) {
const pathPatternRexEx = new RegExp(
path
.normalize(pathPattern)
.replace(/[.+?^${}()|\[\]\\]/g, '\\$&')
.replace(/\*/, '\(\.\*\)'),
)
if (!pathPatternRexEx.test(filePath)) {
throw new Error(`Invalid path ${filePath} for entry point ${pathPatternRexEx}`)
}
return path.normalize(
filePath.replace(pathPatternRexEx, entryPointPattern.replace(/\*/, '\$1')),
)
}
/**
* Expand a subpath pattern to a list of files.
*
* The *-character represents any string including a / or filesystem separator.
*
* @param pattern {string} - The subpath pattern to expand.
* @param cwd {string} - The current working directory.
*/
export async function expandSubpathPattern(pattern, cwd) {
return (
await glob(pattern.replace(/\*/, '{*,**/*}'), {
cwd,
nodir: true,
dotRelative: true,
ignore: 'node_modules/**',
posix: true,
})
).filter((filePath) => /\.([mc]?jsx?|tsx?|css|txt|json)$/.test(filePath))
}
/**
* Resolve a package's entry points.
*
* See also: https://nodejs.org/api/packages.html#package-entry-points
*
* @param pkgName {string} - The name of the package or # for imports.
* @param entryPoints {object|string|string[]} - The entry points (exports/imports).
*
* @returns {object} - A map of entry points to import paths.
*/
export function resolveEntryPoints(pkgName, entryPoints) {
if (typeof entryPoints === 'string') {
return { [pkgName]: resolveImport(entryPoints) }
} else if (Array.isArray(entryPoints)) {
return Object.fromEntries(
entryPoints.map((entryPoint) => [
path.join(pkgName, entryPoint),
resolveImport(entryPoint),
]),
)
} else if (typeof entryPoints === 'object') {
try {
return { [pkgName]: resolveImport(entryPoints) }
} catch (e) {
return Object.entries(entryPoints)
.filter(([key, value]) => value !== undefined)
.reduce((acc, [key, value]) => {
acc[path.join(pkgName, key)] = resolveImport(value)
return acc
}, {})
}
} else {
throw new Error(`Invalid entry points for package ${pkgName}: ${entryPoints}`)
}
}
/**
* Expand the subpaths for all patterns in the entry points.
* @param pkgName {string} - The name of the package or # for imports.
* @param entryPoints {object|string|string[]} - The entry points (exports/imports).
* @param cwd {string} - The current working directory.
* @param projectRoot {string} - The root directory of the project.
* @return {Promise<{object}>} - A map of entry points to their file paths.
*/
export async function expandEntryPoints(pkgName, entryPoints, cwd, projectRoot) {
const entryPointMap = {}
const excludePatterns = []
for (const [entryPointPattern, pathPattern] of Object.entries(
resolveEntryPoints(pkgName, entryPoints),
)) {
if (pathPattern === null) {
excludePatterns.push(entryPointPattern)
} else {
for (const subpath of await expandSubpathPattern(pathPattern, cwd)) {
const importPath = path2EntryPoint(subpath, pathPattern, entryPointPattern)
entryPointMap[importPath] = path.relative(projectRoot, path.join(cwd, subpath))
}
}
}
for (const key in entryPointMap) {
if (
excludePatterns.some((pattern) =>
minimatch(key, pattern.replace(/\*/, '{*,**/*}'), { nocomment: true }),
)
) {
delete entryPointMap[key]
}
}
return entryPointMap
}
export async function bundleExports(cwd, projectRoot) {
const { default: packageInfo } = await import(
`file://${path.join(cwd, 'package.json')}`,
{
with: { type: 'json' },
}
)
return await expandEntryPoints(
packageInfo.name,
packageInfo.exports || {
'.': packageInfo.browser || packageInfo.module || packageInfo.main,
},
cwd,
projectRoot,
)
}
/**
* Build the project using esbuild.
*
* @param projectRoot {string} - The root directory of the project.
* @param outputDir {string} - The output directory for the importmap.json and its collected ES module files.
* @param context {esbuild.BuildContext} - The esbuild context.
* @param entryPointSourceMap {Object.<string,string>} - A map of entry points to their file paths.
* @param options {Object} - The options for the build process.
* @return {Promise<Object.<string,Object.<string,string>>>} - A promise that resolves to the import map.
*/
async function build(projectRoot, outputDir, context, entryPointSourceMap, options) {
const result = await context.rebuild()
if (options.verbose) {
console.debug(await esbuild.analyzeMetafile(result.metafile))
}
console.info(`${Object.keys(result.metafile.inputs).length} ES modules processed.`)
const reverseEntryPointMap = invertObject(entryPointSourceMap)
let entryPointOutputMap = {}
for (const [name, output] of Object.entries(result.metafile.outputs)) {
if (output.entryPoint !== undefined) {
for (const e of reverseEntryPointMap[
path.relative(projectRoot, output.entryPoint)
]) {
entryPointOutputMap[e] = `./${path.relative(outputDir, name)}`
}
}
}
const port = options.serve === true ? 3000 : options.serve
if (options.serve && !options.pathPrefix)
options.pathPrefix = `http://localhost:${port}`
const integrity = {}
for (const value of Object.values(entryPointOutputMap)) {
const filePath = path.join(outputDir, value)
const fileContent = await fs.readFile(filePath)
integrity[options.pathPrefix ? path.join(options.pathPrefix, value) : value] = [
...integrityHashes(fileContent),
].join(' ')
}
if (options.pathPrefix) {
entryPointOutputMap = Object.fromEntries(
Object.entries(entryPointOutputMap).map(([key, value]) => [
key,
path.join(options.pathPrefix, value),
]),
)
}
const importMap = {
imports: entryPointOutputMap,
integrity,
}
await fs.writeFile(path.join(outputDir, 'importmap.json'), JSON.stringify(importMap))
return importMap
}
/**
* Watch the output directory for changes and rebuild the project.
* @param projectRoot {string} - The root directory of the project.
* @param outputDir {string} - The output directory for the importmap.json and its collected ES module files.
* @param context {esbuild.BuildContext} - The esbuild context.
* @param entryPointSourceMap {Object.<string,string>} - A map of entry points to their file paths.
* @param options {Object} - The options for the build process.
* @param server {http.Server} - A server object to be gracefully closed on interrupt.
* @return {Promise<*>}
*/
export async function watch(
projectRoot,
outputDir,
context,
entryPointSourceMap,
options,
server,
) {
const ac = new AbortController()
const { signal } = ac
const watcher = fs.watch(projectRoot, { signal, recursive: true })
process.on('SIGINT', async () => {
ac.abort()
if (server !== undefined) {
console.info('Gracefully shutting down. Please wait...')
await server.close(process.exit)
}
})
try {
for await (const event of watcher) {
if (!isParentDir(outputDir, path.join(projectRoot, event.filename))) {
await build(projectRoot, outputDir, context, entryPointSourceMap, options)
}
}
} catch (err) {
if (err.name === 'AbortError') {
return await context.dispose()
}
throw err
}
}
/**
* Compile the entry points of a package.
*
* @param packageDir {string} - The directory of the package to compile.
* @return {Promise<[Object.<string, string>, Array.<string>]>} - A promise that resolves to an array containing the entry points and external dependencies.
*/
export async function compileEntryPoints(packageDir) {
const { default: pkgInfo } = await import(
`file://${path.join(packageDir, 'package.json')}`,
{
with: { type: 'json' },
}
)
let entryPoints = {}
entryPoints = {
...entryPoints,
...(await expandEntryPoints('', pkgInfo.imports || {}, packageDir, packageDir)),
}
entryPoints = { ...entryPoints, ...(await bundleExports(packageDir, packageDir)) }
for (const dep in {
...pkgInfo.dependencies,
...pkgInfo.peerDependencies,
}) {
entryPoints = {
...entryPoints,
...(await bundleExports(path.join(packageDir, 'node_modules', dep), packageDir)),
}
}
return [
entryPoints,
Object.keys({
...entryPoints,
...pkgInfo.dependencies,
...pkgInfo.peerDependencies,
}),
]
}
export class UnenvResolvePlugin extends Object {
static nodeRuntimeModules = [
'assert',
'assert/strict',
'async_hooks',
'buffer',
'child_process',
'cluster',
'console',
'constants',
'crypto',
'dgram',
'diagnostics_channel',
'dns',
'dns/promises',
'domain',
'events',
'fs',
'fs/promises',
'http',
'http2',
'https',
'inspector',
'inspector/promises',
'module',
'net',
'os',
'path',
'path/posix',
'path/win32',
'perf_hooks',
'process',
'punycode',
'querystring',
'readline',
'readline/promises',
'repl',
'stream',
'stream/consumers',
'stream/promises',
'stream/web',
'string_decoder',
'sys',
'timers',
'timers/promises',
'tls',
'trace_events',
'tty',
'url',
'util',
'util/types',
'v8',
'vm',
'wasi',
'worker_threads',
'zlib',
]
static nodeRuntimeModulesRegex = new RegExp(
`^(node:)?(${UnenvResolvePlugin.nodeRuntimeModules
.map((i) => i.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
.join('|')})$`,
)
static unenvCallback = async (args) => {
let entry
if (args.path.startsWith('node:')) {
entry = args.path.slice(5)
} else {
entry = args.path
}
return {
path: url.fileURLToPath(import.meta.resolve(`unenv/node/${entry}`)),
external: false,
}
}
name = 'unenv'
setup = (build) => {
build.onResolve(
{
filter: UnenvResolvePlugin.nodeRuntimeModulesRegex,
},
UnenvResolvePlugin.unenvCallback,
)
}
}
export async function run(packageDir, outputDir, options) {
packageDir = path.isAbsolute(packageDir)
? packageDir
: path.join(process.cwd(), packageDir)
outputDir = path.isAbsolute(outputDir)
? outputDir
: path.join(process.cwd(), outputDir)
const [entryPoints, external] = await compileEntryPoints(packageDir)
const context = await esbuild.context({
entryPoints: Object.values(entryPoints).map((entryPoint) =>
path.join(packageDir, entryPoint),
),
bundle: true,
format: 'esm',
external,
outbase: packageDir,
outdir: outputDir,
banner: {
js: `/* Build with esimport - https://github.com/codingjoe/esimport */`,
css: `/* Build with esimport - https://github.com/codingjoe/esimport */`,
},
entryNames: '[dir]/[name]-[hash]',
minify: true,
sourcemap: true,
platform: 'browser',
target: 'es2022',
allowOverwrite: true,
metafile: true,
plugins: [new UnenvResolvePlugin()],
})
const importMap = await build(packageDir, outputDir, context, entryPoints, options)
if (options.serve) {
const server = http.createServer((request, response) => {
handler(request, response, {
public: outputDir,
headers: [
{
source: '**/*.@(js|css|map)',
headers: [
{
key: 'Cache-Control',
value: 'max-age=315360000, public, immutable',
},
],
},
{
source: '{**/*,*}',
headers: [
{
key: 'Connection',
value: 'close',
},
],
},
],
etag: true,
})
})
const port = options.serve === true ? 3000 : options.serve
server.listen(port, async () => {
console.info(`Running at http://localhost:${port}/`)
await watch(packageDir, outputDir, context, entryPoints, options, server)
})
} else if (options.watch) {
await watch(packageDir, outputDir, context, entryPoints, options)
} else {
await context.dispose()
}
return importMap
}
/**
* Parse string to integer and check if it's a valid port.
*
* @param value {string} -
* @param dummyPrevious
* @return {number}
*/
export function parsePort(value, dummyPrevious) {
const parsedValue = parseInt(value, 10)
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a number.')
} else if (!(49151 >= parsedValue && parsedValue > 1024)) {
throw new commander.InvalidArgumentError('Port must be between 1024 and 49151.')
}
return parsedValue
}
/**
* Main function to run the esimport command line tool.
* @param argv {string[]} - The command line arguments (process.argv).
* @return {Promise<void>}
*/
export async function main(argv) {
const program = new commander.Command('esimport')
await program
.description('Compile a project into ES modules and generate a browser importmap.')
.version(esimportPkgInfo.version)
.option('-w, --watch', 'Watch for changes and rebuild.')
.option('-v, --verbose', 'Verbose output.')
.option(
'-s , --serve [port]',
'Serve ES modules via HTTP for local development.',
parsePort,
)
.option(
'-p, --path-prefix <path>',
'Prefix all import paths with the given path. Useful when serving behind a reverse proxy.',
)
.argument(
'<package-dir>',
'Path to package that will transformed. The directory must contain a valid package.json file.',
)
.argument(
'<output-dir>',
'The output directory for the importmap.json and its collected ES module files.',
)
.action(run)
program.parse(argv)
}
/* node:coverage ignore next 6 */
if (
process.argv[1] === fileURLToPath(import.meta.url) ||
path.basename(process.argv[1]) === 'esimport' // npx
) {
await main(process.argv)
}