Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): pass on isEntry and custom resolve options #1018

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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
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