unrun 0.2.5 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +10 -4
- package/dist/index.mjs +1 -1
- package/dist/{src-Cy5isIwp.mjs → src-BoMbIRQr.mjs} +67 -10
- package/dist/sync/worker.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -33,11 +33,17 @@ interface Options {
|
|
|
33
33
|
}
|
|
34
34
|
//#endregion
|
|
35
35
|
//#region src/types.d.ts
|
|
36
|
-
interface Result {
|
|
36
|
+
interface Result<T = unknown> {
|
|
37
37
|
/**
|
|
38
38
|
* The module that was loaded.
|
|
39
|
+
* You can specify the type of the module by providing a type argument when using the `unrun` function.
|
|
39
40
|
*/
|
|
40
|
-
module:
|
|
41
|
+
module: T;
|
|
42
|
+
/**
|
|
43
|
+
* The dependencies involved when loading the targeted module.
|
|
44
|
+
* Note: this only includes local file dependencies, npm-resolved dependencies are excluded.
|
|
45
|
+
*/
|
|
46
|
+
dependencies: string[];
|
|
41
47
|
}
|
|
42
48
|
interface CliResult {
|
|
43
49
|
/**
|
|
@@ -53,7 +59,7 @@ interface CliResult {
|
|
|
53
59
|
* @param options - The options for loading the module.
|
|
54
60
|
* @returns A promise that resolves to the loaded module.
|
|
55
61
|
*/
|
|
56
|
-
declare function unrun(options: Options): Promise<Result
|
|
62
|
+
declare function unrun<T>(options: Options): Promise<Result<T>>;
|
|
57
63
|
/**
|
|
58
64
|
* Loads a module with JIT transpilation based on the provided options.
|
|
59
65
|
* This function runs synchronously using a worker thread.
|
|
@@ -61,7 +67,7 @@ declare function unrun(options: Options): Promise<Result>;
|
|
|
61
67
|
* @param options - The options for loading the module.
|
|
62
68
|
* @returns The loaded module.
|
|
63
69
|
*/
|
|
64
|
-
declare function unrunSync(options: Options): Result
|
|
70
|
+
declare function unrunSync<T>(options: Options): Result<T>;
|
|
65
71
|
/**
|
|
66
72
|
* Runs a given module with JIT transpilation based on the provided options.
|
|
67
73
|
* This function does not return the module, as it simply executes it.
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
1
|
+
import { builtinModules, createRequire } from "node:module";
|
|
2
2
|
import process from "node:process";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import fs, { existsSync } from "node:fs";
|
|
@@ -74,6 +74,56 @@ function resolveOptions(options = {}) {
|
|
|
74
74
|
return resolvedOptions;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/utils/module-resolution.ts
|
|
79
|
+
/**
|
|
80
|
+
* Precomputed set of Node.js builtin module specifiers, including both plain and `node:` prefixed forms.
|
|
81
|
+
*/
|
|
82
|
+
const BUILTIN_MODULE_SPECIFIERS = new Set([...builtinModules, ...builtinModules.map((name) => `node:${name}`)]);
|
|
83
|
+
/**
|
|
84
|
+
* Returns true when `id` refers to a Node.js builtin module, accepting both plain and `node:` specifiers.
|
|
85
|
+
*/
|
|
86
|
+
function isBuiltinModuleSpecifier(id) {
|
|
87
|
+
if (!id) return false;
|
|
88
|
+
return BUILTIN_MODULE_SPECIFIERS.has(id) || id.startsWith("node:");
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Removes Rollup-style query/hash suffixes from importer paths, falling back when importer is virtual or missing.
|
|
92
|
+
*/
|
|
93
|
+
function normalizeImporterPath(importer, fallback) {
|
|
94
|
+
if (!importer || importer.startsWith("\0")) return fallback;
|
|
95
|
+
const [withoutQuery] = importer.split("?");
|
|
96
|
+
return withoutQuery || fallback;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Checks if `child` is inside `parent` directory.
|
|
100
|
+
*/
|
|
101
|
+
function isPathWithinDirectory(parent, child) {
|
|
102
|
+
const relative = path.relative(parent, child);
|
|
103
|
+
return relative === "" || !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/features/external.ts
|
|
108
|
+
/**
|
|
109
|
+
* Builds the rolldown external resolver used to decide which imports remain external.
|
|
110
|
+
* Treat bare imports from the primary node_modules as external, but inline
|
|
111
|
+
* nested node_modules so they keep working once executed from .unrun.
|
|
112
|
+
*/
|
|
113
|
+
function createExternalResolver(options) {
|
|
114
|
+
const projectNodeModules = path.resolve(process.cwd(), "node_modules");
|
|
115
|
+
return function external(id, importer) {
|
|
116
|
+
if (!id || id.startsWith("\0")) return false;
|
|
117
|
+
if (id.startsWith(".") || id.startsWith("#") || path.isAbsolute(id)) return false;
|
|
118
|
+
if (isBuiltinModuleSpecifier(id)) return true;
|
|
119
|
+
const importerPath = normalizeImporterPath(importer, options.path);
|
|
120
|
+
try {
|
|
121
|
+
if (!isPathWithinDirectory(projectNodeModules, createRequire(importerPath).resolve(id))) return false;
|
|
122
|
+
} catch {}
|
|
123
|
+
return true;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
77
127
|
//#endregion
|
|
78
128
|
//#region src/plugins/console-output-customizer.ts
|
|
79
129
|
/**
|
|
@@ -372,7 +422,7 @@ async function bundle(options) {
|
|
|
372
422
|
const inputOptions = {
|
|
373
423
|
input: options.path,
|
|
374
424
|
platform: "node",
|
|
375
|
-
external: (
|
|
425
|
+
external: createExternalResolver(options),
|
|
376
426
|
plugins: [
|
|
377
427
|
createMakeCjsWrapperAsyncFriendlyPlugin(),
|
|
378
428
|
createRequireResolveFix(options),
|
|
@@ -403,7 +453,11 @@ async function bundle(options) {
|
|
|
403
453
|
};
|
|
404
454
|
const rolldownOutput = await bundle$1.generate(outputOptions);
|
|
405
455
|
if (!rolldownOutput.output[0]) throw new Error("[unrun] No output chunk found");
|
|
406
|
-
|
|
456
|
+
const files = await bundle$1.watchFiles;
|
|
457
|
+
return {
|
|
458
|
+
chunk: rolldownOutput.output[0],
|
|
459
|
+
dependencies: files
|
|
460
|
+
};
|
|
407
461
|
}
|
|
408
462
|
|
|
409
463
|
//#endregion
|
|
@@ -539,14 +593,17 @@ async function loadModule(code, options) {
|
|
|
539
593
|
*/
|
|
540
594
|
async function unrun(options) {
|
|
541
595
|
const resolvedOptions = resolveOptions(options);
|
|
542
|
-
const
|
|
596
|
+
const output = await bundle(resolvedOptions);
|
|
543
597
|
let module;
|
|
544
598
|
try {
|
|
545
|
-
module = await loadModule(
|
|
599
|
+
module = await loadModule(output.chunk.code, resolvedOptions);
|
|
546
600
|
} catch (error) {
|
|
547
|
-
throw new Error(`[unrun] Import failed (code length: ${
|
|
601
|
+
throw new Error(`[unrun] Import failed (code length: ${output.chunk.code.length}): ${error.message}`);
|
|
548
602
|
}
|
|
549
|
-
return {
|
|
603
|
+
return {
|
|
604
|
+
module: preset(resolvedOptions, module),
|
|
605
|
+
dependencies: output.dependencies
|
|
606
|
+
};
|
|
550
607
|
}
|
|
551
608
|
/**
|
|
552
609
|
* Loads a module with JIT transpilation based on the provided options.
|
|
@@ -569,13 +626,13 @@ function unrunSync(options) {
|
|
|
569
626
|
*/
|
|
570
627
|
async function unrunCli(options, args = []) {
|
|
571
628
|
const resolvedOptions = resolveOptions(options);
|
|
572
|
-
const
|
|
573
|
-
const moduleUrl = writeModule(
|
|
629
|
+
const output = await bundle(resolvedOptions);
|
|
630
|
+
const moduleUrl = writeModule(output.chunk.code, resolvedOptions);
|
|
574
631
|
let cliResult;
|
|
575
632
|
try {
|
|
576
633
|
cliResult = await execModule(moduleUrl, args);
|
|
577
634
|
} catch (error) {
|
|
578
|
-
throw new Error(`[unrun] Run failed (code length: ${
|
|
635
|
+
throw new Error(`[unrun] Run failed (code length: ${output.chunk.code.length}): ${error.message}`);
|
|
579
636
|
}
|
|
580
637
|
cleanModule(moduleUrl, resolvedOptions);
|
|
581
638
|
return cliResult;
|
package/dist/sync/worker.mjs
CHANGED