Skip to content

Commit

Permalink
chore: format everything (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh authored Jul 12, 2024
1 parent 092b91b commit 4020602
Show file tree
Hide file tree
Showing 110 changed files with 2,869 additions and 2,557 deletions.
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

0 comments on commit 4020602

Please sign in to comment.