diff --git a/esinstall/src/index.ts b/esinstall/src/index.ts index 8fce8130dc..3e8dde771d 100644 --- a/esinstall/src/index.ts +++ b/esinstall/src/index.ts @@ -31,6 +31,7 @@ import { createInstallTarget, findMatchingAliasEntry, getWebDependencyName, + isJavaScript, isPackageAliasEntry, MISSING_PLUGIN_SUGGESTIONS, parsePackageImportSpecifier, @@ -99,9 +100,8 @@ function resolveWebDependency( // For details on why we need to call fs.realpathSync.native here and other places, see // https://github.com/snowpackjs/snowpack/pull/999. const loc = fs.realpathSync.native(require.resolve(dep, {paths: [cwd]})); - const isJSFile = ['.js', '.mjs', '.cjs'].includes(path.extname(loc)); return { - type: isJSFile ? 'JS' : 'ASSET', + type: isJavaScript(loc) ? 'JS' : 'ASSET', loc, }; } @@ -139,7 +139,7 @@ function resolveWebDependency( try { const maybeLoc = fs.realpathSync.native(require.resolve(dep, {paths: [cwd]})); return { - type: 'JS', + type: isJavaScript(maybeLoc) ? 'JS' : 'ASSET', loc: maybeLoc, }; } catch (err) { @@ -192,11 +192,12 @@ function resolveWebDependency( if (typeof foundEntrypoint !== 'string') { throw new Error(`"${dep}" has unexpected entrypoint: ${JSON.stringify(foundEntrypoint)}.`); } + const loc = fs.realpathSync.native( + require.resolve(path.join(depManifestLoc || '', '..', foundEntrypoint)), + ); return { - type: 'JS', - loc: fs.realpathSync.native( - require.resolve(path.join(depManifestLoc || '', '..', foundEntrypoint)), - ), + type: isJavaScript(loc) ? 'JS' : 'ASSET', + loc, }; } diff --git a/esinstall/src/util.ts b/esinstall/src/util.ts index 6790d25823..2b7676bdb1 100644 --- a/esinstall/src/util.ts +++ b/esinstall/src/util.ts @@ -189,3 +189,9 @@ export function createInstallTarget(specifier: string, all = true): InstallTarge named: [], }; } + +/** Is this file JavaScript? */ +export function isJavaScript(pathname: string) { + const ext = path.extname(pathname).toLowerCase(); + return ext === '.js' || ext === '.mjs' || ext === '.cjs'; +}