Skip to content

fix: handle frozen default exports#120

Merged
imranbarbhuiya merged 1 commit intoimranbarbhuiya:mainfrom
markdalgleish:handle-frozen-default-exports
Jun 26, 2023
Merged

fix: handle frozen default exports#120
imranbarbhuiya merged 1 commit intoimranbarbhuiya:mainfrom
markdalgleish:handle-frozen-default-exports

Conversation

@markdalgleish
Copy link
Contributor

@markdalgleish markdalgleish commented Jun 26, 2023

We had a bug report against Remix: remix-run/remix#6665

When polyfilling tty, I get the following error:

TypeError: Cannot assign to read only property 'ReadStream' of object '[object Object]'

        module.exports[k5] = polyfill[k5];
                       ^

After some digging, I discovered that this is because the JSPM polyfill for tty looks like this:

// ...
var m = /*#__PURE__*/Object.freeze({
  __proto__: null,
  isatty: isatty,
  ReadStream: ReadStream,
  WriteStream: WriteStream
});

export { ReadStream, WriteStream, m as default, isatty };

The default export is a read-only object since it's been passed to Object.freeze, and then the code injected by this plugin attempts to mutate it:

module.exports = polyfill.default
for (let k in polyfill) {
  module.exports[k] = polyfill[k]
}

I initially tried to fix the injected CommonJS code by using Object.assign({}, polyfill.default, polyfill), but unless I'm missing something, I found that esbuild is able to handle export * even when imported from a CommonJS file. Using ES modules to handle the re-export seems a lot cleaner since it avoids these sorts of issues entirely, otherwise we'd need to handle all sorts of edge cases when re-exporting.

Status and versioning classification:

  • Code changes have been tested and working fine, or there are no code changes

Copy link
Owner

@imranbarbhuiya imranbarbhuiya left a comment

Choose a reason for hiding this comment

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

Thanks for the pr ❤️, changes look good to me but I'll test the changes in an hour to make sure it works in all cases and will merge it.

@imranbarbhuiya imranbarbhuiya merged commit d7e6226 into imranbarbhuiya:main Jun 26, 2023
@markdalgleish markdalgleish deleted the handle-frozen-default-exports branch June 26, 2023 07:23
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.

2 participants