Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add back CJS<>CJS compatability from an earlier pr #2934

Merged
merged 1 commit into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions esinstall/src/index.ts
Expand Up @@ -334,10 +334,9 @@ ${colors.dim(
rollupPluginReplace(generateReplacements(env)),
rollupPluginCommonjs({
extensions: ['.js', '.cjs'],
esmExternals: (id) =>
Array.isArray(externalEsm)
? externalEsm.some((packageName) => isImportOfPackage(id, packageName))
: externalEsm,
esmExternals: Array.isArray(externalEsm)
? (id) => externalEsm.some((packageName) => isImportOfPackage(id, packageName))
: externalEsm,
requireReturnsDefault: 'auto',
} as RollupCommonJSOptions),
rollupPluginWrapInstallTargets(!!isTreeshake, installTargets, logger),
Expand Down
43 changes: 41 additions & 2 deletions snowpack/src/sources/local.ts
@@ -1,5 +1,10 @@
import crypto from 'crypto';
import {InstallOptions, InstallTarget, resolveEntrypoint} from 'esinstall';
import {
InstallOptions,
InstallTarget,
resolveEntrypoint,
resolveDependencyManifest as _resolveDependencyManifest,
} from 'esinstall';
import projectCacheDir from 'find-cache-dir';
import findUp from 'find-up';
import {existsSync, promises as fs} from 'fs';
Expand Down Expand Up @@ -51,6 +56,19 @@ const NEVER_PEER_PACKAGES: string[] = [

const memoizedResolve: Record<string, Record<string, string>> = {};

function isPackageCJS(manifest: any): boolean {
return (
// If a "module" entrypoint is defined, we'll use that.
!manifest.module &&
// If "type":"module", assume ESM.
manifest.type !== 'module' &&
// If export map exists, assume ESM exists somewhere within it.
!manifest.exports &&
// If "main" exists and ends in ".mjs", assume ESM.
!manifest.main?.endsWith('.mjs')
);
}

function getRootPackageDirectory(loc: string) {
const parts = loc.split('node_modules');
if (parts.length === 1) {
Expand Down Expand Up @@ -348,6 +366,17 @@ export default {
...Object.keys(packageManifest.peerDependencies || {}),
].filter((ext) => ext !== _packageName && !NEVER_PEER_PACKAGES.includes(ext));

function getMemoizedResolveDependencyManifest() {
const results = {};
return (packageName: string) => {
results[packageName] =
results[packageName] ||
_resolveDependencyManifest(packageName, rootPackageDirectory!);
return results[packageName];
};
}
const resolveDependencyManifest = getMemoizedResolveDependencyManifest();

const installOptions: InstallOptions = {
dest: installDest,
cwd: packageManifestLoc,
Expand All @@ -356,7 +385,17 @@ export default {
sourcemap: config.buildOptions.sourcemap,
alias: config.alias,
external: externalPackages,
externalEsm: true,
// ESM<>CJS Compatability: If we can detect that a dependency is common.js vs. ESM, then
// we can provide this hint to esinstall to improve our cross-package import support.
externalEsm: (imp) => {
const specParts = imp.split('/');
let _packageName: string = specParts.shift()!;
if (_packageName?.startsWith('@')) {
_packageName += '/' + specParts.shift();
}
const [, result] = resolveDependencyManifest(_packageName);
return !result || !isPackageCJS(result);
},
};
if (config.packageOptions.source === 'local') {
if (config.packageOptions.polyfillNode !== undefined) {
Expand Down