-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
152 lines (144 loc) · 3.82 KB
/
mod.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
143
144
145
146
147
148
149
150
151
152
import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import init, {
parcelCSS,
parseDeps as parseDepsWasmFn,
transform as transformWasmFn,
} from "./dist/compiler.js";
import wasm from "./dist/wasm.js";
import type {
DependencyDescriptor,
TransformCSSOptions,
TransformCSSResult,
TransformOptions,
TransformResult,
} from "./types.ts";
import { VERSION } from "./version.ts";
let modulesCache: string | null = null;
let wasmReady: Promise<void> | boolean = false;
if (typeof Deno.run === "function") {
const p = Deno.run({
cmd: [Deno.execPath(), "info", "--json"],
stdout: "piped",
stderr: "null",
});
const output = (new TextDecoder()).decode(await p.output());
const info = JSON.parse(output);
modulesCache = info?.modulesCache || null;
await p.status();
p.close();
}
/* check whether or not the given path exists as regular file. */
async function existsFile(path: string): Promise<boolean> {
try {
const stat = await Deno.lstat(path);
return stat.isFile;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}
/** initialize the compiler wasm module. */
export async function initWasm() {
if (import.meta.url.startsWith("https://") && modulesCache) {
const cacheDir = join(
modulesCache,
`https/deno.land/x/aleph_compiler@${VERSION}/dist`,
);
const cachePath = join(cacheDir, "compiler.wasm");
if (await existsFile(cachePath)) {
const file = await Deno.open(cachePath, { read: true });
await init(
new Response(file.readable, {
headers: [["Content-Type", "application/wasm"]],
}),
);
} else {
const wasmData = await wasm();
await init(wasmData);
await ensureDir(cacheDir);
await Deno.writeFile(cachePath, new Uint8Array(wasmData));
}
} else {
await init(await wasm());
}
wasmReady = true;
}
async function getWasmReady() {
if (wasmReady === true) return;
if (wasmReady === false) {
wasmReady = initWasm().catch(() => {
wasmReady = false;
});
}
await wasmReady;
}
/** Parse the deps of the modules. */
export async function parseDeps(
specifier: string,
code: string,
options: Pick<TransformOptions, "importMap" | "lang"> = {},
): Promise<DependencyDescriptor[]> {
await getWasmReady();
return parseDepsWasmFn(specifier, code, options);
}
/**
* Transforms the JSX/TS module into a JS module.
*
* ```tsx
* transform(
* '/app.tsx',
* `
* import React from 'https://esm.sh/react';
*
* export default function App() {
* return <h1>Hello world!</h1>
* }
* `
* )
* ```
*/
export async function transform(
specifier: string,
code: string,
options: TransformOptions = {},
): Promise<TransformResult> {
await getWasmReady();
try {
return transformWasmFn(specifier, code, options);
} catch (error) {
if (
options.minify &&
(error.stack ?? error.messsage ?? "").includes("ThreadPoolBuildError")
) {
// retry and disable minify if ThreadPoolBuildError
if (options.minify.compress) {
return await transform(specifier, code, {
...options,
minify: { compress: false },
});
} else {
return transformWasmFn(specifier, code, {
...options,
minify: undefined,
});
}
} else {
throw error;
}
}
}
/**
* Compiles a CSS file, including optionally minifying and lowering syntax to the given
* targets. A source map may also be generated, but this is not enabled by default.
*/
export async function transformCSS(
specifier: string,
code: string,
options: TransformCSSOptions = {},
): Promise<TransformCSSResult> {
await getWasmReady();
return parcelCSS(specifier, code, options);
}