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(node-resolve): forward meta-information from other plugins #1062

Merged
merged 1 commit into from Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/node-resolve/src/index.js
Expand Up @@ -6,10 +6,10 @@ import deepMerge from 'deepmerge';
import isModule from 'is-module';

import { isDirCached, isFileCached, readCachedFile } from './cache';
import handleDeprecatedOptions from './deprecated-options';
import { fileExists, readFile, realpath } from './fs';
import resolveImportSpecifiers from './resolveImportSpecifiers';
import { getMainFields, getPackageName, normalizeInput } from './util';
import handleDeprecatedOptions from './deprecated-options';

const builtins = new Set(builtinList);
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
Expand Down Expand Up @@ -223,11 +223,10 @@ export function nodeResolve(opts = {}) {
}
return null;
}
const result = {
return {
id: `${location}${importSuffix}`,
moduleSideEffects: hasModuleSideEffects(location)
};
return result;
};

return {
Expand Down Expand Up @@ -267,9 +266,13 @@ export function nodeResolve(opts = {}) {
importer,
Object.assign({ skipSelf: true }, resolveOptions)
);
const isExternal = !!(resolvedResolved && resolvedResolved.external);
if (isExternal) {
return false;
if (resolvedResolved) {
// Handle plugins that manually make the result external
if (resolvedResolved.external) {
return false;
}
// Pass on meta information added by other plugins
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should really just target property holding the metadata instead of allowing everything through by default except moduleSideEffects? Don't think we'd be doing any object merging then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly, the last remaining property is syntheticNamedExports. Which we may want to keep IF we also keep the id. So maybe a better solution is: Keep everything from the second resolve, but override moduleSideEffects if the id did not change?

return { ...resolved, meta: resolvedResolved.meta };
}
}
return resolved;
Expand Down
32 changes: 28 additions & 4 deletions packages/node-resolve/test/test.js
Expand Up @@ -487,13 +487,13 @@ test('passes on "isEntry" flag', async (t) => {
]
});
t.deepEqual(resolveOptions, [
['other.js', 'main.js', { custom: void 0, isEntry: true }],
['main.js', void 0, { custom: void 0, isEntry: true }],
['dep.js', 'main.js', { custom: void 0, isEntry: false }]
['other.js', 'main.js', { custom: {}, isEntry: true }],
['main.js', void 0, { custom: {}, isEntry: true }],
['dep.js', 'main.js', { custom: {}, isEntry: false }]
]);
});

test.only('passes on custom options', async (t) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

test('passes on custom options', async (t) => {
const resolveOptions = [];
await rollup({
input: 'entry/other.js',
Expand All @@ -520,3 +520,27 @@ test.only('passes on custom options', async (t) => {
['other.js', void 0, { custom: {}, isEntry: true }]
]);
});

test('passes on meta information from other plugins', async (t) => {
await rollup({
input: 'entry/other.js',
onwarn: failOnWarn(t),
plugins: [
nodeResolve(),
{
name: 'test-meta',
resolveId(importee) {
return {
id: resolve(importee),
meta: { test: { 'I am': 'here' } }
};
},

load(id) {
const info = this.getModuleInfo(id);
t.deepEqual(info.meta, { test: { 'I am': 'here' } });
}
}
]
});
});