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

getSymbolResolution: fallback to asset's symbol with non-static dependencies #7944

Merged
merged 2 commits into from Apr 13, 2022
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
31 changes: 24 additions & 7 deletions packages/core/core/src/BundleGraph.js
Expand Up @@ -1398,13 +1398,14 @@ export default class BundleGraph {
}

let found = false;
let nonStaticDependency = false;
let skipped = false;
let deps = this.getDependencies(asset).reverse();
let potentialResults = [];
for (let dep of deps) {
let depSymbols = dep.symbols;
if (!depSymbols) {
found = true;
nonStaticDependency = true;
continue;
}
// If this is a re-export, find the original module.
Expand Down Expand Up @@ -1523,15 +1524,31 @@ export default class BundleGraph {
// ..., but if it does exist, it has to be behind this one reexport.
return potentialResults[0];
} else {
// ... and there is no single reexport, but `bailout` tells us if it might still be exported.
let result = identifier;
if (skipped) {
// ... and it was excluded (by symbol propagation) or deferred.
result = false;
} else {
// ... and there is no single reexport, but it might still be exported:
if (found) {
// Fallback to namespace access, because of a bundle boundary.
result = null;
} else if (result === undefined) {
// If not exported explicitly by the asset (= would have to be in * or a reexport-all) ...
if (nonStaticDependency || asset.symbols?.has('*')) {
// ... and if there are non-statically analyzable dependencies or it's a CJS asset,
// fallback to namespace access.
result = null;
}
// (It shouldn't be possible for the symbol to be in a reexport-all and to end up here).
// Otherwise return undefined to report that the symbol wasn't found.
}
}

return {
asset,
exportSymbol: symbol,
symbol: skipped
? false
: found
? null
: identifier ?? (asset.symbols?.has('*') ? null : undefined),
symbol: result,
loc: asset.symbols?.get(symbol)?.loc,
};
}
Expand Down
@@ -0,0 +1,3 @@
import path from "path";

export const foo = __filename;
@@ -0,0 +1,8 @@
{
"name": "esm-filename-import",
"private": true,
"main": "dist/index.js",
"engines": {
"node": ">=10"
}
}
11 changes: 11 additions & 0 deletions packages/core/integration-tests/test/output-formats.js
Expand Up @@ -1206,6 +1206,17 @@ describe('output formats', function () {
assert.deepEqual({...ns}, {default: {default: 'default'}});
});

it('should support rewriting filename and importing path', async function () {
let input = path.join(
__dirname,
'/integration/formats/esm-filename-import/index.js',
);
let b = await bundle(input);

let ns = await run(b);
assert.deepEqual(ns.foo, input);
});

it('should rename shadowed imported specifiers to something unique', async function () {
let b = await bundle(
path.join(__dirname, '/integration/formats/esm-import-shadow/a.mjs'),
Expand Down