Skip to content

Commit

Permalink
0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Jul 4, 2022
1 parent 43eba0c commit 858af36
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph-compiler"
version = "0.6.8"
version = "0.7.0"
description = "The compiler of Aleph.js written in Rust."
repository = "https://github.com/alephjs/aleph.js"
authors = ["The Aleph.js authors"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The compiler of Aleph.js written in Rust, powered by [swc](https://github.com/sw
## Usage

```ts
import { transform } from "https://deno.land/x/aleph_compiler@0.3.0/mod.ts";
import { transform } from "https://deno.land/x/aleph_compiler@0.7.0/mod.ts";

const code = `
import { useState, useEffect } from "react"
Expand Down
21 changes: 7 additions & 14 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { dim } from "https://deno.land/std@0.144.0/fmt/colors.ts";
import { encode } from "https://deno.land/std@0.144.0/encoding/base64.ts";
import { ensureDir } from "https://deno.land/std@0.144.0/fs/ensure_dir.ts";
import { dirname } from "https://deno.land/std@0.144.0/path/mod.ts";
import { compress } from "https://deno.land/x/lz4@v0.1.2/mod.ts";
import { dim } from "https://deno.land/std@0.145.0/fmt/colors.ts";
import { ensureDir } from "https://deno.land/std@0.145.0/fs/ensure_dir.ts";
import { dirname } from "https://deno.land/std@0.145.0/path/mod.ts";

async function run(cmd: string[]) {
const p = Deno.run({
Expand All @@ -22,19 +20,14 @@ if (import.meta.main) {
if (ok) {
let prevWasmSize: number;
try {
prevWasmSize = (await Deno.stat("./dist/wasm.js")).size;
prevWasmSize = (await Deno.stat("./dist/compiler.wasm")).size;
} catch (_e) {
prevWasmSize = 0;
}
const wasmData = await Deno.readFile("./pkg/aleph_compiler_bg.wasm");
const jsCode = await Deno.readTextFile("./pkg/aleph_compiler.js");
await ensureDir("./dist");
await Deno.writeTextFile(
"./dist/wasm.js",
`import { decompress } from "https://deno.land/x/lz4@v0.1.2/mod.ts";\nexport default () => decompress(Uint8Array.from(atob("${
encode(compress(wasmData))
}"), c => c.charCodeAt(0)));`,
);
await Deno.writeFile("./dist/compiler.wasm", wasmData);
await Deno.writeTextFile(
"./dist/compiler.js",
jsCode
Expand All @@ -57,11 +50,11 @@ if (import.meta.main) {
),
);
await run(["deno", "fmt", "-q", "./dist/compiler.js"]);
const wasmSize = (await Deno.stat("./dist/wasm.js")).size;
const wasmSize = (await Deno.stat("./dist/compiler.wasm")).size;
const changed = ((wasmSize - prevWasmSize) / prevWasmSize) * 100;
if (changed) {
console.log(
`${dim("[INFO]")}: wasm.js ${changed < 0 ? "-" : "+"}${
`${dim("[INFO]")}: compiler.wasm ${changed < 0 ? "-" : "+"}${
Math.abs(changed).toFixed(2)
}% (${
[prevWasmSize, wasmSize].filter(Boolean).map((n) =>
Expand Down
52 changes: 2 additions & 50 deletions dist/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,47 +112,6 @@ function takeObject(idx) {
dropObject(idx);
return ret;
}
/**
* @param {string} specifier
* @param {string} code
* @param {any} options
* @returns {any}
*/
export function parseExportNames(specifier, code, options) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(
specifier,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passStringToWasm0(
code,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len1 = WASM_VECTOR_LEN;
wasm.parseExportNames(
retptr,
ptr0,
len0,
ptr1,
len1,
addHeapObject(options),
);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return takeObject(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

/**
* @param {string} specifier
* @param {string} code
Expand Down Expand Up @@ -227,7 +186,7 @@ export function transform(specifier, code, options) {
* @param {any} config_val
* @returns {any}
*/
export function transformCSS(filename, code, config_val) {
export function parcelCSS(filename, code, config_val) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(
Expand All @@ -242,14 +201,7 @@ export function transformCSS(filename, code, config_val) {
wasm.__wbindgen_realloc,
);
const len1 = WASM_VECTOR_LEN;
wasm.transformCSS(
retptr,
ptr0,
len0,
ptr1,
len1,
addHeapObject(config_val),
);
wasm.parcelCSS(retptr, ptr0, len0, ptr1, len1, addHeapObject(config_val));
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
Expand Down
Binary file added dist/compiler.wasm
Binary file not shown.
2 changes: 0 additions & 2 deletions dist/wasm.js

This file was deleted.

75 changes: 44 additions & 31 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { ensureDir } from "https://deno.land/std@0.144.0/fs/ensure_dir.ts";
import { dirname, join } from "https://deno.land/std@0.144.0/path/mod.ts";
import { VERSION } from "./version.ts";
import { ensureDir } from "https://deno.land/std@0.145.0/fs/ensure_dir.ts";
import { dirname, join } from "https://deno.land/std@0.145.0/path/mod.ts";
import init, {
parcelCSS,
parseDeps as parseDepsWasmFn,
parseExportNames as parseExportNamesWasmFn,
transform as transformWasmFn,
transformCSS as parcelCSS,
} from "./dist/compiler.js";
import decodeWasmData from "./dist/wasm.js";
import {
import { VERSION } from "./version.ts";
import type {
DependencyDescriptor,
TransformCSSOptions,
TransformCSSResult,
TransformOptions,
TransformResult,
} from "./types.ts";

let modulesCache: string | null = null;
let wasmReady: Promise<void> | boolean = false;

async function checkWasmReady() {
if (wasmReady === false) {
wasmReady = initWasm();
}
if (wasmReady instanceof Promise) {
await wasmReady;
wasmReady = true;
}
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. */
Expand All @@ -43,33 +45,44 @@ async function existsFile(path: string): Promise<boolean> {

/* initialize the compiler wasm module. */
async function initWasm() {
const mcDir = Deno.env.get("MODULES_CACHE_DIR");
if (mcDir) {
const cacheDir = join(mcDir, `https/deno.land/x/aleph_compiler/dist`);
const cachePath = `${cacheDir}/aleph_compiler.${VERSION}.wasm`;
if (import.meta.url.startsWith("file://")) {
const wasmData = await Deno.readFile(
new URL("./dist/compiler.wasm", import.meta.url).pathname,
);
await init(wasmData);
} else if (modulesCache) {
const cacheDir = join(
modulesCache,
`https/deno.land/x/aleph_compiler@${VERSION}/dist`,
);
const cachePath = `${cacheDir}/compiler.wasm`;
if (await existsFile(cachePath)) {
const wasmData = await Deno.readFile(cachePath);
await init(wasmData);
} else {
const wasmData = decodeWasmData();
const res = await fetch(
new URL("./dist/compiler.wasm", import.meta.url),
);
const wasmData = await res.arrayBuffer();
await init(wasmData);
await ensureDir(dirname(cachePath));
await Deno.writeFile(cachePath, wasmData);
await Deno.writeFile(cachePath, new Uint8Array(wasmData));
}
} else {
const wasmData = decodeWasmData();
await init(wasmData);
await init(fetch(
new URL("./dist/compiler.wasm", import.meta.url),
));
}
}

/** Parse export names of the module. */
export async function parseExportNames(
specifier: string,
code: string,
options: Pick<TransformOptions, "lang"> = {},
): Promise<string[]> {
await checkWasmReady();
return parseExportNamesWasmFn(specifier, code, options);
async function checkWasmReady() {
if (wasmReady === false) {
wasmReady = initWasm();
}
if (wasmReady instanceof Promise) {
await wasmReady;
wasmReady = true;
}
}

/** Parse the deps of the modules. */
Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from "https://deno.land/std@0.144.0/testing/asserts.ts";
import "https://deno.land/x/global@0.144.0/testing.ts";
import { transform } from "./mod.ts";

Deno.test("swc", async (t) => {
Expand Down
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** `VERSION` managed by https://deno.land/x/publish */
export const VERSION = "0.6.8";
export const VERSION = "0.7.0";

/** `prepublish` will be invoked before publish */
export async function prepublish(version: string): Promise<boolean> {
Expand Down

0 comments on commit 858af36

Please sign in to comment.