Skip to content

Commit

Permalink
Correctly pass options in test
Browse files Browse the repository at this point in the history
Unfortunately, I did not find a way to detect this case from the plugin
and show a warning.
  • Loading branch information
lukastaegert committed Jan 18, 2024
1 parent 1808f4b commit 62bdd49
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
6 changes: 1 addition & 5 deletions packages/commonjs/src/resolve-id.js
Expand Up @@ -63,11 +63,7 @@ export default function getResolveId(extensions, isPossibleCjsId) {
// All logic below is specific to ES imports.
// Also, if we do not skip this logic for requires that are resolved while
// transforming a commonjs file, it can easily lead to deadlocks.
if (
customOptions &&
customOptions['node-resolve'] &&
customOptions['node-resolve'].isRequire
) {
if (customOptions?.['node-resolve']?.isRequire) {
return null;
}
const currentlyResolvingForParent = currentlyResolving.get(importer);
Expand Down
Expand Up @@ -6,7 +6,7 @@ module.exports = {
async resolveId(source, importer, options) {
if (source.endsWith('dep.js')) {
return {
...(await this.resolve(source, importer, { skipSelf: true, ...options })),
...(await this.resolve(source, importer, options)),
meta: { test: 'provided' }
};
}
Expand Down
@@ -1,27 +1,24 @@
const assert = require('assert');

module.exports = {
description: 'notifies the node-resolve plugin if an id is imported via "require" (strictRequires: "auto")',
description: 'notifies the node-resolve plugin if an id is imported via "require"',
options: {
plugins: [
{
name: 'node-resolve-mock',
resolveId(source, importer, { custom }) {
const { isRequire } = (custom && custom['node-resolve']) || {};
resolveId(source, importer, options) {
const { isRequire } = options.custom?.['node-resolve'] || {};
if (source === './foo') {
return this.resolve(isRequire ? './foo-required' : './foo-imported', importer);
return this.resolve(isRequire ? './foo-required' : './foo-imported', importer, options);
}
if (source === './bar') {
return this.resolve(isRequire ? './bar-required' : './bar-imported', importer);
return this.resolve(isRequire ? './bar-required' : './bar-imported', importer, options);
}
return null;
}
}
]
},
pluginOptions: {
strictRequires: 'auto'
},
async exports(exports) {
assert.deepStrictEqual(await exports, [{ default: 'imported' }, { default: 'imported' }]);
}
Expand Down
32 changes: 27 additions & 5 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -7198,18 +7198,40 @@ Generated by [AVA](https://avajs.dev).
␊
var foo = 'imported';␊
␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
var requiring = {};␊
␊
var fooRequired = 'required';␊
var fooRequired;␊
var hasRequiredFooRequired;␊
␊
requiring.foo = fooRequired;␊
function requireFooRequired () {␊
if (hasRequiredFooRequired) return fooRequired;␊
hasRequiredFooRequired = 1;␊
fooRequired = 'required';␊
return fooRequired;␊
}␊
␊
var hasRequiredRequiring;␊
␊
function requireRequiring () {␊
if (hasRequiredRequiring) return requiring;␊
hasRequiredRequiring = 1;␊
requiring.foo = requireFooRequired();␊
␊
requiring.barPromise = Promise.resolve().then(function () { return require('./bar-imported-QFe_5Jm2.js'); });␊
return requiring;␊
}␊
␊
requiring.barPromise = Promise.resolve().then(function () { return require('./bar-imported-QFe_5Jm2.js'); });␊
var requiringExports = requireRequiring();␊
var required = /*@__PURE__*/getDefaultExportFromCjs(requiringExports);␊
␊
t.is(foo, 'imported');␊
t.is(requiring.foo, 'required');␊
t.is(required.foo, 'required');␊
␊
var main = Promise.all([Promise.resolve().then(function () { return require('./bar-imported-QFe_5Jm2.js'); }), requiring.barPromise]);␊
var main = Promise.all([Promise.resolve().then(function () { return require('./bar-imported-QFe_5Jm2.js'); }), required.barPromise]);␊
␊
module.exports = main;␊
`,
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 62bdd49

Please sign in to comment.