Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/core/integration-tests/test/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,48 @@ describe('bundler', function () {
]);
});

it('should support inline constants in async bundles', async () => {
await fsFixture(overlayFS, __dirname)`
inline-constants-async
index.js:
import('./async').then(m => console.log(m.value));

async.js:
export const value = 'async value';

package.json:
{
"@parcel/transformer-js": {
"unstable_inlineConstants": true
}
}

yarn.lock:`;

let b = await bundle(
path.join(__dirname, 'inline-constants-async/index.js'),
{
mode: 'production',
defaultTargetOptions: {
shouldScopeHoist: true,
sourceMaps: false,
shouldOptimize: false,
},
inputFS: overlayFS,
},
);

// This will fail when the async bundle does not export it's constant
await run(b);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the asset still get inlined in this case? It wouldn't be ideal if you ended up with something like:

import('./async').then(m => console.log('async value'));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question, it doesn't seem to - I assume the static analysis can't track the usage to that level anyway?

From the bundle in the integration test:

(parcelRequire("8Vicm")).then((m)=>console.log(m.value));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added additional verification in the integration test.


// Asset should not be inlined
const index = b.getBundles().find(b => b.name.startsWith('index'));
const contents = overlayFS.readFileSync(index.filePath, 'utf8');
assert(
!contents.includes('async value'),
'async value should not be inlined',
);
});
describe('manual shared bundles', () => {
const dir = path.join(__dirname, 'manual-bundle');

Expand Down
8 changes: 7 additions & 1 deletion packages/packagers/js/src/ScopeHoistingPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,13 @@ export class ScopeHoistingPackager {
.getIncomingDependencies(asset)
.some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')
) {
if (!asset.meta.isConstantModule) {
// Don't wrap constant "entry" modules _except_ if they are referenced by any lazy dependency
if (
!asset.meta.isConstantModule ||
this.bundleGraph
.getIncomingDependencies(asset)
.some(dep => dep.priority === 'lazy')
) {
this.wrappedAssets.add(asset.id);
wrapped.push(asset);
}
Expand Down