Skip to content

Commit

Permalink
feat(sync): Add a sync and CJS entrypoint for editor tooling (withast…
Browse files Browse the repository at this point in the history
…ro#772)

* feat(sync): Add a sync version of transform, parse, convertToTSX etc

* chore: changeset

* feat(sync): Fully sync, CJS entrypoint

* fix: types

* fix: proper cjs output

* fix: .js extension
  • Loading branch information
Princesseuh authored Apr 18, 2023
1 parent a30c32c commit b0e0cfd
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 84 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-files-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Add a sync entrypoint
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
],
"devDependencies": {
"@changesets/cli": "^2.25.0",
"@typescript-eslint/eslint-plugin": "^5.39.0",
"@typescript-eslint/parser": "^5.39.0",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"prettier": "^2.8.7",
"sass": "^1.55.0",
"tsx": "^3.10.1",
"typescript": "~4.8.4",
"typescript": "~4.9.0",
"uvu": "^0.5.6"
},
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ browser/**/*.js
browser/**/*.d.ts
node/**/*.js
node/**/*.d.ts
node/**/*.cjs
node/**/*.d.cts
shared/**/*.js
shared/**/*.d.ts
6 changes: 3 additions & 3 deletions packages/compiler/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type { PreprocessorResult, ParseOptions, TransformOptions, HoistedScript, TransformResult, ParseResult } from '../shared/types';
import type * as types from '../shared/types';
export type { HoistedScript, ParseOptions, ParseResult, PreprocessorResult, TransformOptions, TransformResult } from '../shared/types';
import { promises as fs } from 'fs';
import Go from './wasm_exec.js';
import { fileURLToPath } from 'url';
import type * as types from '../shared/types';
import Go from './wasm_exec.js';

export const transform: typeof types.transform = async (input, options) => {
return getService().then((service) => service.transform(input, options));
Expand Down
62 changes: 62 additions & 0 deletions packages/compiler/node/sync.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import type * as types from '../shared/types';
import Go from './wasm_exec.js';

type UnwrappedPromise<T> = T extends (...params: any) => Promise<infer Return> ? (...params: Parameters<T>) => Return : T;

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

function getService(): Service {
if (!longLivedService) {
longLivedService = startRunningService();
}
return longLivedService;
}

let longLivedService: Service | undefined;

export const transform = ((input, options) => getService().transform(input, options)) satisfies Service['transform'];

export const parse = ((input, options) => {
return getService().parse(input, options);
}) satisfies Service['parse'];

export const convertToTSX = ((input, options) => {
return getService().convertToTSX(input, options);
}) satisfies Service['convertToTSX'];

export function startRunningService(): Service {
const go = new Go();
const wasm = instantiateWASM(join(__dirname, '../astro.wasm'), go.importObject);
go.run(wasm);
const _service: any = (globalThis as any)['@astrojs/compiler'];
return {
transform: (input, options) => {
try {
return _service.transform(input, options || {});
} catch (err) {
// Recreate the service next time on panic
longLivedService = void 0;
throw err;
}
},
parse: (input, options) => {
const result = _service.parse(input, options || {});
return { ...result, ast: JSON.parse(result.ast) };
},
convertToTSX: (input, options) => {
const result = _service.convertToTSX(input, options || {});
return { ...result, map: JSON.parse(result.map) };
},
};
}

function instantiateWASM(wasmURL: string, importObject: Record<string, any>): WebAssembly.Instance {
const wasmArrayBuffer = readFileSync(wasmURL);
return new WebAssembly.Instance(new WebAssembly.Module(wasmArrayBuffer), importObject);
}
11 changes: 8 additions & 3 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"homepage": "https://astro.build",
"version": "1.3.1",
"scripts": {
"build": "tsc -p ."
"build": "tsc -p . && esbuild node/sync.cts --bundle --format=cjs --platform=node --outfile=node/sync.cjs"
},
"main": "./node/index.js",
"types": "./node",
Expand All @@ -21,6 +21,10 @@
"import": "./node/index.js",
"default": "./browser/index.js"
},
"./sync": {
"import": "./node/sync.cjs",
"default": "./node/sync.cjs"
},
"./utils": {
"browser": "./browser/utils.js",
"import": "./node/utils.js",
Expand All @@ -31,9 +35,10 @@
},
"devDependencies": {
"@jridgewell/trace-mapping": "^0.3.16",
"@types/node": "^16.11.64",
"@types/node": "^18.15.11",
"@types/sass": "^1.43.1",
"acorn": "^8.8.1",
"typescript": "~4.8.4"
"esbuild": "^0.17.17",
"typescript": "~4.9.0"
}
}
1 change: 1 addition & 0 deletions packages/compiler/sync.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './node/sync.cjs';
Loading

0 comments on commit b0e0cfd

Please sign in to comment.