Skip to content

Commit

Permalink
feat: add alias support (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Dec 19, 2023
1 parent e22086d commit 798fe29
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 25 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ pub fn compute_root(root: Option<Buffer>) -> Option<PathBuf> {
}
}

// alias: module1:path1 module2:path2
// alias: name1=path1 name2=path2
pub fn compute_alias(root: &Option<PathBuf>, alias: Option<Buffer>) -> Option<Alias> {
match alias {
Some(buf) => {
let alias_str = String::from_utf8_lossy(&buf).to_string();
let alias: Alias = alias_str
.trim()
.split(" ")
.map(|s| {
let kv: Vec<&str> = s.split(":").collect();
let kv: Vec<&str> = s.split("=").collect();
let paths: Vec<AliasValue> = kv[1]
.split(",")
.map(|p| {
Expand Down
2 changes: 1 addition & 1 deletion crates/resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
oxc_resolver = "0.2.0"
oxc_resolver = "0.6.2"
easy_ast_error = { version = "0.1.0", path = "../error" }
regex = "1.10.2"
22 changes: 22 additions & 0 deletions packages/graph/__test__/alias.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'ava'
import {createParser, isWindows, readParsedFile} from './utils.mjs'


test('should parse alias path', async (t) => {
// skip in windows
if(isWindows) {
return t.truthy(true)
}

const parser = createParser({'@pkg': './nested'})

await parser.visit('./alias.js')
const parsed = parser.parse()
const expected = readParsedFile('./alias.json')
const foo = 'nested/foo.js'
t.truthy(parsed[foo].importer.includes('nested/bar.js'))
t.truthy(parsed[foo].importer.includes('alias.js'))
delete parsed[foo].importer
delete expected[foo].importer
t.deepEqual(parsed, expected)
})
3 changes: 3 additions & 0 deletions packages/graph/__test__/fixture/code/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {read} from 'fs'
import {foo} from '@pkg/foo'
export {bar} from '@pkg/bar'
83 changes: 83 additions & 0 deletions packages/graph/__test__/fixture/parsed/alias.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"nested/foo.js": {
"id": "nested/foo.js",
"type": "internal",
"importer": [
"alias.js",
"nested/bar.js"
],
"import": null
},
"alias.js": {
"id": "alias.js",
"type": "internal",
"importer": null,
"import": [
{
"id": "fs",
"type": "static",
"ident": [
{
"name": "read",
"as": "read",
"isType": false
}
],
"typeOnly": false
},
{
"id": "nested/foo.js",
"type": "static",
"ident": [
{
"name": "foo",
"as": "foo",
"isType": false
}
],
"typeOnly": false
},
{
"id": "nested/bar.js",
"type": "static",
"ident": [
{
"name": "bar",
"as": "bar",
"isType": false
}
],
"typeOnly": false
}
]
},
"fs": {
"id": "fs",
"type": "builtin",
"importer": [
"alias.js"
],
"import": null
},
"nested/bar.js": {
"id": "nested/bar.js",
"type": "internal",
"importer": [
"alias.js"
],
"import": [
{
"id": "nested/foo.js",
"type": "static",
"ident": [
{
"name": "foo",
"as": "foo",
"isType": false
}
],
"typeOnly": false
}
]
}
}
4 changes: 3 additions & 1 deletion packages/graph/__test__/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Parser } from '../index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

export const isWindows = /\bwin/.test(process.platform)
export const getCodeFile = (file) => join(__dirname, 'fixture/code', file)
export const getNodeModules = (file) => join('node_modules', file).replaceAll('\\', '/')
export const readParsedFile = (file, codeFiles = {}, nodeModules = {}, resolve = true) => {
Expand All @@ -19,7 +20,8 @@ export const readParsedFile = (file, codeFiles = {}, nodeModules = {}, resolve =

return JSON.parse(content)
}
export const parser = new Parser({ root: join(process.cwd(), '__test__/fixture/code') })
export const createParser = (alias) => new Parser({ root: join(process.cwd(), '__test__/fixture/code'), alias })
export const parser = createParser()
export const getGitRepo = repo => join(__dirname, '../../../repos', repo)
export const getGitRepoFiles = (repo, pattern, ignore) => {
const root = getGitRepo(repo)
Expand Down
9 changes: 8 additions & 1 deletion packages/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ export class Parser {
this.parser = new CoreParser(
Buffer.from(absRoot),
alias ? Buffer.from(
Object.keys(alias).reduce((s, key) => [s, `${key}:${alias[key]}`].join(' '), '')
Object
.keys(alias)
.reduce(
(s, key) => [s, `${key}=${
isAbsolute(alias[key]) ? alias[key] : resolve(absRoot, alias[key])
}`].join(' '),
''
)
) : undefined
)
}
Expand Down

0 comments on commit 798fe29

Please sign in to comment.