forked from withastro/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sync): Add a sync and CJS entrypoint for editor tooling (withast…
…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
1 parent
a30c32c
commit b0e0cfd
Showing
8 changed files
with
416 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/compiler': patch | ||
--- | ||
|
||
Add a sync entrypoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './node/sync.cjs'; |
Oops, something went wrong.