Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
681cd8c
fix(commonjs): attach correct plugin meta-data to commonjs modules (#…
lukastaegert Sep 21, 2021
f55ba96
feat(commonjs): reimplement dynamic import handling (requires Node 12…
lukastaegert Oct 28, 2021
003845f
feat(commonjs): add strictRequires option to wrap modules (#1038)
lukastaegert Nov 6, 2021
771ef4a
feat(commonjs): automatically wrap cyclic modules (#1038)
lukastaegert Nov 9, 2021
aae44a2
feat(commonjs): Infer type for unidentified modules (#1038)
lukastaegert Nov 12, 2021
1054074
feat(commonjs): make namespace callable when requiring ESM with funct…
lukastaegert Nov 12, 2021
4424686
feat(commonjs): limit ignoreTryCatch to external requires (#1038)
lukastaegert Nov 19, 2021
a9ed479
feat(commonjs): auto-detect conditional requires (#1038)
lukastaegert Nov 20, 2021
42e7196
feat(commonjs): add dynamicRequireRoot option (#1038)
lukastaegert Nov 21, 2021
06a74ae
feat(commonjs): throw for dynamic requires from outside the configure…
lukastaegert Nov 22, 2021
3cdad21
refactor(commonjs): deconflict helpers only once globals are known (#…
lukastaegert Nov 22, 2021
3b28947
feat(commonjs): expose plugin version (#1038)
lukastaegert Nov 24, 2021
a66d253
fix(commonjs): do not transform "typeof exports" for mixed modules (#…
lukastaegert Dec 2, 2021
29074f4
fix(commonjs): inject module name into dynamic require function (#1038)
lukastaegert Dec 14, 2021
804eb8e
fix(commonjs): validate node-resolve peer version (#1038)
lukastaegert Dec 14, 2021
d01238f
fix(commonjs): use correct version and add package exports (#1038)
lukastaegert Dec 14, 2021
017ea90
fix(commonjs): proxy all entries to not break legacy polyfill plugins…
lukastaegert Jan 14, 2022
a521f47
fix(commonjs): add heuristic to deoptimize requires after calling imp…
lukastaegert Feb 7, 2022
368f9c8
fix(commonjs): handle external dependencies when using the cache (#1038)
lukastaegert Feb 24, 2022
e5f1abd
fix(commonjs): Do not change semantics when removing requires in if s…
lukastaegert Apr 3, 2022
e2f26a4
fix(commonjs): Warn when plugins do not pass options to resolveId (#1…
lukastaegert Apr 18, 2022
5b3bd05
fix(commonjs): support CJS modules re-exporting transpiled ESM module…
fwouts Apr 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(commonjs): proxy all entries to not break legacy polyfill plugins (
  • Loading branch information
lukastaegert committed Apr 24, 2022
commit 017ea9009507fef19c74ceddc83cbb13507b565a
2 changes: 1 addition & 1 deletion packages/commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"require"
],
"peerDependencies": {
"rollup": "^2.61.1"
"rollup": "^2.67.0"
},
"dependencies": {
"@rollup/pluginutils": "^3.1.0",
Expand Down
13 changes: 6 additions & 7 deletions packages/commonjs/src/generate-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ export function getRequireHandlers() {
exportsName,
id,
exportMode,
resolveRequireSourcesAndGetMeta,
resolveRequireSourcesAndUpdateMeta,
needsRequireWrapper,
isEsModule,
isDynamicRequireModulesEnabled,
getIgnoreTryCatchRequireStatementMode
getIgnoreTryCatchRequireStatementMode,
commonjsMeta
) {
const imports = [];
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
Expand All @@ -117,9 +118,10 @@ export function getRequireHandlers() {
);
}
const requiresBySource = collectSources(requireExpressions);
const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
const requireTargets = await resolveRequireSourcesAndUpdateMeta(
id,
needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
commonjsMeta,
Object.keys(requiresBySource).map((source) => {
return {
source,
Expand All @@ -134,10 +136,7 @@ export function getRequireHandlers() {
getIgnoreTryCatchRequireStatementMode,
magicString
);
return {
importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
usesRequireWrapper
};
return imports.length ? `${imports.join('\n')}\n\n` : '';
}

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const WRAPPED_SUFFIX = '?commonjs-wrapped';
export const EXTERNAL_SUFFIX = '?commonjs-external';
export const EXPORTS_SUFFIX = '?commonjs-exports';
export const MODULE_SUFFIX = '?commonjs-module';
export const ES_IMPORT_SUFFIX = '?es-import';
export const ENTRY_SUFFIX = '?commonjs-entry';
export const ES_IMPORT_SUFFIX = '?commonjs-es-import';

export const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
export const HELPERS_ID = '\0commonjsHelpers.js';
Expand Down
69 changes: 42 additions & 27 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getDynamicModuleRegistry, getDynamicRequireModules } from './dynamic-mo

import {
DYNAMIC_MODULES_ID,
ENTRY_SUFFIX,
ES_IMPORT_SUFFIX,
EXPORTS_SUFFIX,
EXTERNAL_SUFFIX,
Expand All @@ -20,13 +21,20 @@ import {
unwrapId
} from './helpers';
import { hasCjsKeywords } from './parse';
import { getEsImportProxy, getStaticRequireProxy, getUnknownRequireProxy } from './proxies';
import {
getEntryProxy,
getEsImportProxy,
getStaticRequireProxy,
getUnknownRequireProxy
} from './proxies';
import getResolveId from './resolve-id';
import { getResolveRequireSourcesAndGetMeta } from './resolve-require-sources';
import { getRequireResolver } from './resolve-require-sources';
import validateVersion from './rollup-version';
import transformCommonjs from './transform-commonjs';
import { getName, getStrictRequiresFilter, normalizePathSlashes } from './utils';

const PLUGIN_NAME = 'commonjs';

export default function commonjs(options = {}) {
const {
ignoreGlobal,
Expand Down Expand Up @@ -58,11 +66,6 @@ export default function commonjs(options = {}) {
: () =>
typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';

const {
resolveRequireSourcesAndGetMeta,
getWrappedIds,
isRequiredId
} = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
const dynamicRequireRoot =
typeof options.dynamicRequireRoot === 'string'
? resolve(options.dynamicRequireRoot)
Expand All @@ -73,9 +76,6 @@ export default function commonjs(options = {}) {
);
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;

const esModulesWithDefaultExport = new Set();
const esModulesWithNamedExports = new Set();

const ignoreRequire =
typeof options.ignore === 'function'
? options.ignore
Expand Down Expand Up @@ -103,25 +103,31 @@ export default function commonjs(options = {}) {

const sourceMap = options.sourceMap !== false;

// Initialized in buildStart
let requireResolver;

function transformAndCheckExports(code, id) {
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
this.parse,
code,
id
);

const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
if (hasDefaultExport) {
esModulesWithDefaultExport.add(id);
commonjsMeta.hasDefaultExport = true;
}
if (hasNamedExports) {
esModulesWithNamedExports.add(id);
commonjsMeta.hasNamedExports = true;
}

if (
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
(!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
(isEsModule && !options.transformMixedEsModules))
) {
return { meta: { commonjs: { isCommonJS: false } } };
commonjsMeta.isCommonJS = false;
return { meta: { commonjs: commonjsMeta } };
}

const needsRequireWrapper =
Expand Down Expand Up @@ -160,22 +166,23 @@ export default function commonjs(options = {}) {
ast,
getDefaultIsModuleExports(id),
needsRequireWrapper,
resolveRequireSourcesAndGetMeta(this),
isRequiredId(id),
checkDynamicRequire
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
requireResolver.isRequiredId(id),
checkDynamicRequire,
commonjsMeta
);
}

return {
name: 'commonjs',
name: PLUGIN_NAME,

version,

options(rawOptions) {
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
// do not prevent our plugin from resolving entry points ot proxies.
const plugins = Array.isArray(rawOptions.plugins)
? rawOptions.plugins
? [...rawOptions.plugins]
: rawOptions.plugins
? [rawOptions.plugins]
: [];
Expand All @@ -197,11 +204,12 @@ export default function commonjs(options = {}) {
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
);
}
requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
},

buildEnd() {
if (options.strictRequires === 'debug') {
const wrappedIds = getWrappedIds();
const wrappedIds = requireResolver.getWrappedIds();
if (wrappedIds.length) {
this.warn({
code: 'WRAPPED_IDS',
Expand Down Expand Up @@ -250,6 +258,15 @@ export default function commonjs(options = {}) {
);
}

// entry suffix is just appended to not mess up relative external resolution
if (id.endsWith(ENTRY_SUFFIX)) {
return getEntryProxy(
id.slice(0, -ENTRY_SUFFIX.length),
defaultIsModuleExports,
this.getModuleInfo
);
}

if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
}
Expand All @@ -265,18 +282,16 @@ export default function commonjs(options = {}) {

if (isWrappedId(id, PROXY_SUFFIX)) {
const actualId = unwrapId(id, PROXY_SUFFIX);
return getStaticRequireProxy(
actualId,
getRequireReturnsDefault(actualId),
esModulesWithDefaultExport,
esModulesWithNamedExports,
this.load
);
return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
}

return null;
},

shouldTransformCachedModule(...args) {
return requireResolver.shouldTransformCachedModule.call(this, ...args);
},

transform(code, id) {
const extName = extname(id);
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
Expand Down
36 changes: 23 additions & 13 deletions packages/commonjs/src/proxies.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HELPERS_ID } from './helpers';
import { HELPERS_ID, IS_WRAPPED_COMMONJS } from './helpers';
import { capitalize, getName } from './utils';

export function getUnknownRequireProxy(id, requireReturnsDefault) {
Expand All @@ -17,36 +17,46 @@ export function getUnknownRequireProxy(id, requireReturnsDefault) {
return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
}

export async function getStaticRequireProxy(
id,
requireReturnsDefault,
esModulesWithDefaultExport,
esModulesWithNamedExports,
loadModule
) {
export async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
const name = getName(id);
const {
meta: { commonjs: commonjsMeta }
} = await loadModule({ id });
if (commonjsMeta && commonjsMeta.isCommonJS) {
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
} else if (!commonjsMeta) {
if (!commonjsMeta) {
return getUnknownRequireProxy(id, requireReturnsDefault);
} else if (commonjsMeta.isCommonJS) {
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
} else if (!requireReturnsDefault) {
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
id
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
} else if (
requireReturnsDefault !== true &&
(requireReturnsDefault === 'namespace' ||
!esModulesWithDefaultExport.has(id) ||
(requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id)))
!commonjsMeta.hasDefaultExport ||
(requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
) {
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
}
return `export { default } from ${JSON.stringify(id)};`;
}

export function getEntryProxy(id, defaultIsModuleExports, getModuleInfo) {
const {
meta: { commonjs: commonjsMeta },
hasDefaultExport
} = getModuleInfo(id);
if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
const stringifiedId = JSON.stringify(id);
let code = `export * from ${stringifiedId};`;
if (hasDefaultExport) {
code += `export { default } from ${stringifiedId};`;
}
return code;
}
return getEsImportProxy(id, defaultIsModuleExports);
}

export function getEsImportProxy(id, defaultIsModuleExports) {
const name = getName(id);
const exportsName = `${name}Exports`;
Expand Down
35 changes: 20 additions & 15 deletions packages/commonjs/src/resolve-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dirname, resolve, sep } from 'path';

import {
DYNAMIC_MODULES_ID,
ENTRY_SUFFIX,
ES_IMPORT_SUFFIX,
EXPORTS_SUFFIX,
EXTERNAL_SUFFIX,
Expand Down Expand Up @@ -51,18 +52,16 @@ export function resolveExtensions(importee, importer, extensions) {
export default function getResolveId(extensions) {
return async function resolveId(importee, importer, resolveOptions) {
// We assume that all requires are pre-resolved
if (
resolveOptions.custom &&
resolveOptions.custom['node-resolve'] &&
resolveOptions.custom['node-resolve'].isRequire
) {
const customOptions = resolveOptions.custom;
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
return null;
}
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
return unwrapId(importee, WRAPPED_SUFFIX);
}

if (
importee.endsWith(ENTRY_SUFFIX) ||
isWrappedId(importee, MODULE_SUFFIX) ||
isWrappedId(importee, EXPORTS_SUFFIX) ||
isWrappedId(importee, PROXY_SUFFIX) ||
Expand All @@ -79,7 +78,8 @@ export default function getResolveId(extensions) {
importer === DYNAMIC_MODULES_ID ||
// Proxies are only importing resolved ids, no need to resolve again
isWrappedId(importer, PROXY_SUFFIX) ||
isWrappedId(importer, ES_IMPORT_SUFFIX)
isWrappedId(importer, ES_IMPORT_SUFFIX) ||
importer.endsWith(ENTRY_SUFFIX)
) {
return importee;
}
Expand All @@ -99,22 +99,27 @@ export default function getResolveId(extensions) {

// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
// if that is the case, we need to add a proxy.
const customOptions = resolveOptions.custom;

// If this is a require, we do not need a proxy
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
return null;
}

const resolved =
(await this.resolve(importee, importer, Object.assign({ skipSelf: true }, resolveOptions))) ||
resolveExtensions(importee, importer, extensions);
if (!resolved || resolved.external) {
// Make sure that even if other plugins resolve again, we ignore our own proxies
if (
!resolved ||
resolved.external ||
resolved.id.endsWith(ENTRY_SUFFIX) ||
isWrappedId(resolved.id, ES_IMPORT_SUFFIX)
) {
return resolved;
}
const moduleInfo = await this.load(resolved);
if (resolveOptions.isEntry) {
moduleInfo.moduleSideEffects = true;
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
return resolved.id + ENTRY_SUFFIX;
}
const {
meta: { commonjs: commonjsMeta }
} = await this.load(resolved);
} = moduleInfo;
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
}
Expand Down
Loading