Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 058445e

Browse files
authored
refactor: use esbuild as resolver, eliminating a bunch of deps @hanayashiki (#26)
1 parent c083765 commit 058445e

File tree

10 files changed

+189
-205
lines changed

10 files changed

+189
-205
lines changed

loader.mjs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
11
import { URL, pathToFileURL, fileURLToPath } from 'url'
22
import fs from 'fs'
3-
import path from 'path'
4-
import { transformSync } from 'esbuild'
5-
import typescript from '@rollup/plugin-typescript'
6-
import JoyCon from 'joycon'
3+
import { dirname } from 'path'
4+
import { transformSync, build } from 'esbuild'
75

86
const isWindows = process.platform === 'win32'
97

108
const extensionsRegex = /\.m?(tsx?|json)$/
11-
const tsconfigPath = new JoyCon({ parseJSON: () => {} }).loadSync(['tsconfig.json']).path
12-
const pluginTypescript = typescript({
13-
tsconfig: tsconfigPath,
14-
})
9+
10+
async function esbuildResolve(id, dir) {
11+
let result
12+
13+
await build({
14+
stdin: {
15+
contents: `import ${JSON.stringify(id)}`,
16+
resolveDir: dir,
17+
},
18+
write: false,
19+
bundle: true,
20+
treeShaking: false,
21+
ignoreAnnotations: true,
22+
platform: 'node',
23+
plugins: [{
24+
name: 'resolve',
25+
setup({ onLoad }) {
26+
onLoad({ filter: /.*/ }, (args) => {
27+
result = args.path
28+
return { contents: '' }
29+
})
30+
},
31+
}],
32+
})
33+
return result
34+
}
1535

1636
function esbuildTransformSync(rawSource, filename, url, format) {
1737
const {
@@ -85,8 +105,9 @@ export async function resolve(specifier, context, defaultResolve) {
85105
else {
86106
// Try to resolve the module according to typescript's algorithm,
87107
// and construct a valid url.
108+
88109
const parsed = getTsCompatSpecifier(parentURL, specifier)
89-
const path = pluginTypescript.resolveId(parsed.tsSpecifier, fileURLToPath(parentURL))
110+
const path = await esbuildResolve(parsed.tsSpecifier, dirname(fileURLToPath(parentURL)))
90111
if (path) {
91112
url = pathToFileURL(path)
92113
url.search = parsed.search

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
"test": "node --experimental-loader ./loader.mjs test/entry.ts"
3434
},
3535
"dependencies": {
36-
"@rollup/plugin-typescript": "^8.3.0",
37-
"esbuild": "^0.13.12",
38-
"joycon": "^3.0.1"
36+
"esbuild": "^0.13.12"
3937
},
4038
"peerDependencies": {
4139
"typescript": "^4.0"
@@ -47,7 +45,6 @@
4745
"bumpp": "^7.1.1",
4846
"eslint": "^8.2.0",
4947
"execa": "^5.1.1",
50-
"typescript": "^4.4.4",
5148
"uvu": "^0.5.2"
5249
}
5350
}

0 commit comments

Comments
 (0)