-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- evanw/esbuild#1662 - golang/go#59099 confusing output from esbuild-wasm even after supposed end of process
- Loading branch information
Showing
11 changed files
with
424 additions
and
17 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,46 @@ | ||
<div align="center"> | ||
|
||
![Ganesha](ganesha.svg) | ||
|
||
**Fast TypeScript loader based on a WASM build of esbuild.** | ||
|
||
[![](https://img.shields.io/npm/v/@ganesha/esbuild?color=%23f68f21&style=for-the-badge&label=@ganesha/esbuild)](https://fadroma.tech) | ||
|
||
Made with [🧡](mailto:hello@hack.bg) at [Hack.bg](https://hack.bg). | ||
|
||
</div> | ||
|
||
--- | ||
|
||
## How to use | ||
|
||
Install with: | ||
|
||
```sh | ||
npm i --save @ganesha/esbuild | ||
``` | ||
|
||
Use from shell: | ||
|
||
```sh | ||
node --import @ganesha/esbuild my-program.ts | ||
``` | ||
|
||
Use from script: | ||
|
||
```js | ||
await import("@ganesha/esbuild") | ||
await import("./my-program.ts") | ||
``` | ||
|
||
--- | ||
|
||
<div align="center"> | ||
|
||
>*The very day I was born I made my first mistake, | ||
>and by that path have I sought wisdom ever since. | ||
> | ||
> - **William Buck*** | ||
</div> | ||
|
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 @@ | ||
../ganesha.svg |
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,79 @@ | ||
import * as esbuild from 'esbuild-wasm' | ||
import { fileURLToPath, pathToFileURL } from 'node:url' | ||
import { extname } from 'node:path' | ||
import { readFile, stat } from 'node:fs/promises' | ||
import { getTsconfig, parseTsconfig } from 'get-tsconfig' | ||
import createCache from '@ganesha/caching' | ||
|
||
const debug = (...args) => process.env.GANESHA_DEBUG && | ||
process.stderr.write(args.join(' ') + '\n') | ||
|
||
await esbuild.initialize() | ||
|
||
const typeScriptExtensions = ['.ts', '.mts', '.cts'] | ||
|
||
const cache = await createCache() | ||
|
||
export async function initialize (context) {} | ||
|
||
export async function resolve (specifier, context, next) { | ||
debug('Resolve: ', specifier) | ||
try { | ||
return await next(specifier, context, next) | ||
} catch (e) { | ||
if (specifier.startsWith('./') || specifier.startsWith('../')) { | ||
const path = fileURLToPath(new URL(specifier, context?.parentURL).href) | ||
const extension = extname(path).toLowerCase() | ||
if (!(typeScriptExtensions.includes(extension))) { | ||
const extended = path + '.ts' | ||
try { | ||
if (await stat(extended)) { | ||
return { shortCircuit: true, url: pathToFileURL(extended).href } | ||
} | ||
} catch (e) { | ||
e.parentURL = context?.parentURL | ||
throw e | ||
} | ||
} | ||
} | ||
throw e | ||
} | ||
} | ||
|
||
export async function load (url, context, next) { | ||
debug('Load: ', url) | ||
if (url.startsWith('file://')) { | ||
const path = fileURLToPath(url) | ||
const extension = extname(path).toLowerCase() | ||
if (typeScriptExtensions.includes(extension)) { | ||
debug('Transform:', url) | ||
const source = await readFile(path, 'utf8') | ||
if (!process.env.GANESHA_CACHE_OFF) { | ||
const cached = await cache.get(source) | ||
if (cached) { | ||
debug('Cached: ', cached.key) | ||
return { format: 'module', shortCircuit: true, source: cached.output } | ||
} | ||
} | ||
const config = await readFile(getTsconfig(path).path, 'utf8') | ||
const { code, warnings, map } = await esbuild.transform(source, { | ||
sourcefile: path, | ||
sourcemap: 'both', | ||
loader: 'ts', | ||
target: config.target, | ||
format: 'esm' | ||
}) | ||
if (warnings && warnings.length > 0) { | ||
for (const warning of warnings) { | ||
console.warn(`${warning.location}: ${warning.text}`) | ||
} | ||
} | ||
if (!process.env.GANESHA_CACHE_OFF) { | ||
const key = await cache.put(source, code) | ||
debug('Caching: ', key) | ||
} | ||
return { format: 'module', shortCircuit: true, source: code } | ||
} | ||
} | ||
return await next(url, context, next) | ||
} |
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,25 @@ | ||
{ | ||
"name": "@ganesha/esbuild-wasm", | ||
"type": "module", | ||
"main": "register.mjs", | ||
"version": "1.0.0-rc.1", | ||
"description": "🐘 Fast TypeScript loader based on the WASM build of esbuild.", | ||
"keywords": [ "esbuild", "esbuild-wasm", "wasm", "typescript", "loader" ], | ||
"files": [ | ||
"ganesha.svg", | ||
"register.mjs", | ||
"loader.mjs" | ||
], | ||
"scripts": { | ||
"test:loader": "node --import ./register.mjs ../tests/typescript.test.ts", | ||
"release": "npm run test:loader && ubik release --otp 000000" | ||
}, | ||
"dependencies": { | ||
"@ganesha/caching": "workspace:^", | ||
"get-tsconfig": "^4.7.2", | ||
"esbuild-wasm": "^0.19.11" | ||
}, | ||
"devDependencies": { | ||
"@hackbg/ubik": "^3.0.1" | ||
} | ||
} |
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,2 @@ | ||
import { register } from 'node:module' | ||
register('./loader.mjs', { parentURL: import.meta.url }) |
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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
import { register } from 'node:module' | ||
register('./loader.mjs', { | ||
parentURL: import.meta.url | ||
}) | ||
register('./loader.mjs', { parentURL: import.meta.url }) |
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
Oops, something went wrong.