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 transformation of import/requires wrapped into Promise.resolve(). #8167

Merged
merged 2 commits into from Jun 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
@@ -0,0 +1 @@
module.exports = Promise.resolve(require('./async')).then(x => x + 1335);
23 changes: 23 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -4630,6 +4630,29 @@ describe('javascript', function () {
assert.equal(await run(b), 5);
});

it('should properly chain a dynamic import wrapped in a Promise.resolve()', async () => {
let b = await bundle(
path.join(__dirname, '/integration/require-async/resolve-chain.js'),
);

assertBundles(b, [
{
name: 'resolve-chain.js',
assets: [
'resolve-chain.js',
'bundle-url.js',
'cacheLoader.js',
'js-loader.js',
],
},
{
assets: ['async.js'],
},
]);

assert.equal(await run(b), 1337);
});

it('should detect parcel style async requires in commonjs', async () => {
let b = await bundle(
path.join(__dirname, '/integration/require-async/parcel.js'),
Expand Down
9 changes: 8 additions & 1 deletion packages/transformers/js/core/src/dependency_collector.rs
Expand Up @@ -458,10 +458,17 @@ impl<'a> Fold for DependencyCollector<'a> {
// Promise.resolve().then(() => require('foo'))
// Promise.resolve().then(() => { return require('foo') })
// Promise.resolve().then(function () { return require('foo') })
// but not
// Promise.resolve(require('foo'))
if let Call(call) = &*member.obj {
if let Callee::Expr(e) = &call.callee {
if let Member(m) = &**e {
if match_member_expr(m, vec!["Promise", "resolve"], self.decls) {
if match_member_expr(m, vec!["Promise", "resolve"], self.decls) &&
// Make sure the arglist is empty.
// I.e. do not proceed with the below unless Promise.resolve has an empty arglist
// because build_promise_chain() will not work in this case.
call.args.is_empty()
{
if let MemberProp::Ident(id) = &member.prop {
if id.sym.to_string().as_str() == "then" {
if let Some(arg) = node.args.get(0) {
Expand Down