Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: format everything #1020

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
"files": {
"ignore": ["**/dist", "**/pnpm-lock.yaml"],
"ignore": ["**/dist", "**/pnpm-lock.yaml", "wasm_exec.ts"],
"include": ["packages/**"]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentStyle": "tab",
"indentWidth": 2,
"lineWidth": 180
"lineWidth": 100
},
"organizeImports": {
"enabled": true
Expand Down Expand Up @@ -36,12 +36,24 @@
},
"javascript": {
"formatter": {
"quoteStyle": "single"
"trailingCommas": "es5",
"quoteStyle": "single",
"semicolons": "always"
}
},
"json": {
"parser": {
"allowComments": true,
"allowTrailingCommas": true
},
"formatter": {
"indentStyle": "space",
"trailingCommas": "none"
}
},
"overrides": [
{
"include": ["package.json", "biome.jsonc"],
"include": ["package.json"],
"json": {
"formatter": {
"lineWidth": 1
Expand Down
110 changes: 61 additions & 49 deletions packages/compiler/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,95 @@ import type * as types from '../shared/types';
import Go from './wasm_exec.js';

export const transform: typeof types.transform = (input, options) => {
return ensureServiceIsRunning().transform(input, options);
return ensureServiceIsRunning().transform(input, options);
};

export const parse: typeof types.parse = (input, options) => {
return ensureServiceIsRunning().parse(input, options);
return ensureServiceIsRunning().parse(input, options);
};

export const convertToTSX: typeof types.convertToTSX = (input, options) => {
return ensureServiceIsRunning().convertToTSX(input, options);
return ensureServiceIsRunning().convertToTSX(input, options);
};

interface Service {
transform: typeof types.transform;
parse: typeof types.parse;
convertToTSX: typeof types.convertToTSX;
transform: typeof types.transform;
parse: typeof types.parse;
convertToTSX: typeof types.convertToTSX;
}

let initializePromise: Promise<Service> | undefined;
let longLivedService: Service | undefined;

export const teardown: typeof types.teardown = () => {
initializePromise = undefined;
longLivedService = undefined;
(globalThis as any)['@astrojs/compiler'] = undefined;
initializePromise = undefined;
longLivedService = undefined;
(globalThis as any)['@astrojs/compiler'] = undefined;
};

export const initialize: typeof types.initialize = async (options) => {
let wasmURL = options.wasmURL;
if (!wasmURL) throw new Error('Must provide the "wasmURL" option');
wasmURL += '';
if (!initializePromise) {
initializePromise = startRunningService(wasmURL).catch((err) => {
// Let the caller try again if this fails.
initializePromise = void 0;
// But still, throw the error back up the caller.
throw err;
});
}
longLivedService = longLivedService || (await initializePromise);
let wasmURL = options.wasmURL;
if (!wasmURL) throw new Error('Must provide the "wasmURL" option');
wasmURL += '';
if (!initializePromise) {
initializePromise = startRunningService(wasmURL).catch((err) => {
// Let the caller try again if this fails.
initializePromise = void 0;
// But still, throw the error back up the caller.
throw err;
});
}
longLivedService = longLivedService || (await initializePromise);
};

const ensureServiceIsRunning = (): Service => {
if (!initializePromise) throw new Error('You need to call "initialize" before calling this');
if (!longLivedService) throw new Error('You need to wait for the promise returned from "initialize" to be resolved before calling this');
return longLivedService;
if (!initializePromise) throw new Error('You need to call "initialize" before calling this');
if (!longLivedService)
throw new Error(
'You need to wait for the promise returned from "initialize" to be resolved before calling this'
);
return longLivedService;
};

const instantiateWASM = async (wasmURL: string, importObject: Record<string, any>): Promise<WebAssembly.WebAssemblyInstantiatedSource> => {
let response = undefined;
const instantiateWASM = async (
wasmURL: string,
importObject: Record<string, any>
): Promise<WebAssembly.WebAssemblyInstantiatedSource> => {
let response = undefined;

if (WebAssembly.instantiateStreaming) {
response = await WebAssembly.instantiateStreaming(fetch(wasmURL), importObject);
} else {
const fetchAndInstantiateTask = async () => {
const wasmArrayBuffer = await fetch(wasmURL).then((res) => res.arrayBuffer());
return WebAssembly.instantiate(wasmArrayBuffer, importObject);
};
response = await fetchAndInstantiateTask();
}
if (WebAssembly.instantiateStreaming) {
response = await WebAssembly.instantiateStreaming(fetch(wasmURL), importObject);
} else {
const fetchAndInstantiateTask = async () => {
const wasmArrayBuffer = await fetch(wasmURL).then((res) => res.arrayBuffer());
return WebAssembly.instantiate(wasmArrayBuffer, importObject);
};
response = await fetchAndInstantiateTask();
}

return response;
return response;
};

const startRunningService = async (wasmURL: string): Promise<Service> => {
const go = new Go();
const wasm = await instantiateWASM(wasmURL, go.importObject);
go.run(wasm.instance);
const go = new Go();
const wasm = await instantiateWASM(wasmURL, go.importObject);
go.run(wasm.instance);

const service: any = (globalThis as any)['@astrojs/compiler'];
const service: any = (globalThis as any)['@astrojs/compiler'];

return {
transform: (input, options) => new Promise((resolve) => resolve(service.transform(input, options || {}))),
convertToTSX: (input, options) =>
new Promise((resolve) => resolve(service.convertToTSX(input, options || {}))).then((result: any) => ({
...result,
map: JSON.parse(result.map),
})),
parse: (input, options) => new Promise((resolve) => resolve(service.parse(input, options || {}))).then((result: any) => ({ ...result, ast: JSON.parse(result.ast) })),
};
return {
transform: (input, options) =>
new Promise((resolve) => resolve(service.transform(input, options || {}))),
convertToTSX: (input, options) =>
new Promise((resolve) => resolve(service.convertToTSX(input, options || {}))).then(
(result: any) => ({
...result,
map: JSON.parse(result.map),
})
),
parse: (input, options) =>
new Promise((resolve) => resolve(service.parse(input, options || {}))).then(
(result: any) => ({ ...result, ast: JSON.parse(result.ast) })
),
};
};
Loading