Skip to content

Commit

Permalink
fix(commonjs): pass on isEntry and custom resolve options
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 8, 2021
1 parent 00e16d9 commit 877a805
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 577 deletions.
2 changes: 1 addition & 1 deletion packages/commonjs/package.json
Expand Up @@ -63,7 +63,7 @@
"@rollup/plugin-node-resolve": "^8.4.0",
"locate-character": "^2.0.5",
"require-relative": "^0.8.7",
"rollup": "^2.39.0",
"rollup": "^2.58.0",
"shx": "^0.3.2",
"source-map": "^0.7.3",
"source-map-support": "^0.5.19",
Expand Down
16 changes: 11 additions & 5 deletions packages/commonjs/src/resolve-id.js
Expand Up @@ -49,7 +49,7 @@ export default function getResolveId(extensions) {
return undefined;
}

return function resolveId(importee, rawImporter) {
return function resolveId(importee, rawImporter, resolveOptions) {
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
return importee;
}
Expand Down Expand Up @@ -92,10 +92,16 @@ export default function getResolveId(extensions) {
return null;
}

return this.resolve(importee, importer, {
skipSelf: true,
custom: { 'node-resolve': { isRequire: isProxyModule || isRequiredModule } }
}).then((resolved) => {
return this.resolve(
importee,
importer,
Object.assign({}, resolveOptions, {
skipSelf: true,
custom: Object.assign({}, resolveOptions.custom, {
'node-resolve': { isRequire: isProxyModule || isRequiredModule }
})
})
).then((resolved) => {
if (!resolved) {
resolved = resolveExtensions(importee, importer);
}
Expand Down
37 changes: 37 additions & 0 deletions packages/commonjs/test/fixtures/function/custom-options/_config.js
@@ -0,0 +1,37 @@
const path = require('path');
const assert = require('assert');

const ID_MAIN = path.join(__dirname, 'main.js');

const getLastPathFragment = (pathString) => pathString && pathString.split(/[\\/]/).slice(-1)[0];

const resolveIdArgs = [];

module.exports = {
description: 'passes on isEntry and custom options when resolving via other plugins',
options: {
plugins: [
{
async buildStart() {
await this.resolve('./other.js', ID_MAIN, { isEntry: true, custom: { test: 42 } });
},
buildEnd() {
assert.deepStrictEqual(resolveIdArgs, [
['other.js', 'main.js', { custom: { test: 42 }, isEntry: true }],
[
'other.js',
'main.js',
// This is the important one
{ custom: { test: 42, 'node-resolve': { isRequire: false } }, isEntry: true }
],
['main.js', void 0, { custom: {}, isEntry: true }],
['main.js', void 0, { custom: { 'node-resolve': { isRequire: false } }, isEntry: true }]
]);
},
resolveId(source, importer, options) {
resolveIdArgs.push([getLastPathFragment(source), getLastPathFragment(importer), options]);
}
}
]
}
};
@@ -0,0 +1 @@
console.log('main');
@@ -0,0 +1 @@
console.log('other');

0 comments on commit 877a805

Please sign in to comment.