Skip to content

Commit

Permalink
fix(commonjs): pass on isEntry and custom resolve options (#1018)
Browse files Browse the repository at this point in the history
Co-authored-by: shellscape <[email protected]>
  • Loading branch information
lukastaegert and shellscape authored Oct 19, 2021
1 parent dbfb996 commit d59088e
Show file tree
Hide file tree
Showing 11 changed files with 692 additions and 616 deletions.
2 changes: 1 addition & 1 deletion packages/commonjs/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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]);
}
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('main');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('other');
Loading

0 comments on commit d59088e

Please sign in to comment.