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: workaround for weirdly packaged cjs modules #1178

Merged
merged 2 commits into from
Jan 17, 2023

Conversation

yepitschunked
Copy link
Contributor

Motivation

We found a weird bug when shaker attempted to operate on preact/compat. Note that it's probably a bug that shaker is even processing this file, since it means we didn't strip the import in an earlier stage, but I'm not sure what's going on there.

I think the issue is demonstrated pretty clearly in the unit test, but the gist is that certain libraries might be packaged in a way where they have a default export, but no __esModule: true export. This will break babel's interopRequireDefault helper, which wraps its argument if it doesn't detect __esModule: true, resulting in an object like { default: { default: someExport } }.

In the case of preact/compat, this is fine, because all of the exports are also defined directly on module.exports. resulting in { default: { default: someExport, someExport } }. So even if interopRequireDefault produces a broken result, interopRequireDefault(...).default.someExport still exists.

Thee trouble comes when shaker deletes someExport because it was not in onlyExports, leaving only the default export; now, interopRequireDefault produces { default: { default: someExport } } and interopRequireDefault(...).default.someExport will be undefined.

Summary

I added detection of __esModule to collectImportAndExports. Now, if we detect that default is in onlyExports, and the module is not an esModule, we'll skip the shaker process as it will be unsafe.

Test plan

Added a unit test.

@changeset-bot
Copy link

changeset-bot bot commented Jan 11, 2023

🦋 Changeset detected

Latest commit: b443e53

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@linaria/shaker Patch
@linaria/testkit Patch
@linaria/utils Patch
@linaria/babel-preset Patch
linaria Patch
linaria-website Patch
vite-example Patch
@linaria/atomic Patch
@linaria/cli Patch
@linaria/core Patch
@linaria/esbuild Patch
@linaria/griffel Patch
@linaria/react Patch
@linaria/rollup Patch
@linaria/stylelint Patch
@linaria/tags Patch
@linaria/vite Patch
@linaria/webpack4-loader Patch
@linaria/webpack5-loader Patch
esbuild-example Patch
rollup-example Patch
vpssr-linaria-solid-example Patch
@linaria/webpack-loader Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Anber Anber merged commit 71a5b35 into callstack:master Jan 17, 2023
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.

None yet

2 participants