-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtransform.ts
142 lines (126 loc) · 3.88 KB
/
transform.ts
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { path } from "./lib/transform.deps.ts";
import init, * as wasmFuncs from "./lib/pkg/dnt_wasm.js";
import { source } from "./lib/pkg/dnt_wasm_bg.ts";
await init(source);
export interface Redirects {
/** The to and from specifier redirect. */
[specifier: string]: string;
}
/** Specifier to bare specifier mappings. */
export interface SpecifierMappings {
[specifier: string]: MappedSpecifier;
}
export interface MappedSpecifier {
/** Name of the npm package specifier to map to. */
name: string;
/** Version to use in the package.json file.
*
* Not specifying a version will exclude it from the package.json file.
* This is useful for built-in modules such as "fs".
*/
version?: string;
/** Sub path of the npm package to use in the module specifier.
*
* @remarks This should not include the package name and should not
* include a leading slash. It will be concatenated to the package
* name in the module specifier like so: `<package-name>/<sub-path>`
*/
subPath?: string;
}
export interface GlobalName {
/** Name to use as the global name. */
name: string;
/** Name of the export from the package.
* @remarks Defaults to the name. Specify `"default"` to use the default export.
*/
exportName?: string;
/** Whether this is a name that only exists as a type declaration. */
typeOnly?: boolean;
}
export interface Shim {
/** Information about the npm package or bare specifier to import. */
package: MappedSpecifier;
/** Named exports from the shim to use as globals. */
globalNames: (GlobalName | string)[];
}
export interface TransformOptions {
entryPoints: string[];
testEntryPoints?: string[];
shims?: Shim[];
testShims?: Shim[];
mappings?: SpecifierMappings;
redirects?: Redirects;
}
export interface Dependency {
name: string;
version: string;
}
export interface TransformOutput {
main: TransformOutputEnvironment;
test: TransformOutputEnvironment;
warnings: string[];
}
export interface TransformOutputEnvironment {
entryPoints: string[];
dependencies: Dependency[];
files: OutputFile[];
}
export interface OutputFile {
filePath: string;
fileText: string;
}
/** Analyzes the provided entry point to get all the dependended on modules and
* outputs canonical TypeScript code in memory. The output of this function
* can then be sent to the TypeScript compiler or a bundler for further processing. */
export function transform(options: TransformOptions): Promise<TransformOutput> {
if (options.entryPoints.length === 0) {
throw new Error("Specify one or more entry points.");
}
const newOptions = {
...options,
mappings: Object.fromEntries(
Object.entries(options.mappings ?? {}).map(([key, value]) => {
return [valueToUrl(key), value];
}),
),
redirects: Object.fromEntries(
Object.entries(options.redirects ?? {}).map(([key, value]) => {
return [valueToUrl(key), valueToUrl(value)];
}),
),
entryPoints: options.entryPoints.map(valueToUrl),
testEntryPoints: (options.testEntryPoints ?? []).map(valueToUrl),
shims: (options.shims ?? []).map(mapShim),
testShims: (options.testShims ?? []).map(mapShim),
};
return wasmFuncs.transform(newOptions);
}
function valueToUrl(value: string) {
const lowerCaseValue = value.toLowerCase();
if (
lowerCaseValue.startsWith("http://") ||
lowerCaseValue.startsWith("https://")
) {
return value;
} else {
return path.toFileUrl(path.resolve(value)).toString();
}
}
function mapShim(value: Shim): Shim {
return {
...value,
globalNames: value.globalNames.map(mapToGlobalName),
};
}
function mapToGlobalName(value: string | GlobalName): GlobalName {
if (typeof value === "string") {
return {
name: value,
typeOnly: false,
};
} else {
value.typeOnly ??= false;
return value;
}
}