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

Export build time not found packages on runtime #518

Merged
merged 2 commits into from
Mar 23, 2020

Conversation

legendecas
Copy link
Contributor

Exporting eval results.

@@ -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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants