From 3acca423bcabb6598bf1e1f0cad05eca4e3aeb7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Olivera?= Date: Thu, 3 Jan 2019 14:47:28 -0300 Subject: [PATCH] Refactor the jsOrTs function * Calc the extension and save it in a const to make the code simpler --- src/index.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 4896394..e7ecf9d 100644 --- a/src/index.js +++ b/src/index.js @@ -69,18 +69,6 @@ export default async function microbundle(inputOptions) { options.sourcemap = true; } - const jsOrTs = async filename => - resolve( - cwd, - `${filename}${ - (await isFile(resolve(cwd, filename + '.ts'))) - ? '.ts' - : (await isFile(resolve(cwd, filename + '.tsx'))) - ? '.tsx' - : '.js' - }`, - ); - options.input = []; [] .concat( @@ -88,8 +76,8 @@ export default async function microbundle(inputOptions) { ? options.entries : (options.pkg.source && resolve(cwd, options.pkg.source)) || ((await isDir(resolve(cwd, 'src'))) && - (await jsOrTs('src/index'))) || - (await jsOrTs('index')) || + (await jsOrTs(cwd, 'src/index'))) || + (await jsOrTs(cwd, 'index')) || options.pkg.module, ) .map(file => glob(file)) @@ -247,6 +235,16 @@ function getName({ name, pkgName, amdName, cwd, hasPackageJson }) { return name || amdName || safeVariableName(pkgName); } +async function jsOrTs(cwd, filename) { + const extension = (await isFile(resolve(cwd, filename + '.ts'))) + ? '.ts' + : (await isFile(resolve(cwd, filename + '.tsx'))) + ? '.tsx' + : '.js'; + + return resolve(cwd, `${filename}${extension}`); +} + function createConfig(options, entry, format, writeMeta) { let { pkg } = options;