Skip to content

Commit

Permalink
esbuild: add native option
Browse files Browse the repository at this point in the history
- evanw/esbuild#1662
- golang/go#59099

confusing output from esbuild-wasm even after supposed end of process
  • Loading branch information
egasimus committed Jan 22, 2024
1 parent 2806f84 commit f5c6095
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 17 deletions.
46 changes: 46 additions & 0 deletions esbuild-wasm/README.md
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>

1 change: 1 addition & 0 deletions esbuild-wasm/ganesha.svg
79 changes: 79 additions & 0 deletions esbuild-wasm/loader.mjs
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)
}
25 changes: 25 additions & 0 deletions esbuild-wasm/package.json
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"
}
}
2 changes: 2 additions & 0 deletions esbuild-wasm/register.mjs
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 })
4 changes: 1 addition & 3 deletions esbuild/loader.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as esbuild from 'esbuild-wasm'
import * as esbuild from 'esbuild'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { extname } from 'node:path'
import { readFile, stat } from 'node:fs/promises'
Expand All @@ -8,8 +8,6 @@ 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()
Expand Down
10 changes: 4 additions & 6 deletions esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"name": "@ganesha/esbuild",
"type": "module",
"main": "register.mjs",
"version": "1.0.0-rc.2",
"description": "🐘 Fast TypeScript loader based on the WASM build of esbuild.",
"version": "1.0.0-rc.3",
"description": "🐘 Fast TypeScript loader based on the native build of esbuild.",
"keywords": [ "esbuild", "esbuild-wasm", "wasm", "typescript", "loader" ],
"files": [
"ganesha.svg",
"register.mjs",
"loader.mjs",
"pkg/ganesha_ezno.js",
"pkg/ganesha_ezno_bg.wasm"
"loader.mjs"
],
"scripts": {
"test:loader": "node --import ./register.mjs ../tests/typescript.test.ts",
Expand All @@ -19,7 +17,7 @@
"dependencies": {
"@ganesha/caching": "workspace:^",
"get-tsconfig": "^4.7.2",
"esbuild-wasm": "^0.19.11"
"esbuild": "^0.19.11"
},
"devDependencies": {
"@hackbg/ubik": "^3.0.1"
Expand Down
4 changes: 1 addition & 3 deletions esbuild/register.mjs
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 })
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"release": "ubik release --otp 000000"
},
"devDependencies": {
"@hackbg/ubik": "^3.0.1",
"@ganesha/caching": "workspace:^",
"@ganesha/esbuild": "workspace:^",
"@ganesha/oxc": "workspace:^",
"rimraf": "^5.0.5"
"@ganesha/caching": "workspace:^",
"@ganesha/esbuild": "workspace:^",
"@ganesha/esbuild-wasm": "workspace:^",
"@ganesha/oxc": "workspace:^",

"@hackbg/ubik": "^3.0.1",
"rimraf": "^5.0.5"
}
}
Loading

0 comments on commit f5c6095

Please sign in to comment.