Skip to content

Commit

Permalink
Allow resolution of inner package files
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Nov 24, 2020
1 parent b10b97b commit 388acff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 8 additions & 7 deletions esinstall/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
createInstallTarget,
findMatchingAliasEntry,
getWebDependencyName,
isJavaScript,
isPackageAliasEntry,
MISSING_PLUGIN_SUGGESTIONS,
parsePackageImportSpecifier,
Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
};
}

Expand Down
6 changes: 6 additions & 0 deletions esinstall/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

0 comments on commit 388acff

Please sign in to comment.