Skip to content

Commit 2628e7b

Browse files
authoredMay 28, 2024··
Ensure const modules in async bundles are wrapped (#9740)
* Add failing integration test * Wrap constant modules that might be imported async * Ensure async value isn't inlined
1 parent 3f32e0d commit 2628e7b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
 

‎packages/core/integration-tests/test/bundler.js

+42
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,48 @@ describe('bundler', function () {
12141214
]);
12151215
});
12161216

1217+
it('should support inline constants in async bundles', async () => {
1218+
await fsFixture(overlayFS, __dirname)`
1219+
inline-constants-async
1220+
index.js:
1221+
import('./async').then(m => console.log(m.value));
1222+
1223+
async.js:
1224+
export const value = 'async value';
1225+
1226+
package.json:
1227+
{
1228+
"@parcel/transformer-js": {
1229+
"unstable_inlineConstants": true
1230+
}
1231+
}
1232+
1233+
yarn.lock:`;
1234+
1235+
let b = await bundle(
1236+
path.join(__dirname, 'inline-constants-async/index.js'),
1237+
{
1238+
mode: 'production',
1239+
defaultTargetOptions: {
1240+
shouldScopeHoist: true,
1241+
sourceMaps: false,
1242+
shouldOptimize: false,
1243+
},
1244+
inputFS: overlayFS,
1245+
},
1246+
);
1247+
1248+
// This will fail when the async bundle does not export it's constant
1249+
await run(b);
1250+
1251+
// Asset should not be inlined
1252+
const index = b.getBundles().find(b => b.name.startsWith('index'));
1253+
const contents = overlayFS.readFileSync(index.filePath, 'utf8');
1254+
assert(
1255+
!contents.includes('async value'),
1256+
'async value should not be inlined',
1257+
);
1258+
});
12171259
describe('manual shared bundles', () => {
12181260
const dir = path.join(__dirname, 'manual-bundle');
12191261

‎packages/packagers/js/src/ScopeHoistingPackager.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,13 @@ export class ScopeHoistingPackager {
317317
.getIncomingDependencies(asset)
318318
.some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')
319319
) {
320-
if (!asset.meta.isConstantModule) {
320+
// Don't wrap constant "entry" modules _except_ if they are referenced by any lazy dependency
321+
if (
322+
!asset.meta.isConstantModule ||
323+
this.bundleGraph
324+
.getIncomingDependencies(asset)
325+
.some(dep => dep.priority === 'lazy')
326+
) {
321327
this.wrappedAssets.add(asset.id);
322328
wrapped.push(asset);
323329
}

0 commit comments

Comments
 (0)
Please sign in to comment.