Skip to content

Commit

Permalink
getSymbolResolution: fallback to asset's symbol with non-static depen…
Browse files Browse the repository at this point in the history
…dencies (#7944)
  • Loading branch information
mischnic committed Apr 13, 2022
1 parent 8daaed8 commit 0e2ebd5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
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"
}
}
Empty file.
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

0 comments on commit 0e2ebd5

Please sign in to comment.