-
Notifications
You must be signed in to change notification settings - Fork 170
/
mod.ts
108 lines (101 loc) · 2.84 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
import { ensureDir } from "https://deno.land/std@0.136.0/fs/ensure_dir.ts";
import { dirname, join } from "https://deno.land/std@0.136.0/path/mod.ts";
import { existsFile } from "../lib/fs.ts";
import { isCanary, VERSION } from "../version.ts";
import init, {
parseDeps as parseDepsWasmFn,
parseExportNames as parseExportNamesWasmFn,
transform as transformWasmFn,
transformCSS as parcelCSS,
} from "./dist/compiler.js";
import decodeWasmData from "./dist/wasm.js";
import {
DependencyDescriptor,
TransformCSSOptions,
TransformCSSResult,
TransformOptions,
TransformResult,
} from "./types.ts";
let wasmReady: Promise<void> | boolean = false;
async function checkWasmReady() {
if (wasmReady === false) {
wasmReady = initWasm();
}
if (wasmReady instanceof Promise) {
await wasmReady;
wasmReady = true;
}
}
async function initWasm() {
const mcdir = Deno.env.get("MODULES_CACHE_DIR");
if (mcdir && !Deno.env.get("ALEPH_DEV")) {
const pkgName = isCanary ? "aleph_canary" : "aleph";
const cacheDir = join(mcdir, `https/deno.land/x/${pkgName}`);
const cachePath = `${cacheDir}/compiler.${VERSION}.wasm`;
if (await existsFile(cachePath)) {
const wasmData = await Deno.readFile(cachePath);
await init(wasmData);
} else {
const wasmData = decodeWasmData();
await init(wasmData);
await ensureDir(dirname(cachePath));
await Deno.writeFile(cachePath, wasmData);
}
} else {
const wasmData = decodeWasmData();
await init(wasmData);
}
}
/** parse export names */
export async function parseExportNames(
specifier: string,
code: string,
): Promise<string[]> {
await checkWasmReady();
return parseExportNamesWasmFn(specifier, code);
}
/** fast transform without transpile code raw syntax */
export async function parseDeps(
specifier: string,
code: string,
options: Pick<TransformOptions, "importMap" | "lang"> = {},
): Promise<DependencyDescriptor[]> {
await checkWasmReady();
return parseDepsWasmFn(specifier, code, options);
}
/**
* Transforms the module with esbuild/swc.
*
* ```tsx
* transform(
* '/app.tsx',
* `
* import React from 'https://esm.sh/react';
*
* export default App() {
* return <h1>Hello world!</h1>
* }
* `
* )
* ```
*/
export async function transform(
specifier: string,
code: string,
options: TransformOptions = {},
): Promise<TransformResult> {
await checkWasmReady();
return transformWasmFn(specifier, code, options);
}
/**
* 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 checkWasmReady();
return parcelCSS(specifier, code, options);
}