Skip to content

Commit bc30322

Browse files
committedFeb 24, 2025··
feat: support async and sync with quansync
1 parent b88948e commit bc30322

7 files changed

+91
-21
lines changed
 

‎build.config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineBuildConfig } from 'unbuild'
2+
import Quansync from 'unplugin-quansync/rollup'
23

34
export default defineBuildConfig({
45
entries: [
@@ -11,4 +12,9 @@ export default defineBuildConfig({
1112
inlineDependencies: true,
1213
},
1314
failOnWarn: false,
15+
hooks: {
16+
'rollup:options': function (context, options) {
17+
options.plugins.push(Quansync())
18+
},
19+
},
1420
})

‎eslint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export default antfu(
44
{
55
vue: false,
66
},
7-
)
7+
).removeRules(['prefer-arrow-callback'])

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
},
4646
"dependencies": {
4747
"mlly": "^1.7.4",
48-
"pkg-types": "^1.3.1"
48+
"pkg-types": "^1.3.1",
49+
"quansync": "^0.0.5"
4950
},
5051
"devDependencies": {
5152
"@antfu/eslint-config": "^4.3.0",
@@ -60,6 +61,7 @@
6061
"find-up-simple": "^1.0.0",
6162
"typescript": "^5.7.3",
6263
"unbuild": "^3.3.1",
64+
"unplugin-quansync": "^0.0.0",
6365
"vitest": "^3.0.6"
6466
}
6567
}

‎pnpm-lock.yaml

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.ts

+10-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { dirname, join, win32 } from 'node:path'
66
import process from 'node:process'
77
import { findUp } from 'find-up-simple'
88
import { interopDefault, resolvePathSync } from 'mlly'
9+
import { quansyncMacro } from 'quansync'
910

1011
export interface PackageInfo {
1112
name: string
@@ -76,28 +77,17 @@ function getPackageJsonPath(name: string, options: PackageResolvingOptions = {})
7677
return searchPackageJSON(entry)
7778
}
7879

79-
export async function getPackageInfo(name: string, options: PackageResolvingOptions = {}) {
80-
const packageJsonPath = getPackageJsonPath(name, options)
81-
if (!packageJsonPath)
82-
return
83-
84-
const packageJson: PackageJson = JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf8'))
80+
const readFile = quansyncMacro({
81+
async: (id: string) => fs.promises.readFile(id, 'utf8'),
82+
sync: id => fs.readFileSync(id, 'utf8'),
83+
})
8584

86-
return {
87-
name,
88-
version: packageJson.version,
89-
rootPath: dirname(packageJsonPath),
90-
packageJsonPath,
91-
packageJson,
92-
}
93-
}
94-
95-
export function getPackageInfoSync(name: string, options: PackageResolvingOptions = {}) {
85+
export const getPackageInfo = quansyncMacro(async function (name: string, options: PackageResolvingOptions = {}) {
9686
const packageJsonPath = getPackageJsonPath(name, options)
9787
if (!packageJsonPath)
9888
return
9989

100-
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
90+
const packageJson: PackageJson = JSON.parse(await readFile(packageJsonPath))
10191

10292
return {
10393
name,
@@ -106,7 +96,9 @@ export function getPackageInfoSync(name: string, options: PackageResolvingOption
10696
packageJsonPath,
10797
packageJson,
10898
}
109-
}
99+
})
100+
101+
export const getPackageInfoSync = getPackageInfo.sync
110102

111103
function resolvePackage(name: string, options: PackageResolvingOptions = {}) {
112104
try {

‎test/esm.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'node:fs'
22
import { join } from 'node:path'
33
import { expect } from 'chai'
44
// eslint-disable-next-line antfu/no-import-dist
5-
import { getPackageInfo, importModule, isPackageExists, loadPackageJSON, resolveModule } from '../dist/index.mjs'
5+
import { getPackageInfo, getPackageInfoSync, importModule, isPackageExists, loadPackageJSON, resolveModule } from '../dist/index.mjs'
66

77
console.warn('===== ESM =====')
88

@@ -14,6 +14,7 @@ async function run() {
1414
expect(isPackageExists('esno')).to.eq(true)
1515

1616
const info1 = await getPackageInfo('unbuild')
17+
expect(getPackageInfoSync('unbuild')).deep.eq(info1)
1718
expect(!!info1).to.eq(true)
1819
expect(info1.name).to.eq('unbuild')
1920
expect(info1.packageJson.name).to.eq('unbuild')

‎vitest.config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Quansync from 'unplugin-quansync/vite'
2+
import { defineConfig } from 'vitest/config'
3+
4+
export default defineConfig({
5+
plugins: [Quansync()],
6+
})

0 commit comments

Comments
 (0)
Please sign in to comment.