-
-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commonjs): automatically wrap cyclic modules (#1038)
- Loading branch information
1 parent
e8dd3c3
commit 2b3d215
Showing
49 changed files
with
541 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,80 @@ | ||
import { EXTERNAL_SUFFIX, IS_WRAPPED_COMMONJS, PROXY_SUFFIX, wrapId } from './helpers'; | ||
import { resolveExtensions } from './resolve-id'; | ||
|
||
// TODO Lukas auto-detect circular dependencies | ||
// * only return once all dependencies have been analyzed | ||
// * wait for this.load dependencies unless they already have an entry in knownCjsModuleTypes to avoid deadlocks | ||
// as those have already started being processed | ||
// * only analyze cycles if we do not have an explicit config | ||
export function getResolveRequireSourcesAndGetMeta(extensions) { | ||
export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) { | ||
const knownCjsModuleTypes = Object.create(null); | ||
return (rollupContext) => (id, isParentCommonJS, sources) => { | ||
knownCjsModuleTypes[id] = isParentCommonJS; | ||
return Promise.all( | ||
sources.map(async (source) => { | ||
// Never analyze or proxy internal modules | ||
if (source.startsWith('\0')) { | ||
return { source, id: source, isCommonJS: false }; | ||
} | ||
const resolved = | ||
(await rollupContext.resolve(source, id, { | ||
skipSelf: true, | ||
custom: { | ||
'node-resolve': { isRequire: true } | ||
const dependentModules = Object.create(null); | ||
const getDependentModules = (id) => | ||
dependentModules[id] || (dependentModules[id] = Object.create(null)); | ||
|
||
return { | ||
getWrappedIds: () => | ||
Object.keys(knownCjsModuleTypes).filter( | ||
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS | ||
), | ||
resolveRequireSourcesAndGetMeta: (rollupContext) => async (id, isParentCommonJS, sources) => { | ||
knownCjsModuleTypes[id] = isParentCommonJS; | ||
const requireTargets = await Promise.all( | ||
sources.map(async (source) => { | ||
// Never analyze or proxy internal modules | ||
if (source.startsWith('\0')) { | ||
return { id: source, allowProxy: false }; | ||
} | ||
const resolved = | ||
(await rollupContext.resolve(source, id, { | ||
skipSelf: true, | ||
custom: { | ||
'node-resolve': { isRequire: true } | ||
} | ||
})) || resolveExtensions(source, id, extensions); | ||
if (!resolved) { | ||
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false }; | ||
} | ||
const childId = resolved.id; | ||
if (resolved.external) { | ||
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false }; | ||
} | ||
const parentDependentModules = getDependentModules(id); | ||
const childDependentModules = getDependentModules(childId); | ||
childDependentModules[id] = true; | ||
for (const dependentId of Object.keys(parentDependentModules)) { | ||
childDependentModules[dependentId] = true; | ||
} | ||
if (parentDependentModules[childId]) { | ||
// If we depend on one of our dependencies, we have a cycle. Then all modules that | ||
// we depend on that also depend on the same module are part of a cycle as well. | ||
if (detectCycles && isParentCommonJS) { | ||
knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS; | ||
knownCjsModuleTypes[childId] = IS_WRAPPED_COMMONJS; | ||
for (const dependentId of Object.keys(parentDependentModules)) { | ||
if (getDependentModules(dependentId)[childId]) { | ||
knownCjsModuleTypes[dependentId] = IS_WRAPPED_COMMONJS; | ||
} | ||
} | ||
} | ||
})) || resolveExtensions(source, id, extensions); | ||
if (!resolved) { | ||
return { source, id: wrapId(source, EXTERNAL_SUFFIX), isCommonJS: false }; | ||
} | ||
if (resolved.external) { | ||
return { source, id: wrapId(resolved.id, EXTERNAL_SUFFIX), isCommonJS: false }; | ||
} | ||
if (resolved.id in knownCjsModuleTypes) { | ||
} else { | ||
// This makes sure the current transform handler waits for all direct dependencies to be | ||
// loaded and transformed and therefore for all transitive CommonJS dependencies to be | ||
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable. | ||
await rollupContext.load(resolved); | ||
} | ||
return { id: childId, allowProxy: true }; | ||
}) | ||
); | ||
return { | ||
requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => { | ||
const isCommonJS = knownCjsModuleTypes[dependencyId]; | ||
return { | ||
source, | ||
source: sources[index], | ||
id: | ||
knownCjsModuleTypes[resolved.id] === IS_WRAPPED_COMMONJS | ||
? resolved.id | ||
: wrapId(resolved.id, PROXY_SUFFIX), | ||
isCommonJS: knownCjsModuleTypes[resolved.id] | ||
allowProxy && isCommonJS !== IS_WRAPPED_COMMONJS | ||
? wrapId(dependencyId, PROXY_SUFFIX) | ||
: dependencyId, | ||
isCommonJS | ||
}; | ||
} | ||
const { | ||
meta: { commonjs: commonjsMeta } | ||
} = await rollupContext.load(resolved); | ||
const isCommonJS = commonjsMeta && commonjsMeta.isCommonJS; | ||
return { | ||
source, | ||
id: isCommonJS === IS_WRAPPED_COMMONJS ? resolved.id : wrapId(resolved.id, PROXY_SUFFIX), | ||
isCommonJS | ||
}; | ||
}) | ||
); | ||
}), | ||
usesRequireWrapper: knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS | ||
}; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.