Skip to content

Conversation

@legendecas
Copy link
Contributor

Exporting eval results.

@legendecas legendecas requested review from Timer and styfle as code owners March 16, 2020 06:50
@@ -1 +1 @@
__non_webpack_require__('UNKNOWN');
module.exports = __non_webpack_require__('UNKNOWN');
Copy link
Member

Choose a reason for hiding this comment

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

What's the purpose of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are cases that the packages are missing at ncc build time, yet presented at global scope or runtime. Without this fix, the eval("require")('dep') can succeed at runtime but the result is not exported.

Copy link
Member

Choose a reason for hiding this comment

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

There are cases that the packages are missing at ncc build time, yet presented at global scope or runtime.

Which cases? Can you add a test that fails but succeeds with this PR?

Copy link
Contributor Author

@legendecas legendecas Mar 18, 2020

Choose a reason for hiding this comment

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

Let's say we have a project referencing an optional dependency using

try {
  foo = require('foo');
} catch {}

Package foo is not installed on build time since it's totally optional and should not be bundled into the products. So package foo will be transformed by loader runtime-notfound to eval("require")("foo") without assigning return value to module.exports. Since then the require('foo') can succeed without any error at runtime but the return value is undefined, not the dynamically required package module.

The test case test/unit/runtime-notfound/output.js is already presented in the repo, and is identical to the case above, which has been updated in the PR to reflect the correct result.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I should have been more clear. Can you add an integration test that fails with the latest ncc but succeeds with this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Integration test added, PTAL :)

Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Looks good to me, it seems like this should have been exported from the beginning.

Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

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

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants