Write async functions, get both async and sync functions with
quansync and compiler magics.
npm i -D unplugin-quansync
// vite.config.ts
import Quansync from 'unplugin-quansync/vite'
export default defineConfig({
plugins: [Quansync()],
})
// rollup.config.js
import Quansync from 'unplugin-quansync/rollup'
export default {
plugins: [Quansync()],
}
// rolldown.config.js
import Quansync from 'unplugin-quansync/rolldown'
export default {
plugins: [Quansync()],
}
import { build } from 'esbuild'
import Quansync from 'unplugin-quansync/esbuild'
build({
plugins: [Quansync()],
})
// webpack.config.js
import Quansync from 'unplugin-quansync/webpack'
export default {
/* ... */
plugins: [Quansync()],
}
// rspack.config.js
import Quansync from 'unplugin-quansync/rspack'
export default {
/* ... */
plugins: [Quansync()],
}
import fs from 'node:fs'
import { quansyncMacro } from 'quansync'
// Create an quansync function by providing `sync` and `async` implementations
const readFile = quansyncMacro({
sync: (path: string) => fs.readFileSync(path),
async: (path: string) => fs.promises.readFile(path),
})
// Create an quansync function by providing a **async** function
const myFunction = quansyncMacro(async function (filename) {
// Use `await` to call another quansync function
const code = await readFile(filename, 'utf8')
return `// some custom prefix\n${code}`
})
// Use it as a sync function
const result = myFunction.sync('./some-file.js')
// Use it as an async function
const asyncResult = await myFunction.async('./some-file.js')
For more details, please refer to the
quansync documentation.