Skip to content

Commit

Permalink
perf: async default options
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed May 16, 2024
1 parent ab31ce7 commit d99e8ba
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import semver from 'semver'
* @typedef {PartialDeep<Options>} UserOptions
*/

async function getTSCompilerOptions() {
async function loadTSCompilerOptions() {
try {
const ts = await importModule('typescript')
const configPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists, 'tsconfig.json')
Expand All @@ -39,6 +39,16 @@ async function getTSCompilerOptions() {
}
}

/** @type {Promise<any> | undefined} */
let loadingTSCompilerOptions

function getTSCompilerOptions() {
if (!loadingTSCompilerOptions) {
loadingTSCompilerOptions = loadTSCompilerOptions()
}
return loadingTSCompilerOptions
}

/**
* @returns {Promise<Options['typescript']>}
*/
Expand Down Expand Up @@ -112,12 +122,25 @@ export async function resolveVue() {
* @returns {Promise<Options>}
*/
export async function resolveOptions(options = {}) {
const [
typescript,
babel,
react,
vue,
jsx,
] = await Promise.all([
options.typescript ?? resolveTypescript(),
options.babel ?? resolveBabel(),
options.react ?? resolveReact(),
options.vue ?? resolveVue(),
options.jsx ?? resolveJsx(),
])
return {
typescript: options.typescript ?? await resolveTypescript(),
babel: options.babel ?? await resolveBabel(),
react: options.react ?? resolveReact(),
vue: options.vue ?? await resolveVue(),
jsx: options.jsx ?? await resolveJsx(),
typescript,
babel,
react,
vue,
jsx,
configs: options.configs ?? [],
}
}

0 comments on commit d99e8ba

Please sign in to comment.