Skip to content

Commit

Permalink
feat(commonjs): Infer type for unidentified modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 12, 2021
1 parent 81f0eb7 commit f306907
Show file tree
Hide file tree
Showing 48 changed files with 74 additions and 45 deletions.
15 changes: 9 additions & 6 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export default function commonjs(options = {}) {
const defaultIsModuleExports =
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';

const { resolveRequireSourcesAndGetMeta, getWrappedIds } = getResolveRequireSourcesAndGetMeta(
extensions,
detectCycles
);
const {
resolveRequireSourcesAndGetMeta,
getWrappedIds,
isRequiredId
} = getResolveRequireSourcesAndGetMeta(extensions, detectCycles);
const dynamicRequireModuleSet = getDynamicRequireModuleSet(options.dynamicRequireTargets);
const isDynamicRequireModulesEnabled = dynamicRequireModuleSet.size > 0;
const commonDir = isDynamicRequireModulesEnabled
Expand Down Expand Up @@ -111,7 +112,8 @@ export default function commonjs(options = {}) {

if (
!dynamicRequireModuleSet.has(normalizePathSlashes(id)) &&
(!hasCjsKeywords(code, ignoreGlobal) || (isEsModule && !options.transformMixedEsModules))
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
(isEsModule && !options.transformMixedEsModules))
) {
return { meta: { commonjs: { isCommonJS: false } } };
}
Expand All @@ -136,7 +138,8 @@ export default function commonjs(options = {}) {
ast,
defaultIsModuleExports,
needsRequireWrapper,
resolveRequireSourcesAndGetMeta(this)
resolveRequireSourcesAndGetMeta(this),
isRequiredId(id)
);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/commonjs/src/resolve-require-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolveExtensions } from './resolve-id';

export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) {
const knownCjsModuleTypes = Object.create(null);
const requiredIds = Object.create(null);
const dependentModules = Object.create(null);
const getDependentModules = (id) =>
dependentModules[id] || (dependentModules[id] = Object.create(null));
Expand All @@ -12,6 +13,7 @@ export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) {
Object.keys(knownCjsModuleTypes).filter(
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
),
isRequiredId: (id) => requiredIds[id],
resolveRequireSourcesAndGetMeta: (rollupContext) => async (id, isParentCommonJS, sources) => {
knownCjsModuleTypes[id] = isParentCommonJS;
const requireTargets = await Promise.all(
Expand All @@ -34,6 +36,7 @@ export function getResolveRequireSourcesAndGetMeta(extensions, detectCycles) {
if (resolved.external) {
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
}
requiredIds[childId] = true;
const parentDependentModules = getDependentModules(id);
const childDependentModules = getDependentModules(childId);
childDependentModules[id] = true;
Expand Down
4 changes: 3 additions & 1 deletion packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export default async function transformCommonjs(
astCache,
defaultIsModuleExports,
needsRequireWrapper,
resolveRequireSourcesAndGetMeta
resolveRequireSourcesAndGetMeta,
isRequired
) {
const ast = astCache || tryParse(parse, code, id);
const magicString = new MagicString(code);
Expand Down Expand Up @@ -418,6 +419,7 @@ export default async function transformCommonjs(
if (
!(
shouldWrap ||
isRequired ||
uses.module ||
uses.exports ||
uses.require ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module.exports = {
description: 'strict require semantic modules can be entry points',
options: {
input: [
'fixtures/function/strict-require-semantic-entry/main.js',
'fixtures/function/strict-require-semantic-entry/other.js'
'fixtures/function/strict-requires-entry/main.js',
'fixtures/function/strict-requires-entry/other.js'
],
output: {
chunkFileNames: 'generated-[name].js'
}
},
pluginOptions: {
strictRequires: ['fixtures/function/strict-require-semantic-entry/main.js']
strictRequires: ['fixtures/function/strict-requires-entry/main.js']
},
exports(exports) {
assert.deepStrictEqual(exports, { foo: 'foo' });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
description: 'supports using function wrappers for modules for export mode "exports"',
pluginOptions: {
strictRequires: ['fixtures/function/strict-require-semantic-exportmode-exports/*E*.js']
strictRequires: ['fixtures/function/strict-requires-exportmode-exports/*E*.js']
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
description: 'supports using function wrappers for modules for export mode "module"',
pluginOptions: {
strictRequires: ['fixtures/function/strict-require-semantic-exportmode-module/*E*.js']
strictRequires: ['fixtures/function/strict-requires-exportmode-module/*E*.js']
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
description: 'supports using function wrappers for modules for export mode "replace"',
pluginOptions: {
strictRequires: ['fixtures/function/strict-require-semantic-exportmode-replace/*E*.js']
strictRequires: ['fixtures/function/strict-requires-exportmode-replace/*E*.js']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
description:
'identifies files without module features as commonjs if they are required by another file',
pluginOptions: {
strictRequires: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('FAIL');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line global-require
t.is(0 && require('./error.js'), 0);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
description: 'handles importing wrapped modules from ESM',
pluginOptions: {
strictRequires: ['fixtures/function/strict-require-semantic-from-esm/strict.js']
strictRequires: ['fixtures/function/strict-requires-from-esm/strict.js']
}
};
73 changes: 42 additions & 31 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -3588,9 +3588,7 @@ Generated by [AVA](https://avajs.dev).
␊
var require$$5 = 'bar';␊
␊
var none = /*#__PURE__*/Object.freeze({␊
__proto__: null␊
});␊
var none = {};␊
␊
const externalNamed = require$$0;␊
const externalMixed = require$$1;␊
Expand Down Expand Up @@ -3698,11 +3696,7 @@ Generated by [AVA](https://avajs.dev).
␊
var require$$5 = /*@__PURE__*/getAugmentedNamespace(_default$1);␊
␊
var none = /*#__PURE__*/Object.freeze({␊
__proto__: null␊
});␊
␊
var require$$6 = /*@__PURE__*/getAugmentedNamespace(none);␊
var none = {};␊
␊
const externalNamed = require$$0;␊
const externalMixed = require$$1;␊
Expand All @@ -3711,7 +3705,7 @@ Generated by [AVA](https://avajs.dev).
const namedExports = require$$3;␊
const mixedExports = require$$4;␊
const defaultExport = require$$5;␊
const noExports = require$$6;␊
const noExports = none;␊
␊
t.deepEqual(namedExports, { foo: 'foo' }, 'named exports');␊
t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports');␊
Expand Down Expand Up @@ -4004,9 +3998,7 @@ Generated by [AVA](https://avajs.dev).
'default': _default␊
});␊
␊
var none = /*#__PURE__*/Object.freeze({␊
__proto__: null␊
});␊
var none = {};␊
␊
const externalNamed = externalEsmNamed__namespace;␊
const externalMixed = externalEsmMixed__namespace;␊
Expand Down Expand Up @@ -4085,9 +4077,7 @@ Generated by [AVA](https://avajs.dev).
␊
var require$$5 = 'bar';␊
␊
var none = /*#__PURE__*/Object.freeze({␊
__proto__: null␊
});␊
var none = {};␊
␊
const externalNamed = require$$0;␊
const externalMixed = require$$1;␊
Expand Down Expand Up @@ -4235,11 +4225,7 @@ Generated by [AVA](https://avajs.dev).
␊
var require$$5 = /*@__PURE__*/getAugmentedNamespace(_default$1);␊
␊
var none = /*#__PURE__*/Object.freeze({␊
__proto__: null␊
});␊
␊
var require$$6 = /*@__PURE__*/getAugmentedNamespace(none);␊
var none = {};␊
␊
const externalNamed = require$$0;␊
const externalMixed = require$$1;␊
Expand All @@ -4248,7 +4234,7 @@ Generated by [AVA](https://avajs.dev).
const namedExports = require$$3;␊
const mixedExports = require$$4;␊
const defaultExport = require$$5;␊
const noExports = require$$6;␊
const noExports = none;␊
␊
t.deepEqual(namedExports, { foo: 'foo' }, 'named exports');␊
t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports');␊
Expand Down Expand Up @@ -5442,7 +5428,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-auto
## strict-requires-auto

> Snapshot 1
Expand Down Expand Up @@ -5491,7 +5477,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-circular
## strict-requires-circular

> Snapshot 1
Expand Down Expand Up @@ -5527,7 +5513,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-cycle-detection
## strict-requires-cycle-detection

> Snapshot 1
Expand Down Expand Up @@ -5576,7 +5562,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-debug
## strict-requires-debug

> Snapshot 1
Expand Down Expand Up @@ -5625,7 +5611,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-debug-none
## strict-requires-debug-none

> Snapshot 1
Expand All @@ -5638,7 +5624,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-entry
## strict-requires-entry

> Snapshot 1
Expand Down Expand Up @@ -5678,7 +5664,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-exportmode-exports
## strict-requires-exportmode-exports

> Snapshot 1
Expand Down Expand Up @@ -5754,7 +5740,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-exportmode-module
## strict-requires-exportmode-module

> Snapshot 1
Expand Down Expand Up @@ -5830,7 +5816,7 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-exportmode-replace
## strict-requires-exportmode-replace

> Snapshot 1
Expand Down Expand Up @@ -5862,7 +5848,32 @@ Generated by [AVA](https://avajs.dev).
`,
}

## strict-require-semantic-from-esm
## strict-requires-file-without-module-type

> Snapshot 1
{
'main.js': `'use strict';␊
␊
var main = {};␊
␊
var hasRequiredMain;␊
␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
hasRequiredMain = 1;␊
// eslint-disable-next-line global-require␊
t.is(0 , 0);␊
return main;␊
}␊
␊
var mainExports = requireMain();␊
␊
module.exports = mainExports;␊
`,
}

## strict-requires-from-esm

> Snapshot 1
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit f306907

Please sign in to comment.