Skip to content

Commit

Permalink
add back cjs-cjs compat from an earlier pr (#2934)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Mar 20, 2021
1 parent 9d1b822 commit 1232e25
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
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

1 comment on commit 1232e25

@vercel
Copy link

@vercel vercel bot commented on 1232e25 Mar 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.