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: handle namespace import and dynamic import interop consistently #15619

Conversation

XiSenao
Copy link
Collaborator

@XiSenao XiSenao commented Jan 16, 2024

Description

fix #15542
fix #15018

Additional context

From the following repository examples:
https://github.com/XiSenao/exceptional-performance-of-loading-commonjs-modules/blob/main/src/main.js

it can be found that using namespace imports to load CommonJS modules results in inconsistent development and build behaviors. In most cases, dynamic import behaves as expected. That is to say, it seems effective to rewrite in the following way.

import('${rewrittenUrl}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))

However, when exploring how the @rollup/plugin-commonjs plugin interacts with rollup, it seems that rollup will merge the transpiled ESM product through

mergeNamespaces({
  __proto__: null,
  default: getDefaultExportFromCjs(rectClamp)
}, [rectClamp])

The logic of the rollup processing and the rewritten dynamic import are somewhat similar, but it can be found that there are still boundary scenarios that will cause inconsistencies between development and build behaviors for dynamic imports(e.g. only export Array or String).


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link

stackblitz bot commented Jan 16, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

patak-dev
patak-dev previously approved these changes Jan 16, 2024
@XiSenao XiSenao changed the title fix: the abnormal behavior of namespace import and dynamic import when loading commonjs modules fix: maintain the rationality of interoperation between namespace import and dynamic import. Jan 18, 2024
@bluwy bluwy changed the title fix: maintain the rationality of interoperation between namespace import and dynamic import. fix: handle namespace import and dynamic import interop consistently Jan 18, 2024
@bluwy bluwy added the p3-minor-bug An edge case that only affects very specific usage (priority) label Jan 18, 2024
@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 659e82e: Open

suite result latest scheduled
analogjs success success
astro success success
histoire success success
ladle success success
laravel failure failure
marko success success
nuxt failure success
nx success success
previewjs success success
qwik failure failure
rakkas failure success
remix failure failure
sveltekit success success
unocss success success
vike failure failure
vite-plugin-pwa success success
vite-plugin-react success success
vite-plugin-react-pages success success
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success success
vite-setup-catalogue success success
vitepress success success
vitest success success

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 0c75563: Open

suite result latest scheduled
analogjs success success
astro success success
histoire success success
ladle success success
laravel failure failure
marko success success
nuxt success success
nx success success
previewjs success success
qwik failure failure
rakkas success success
remix failure failure
sveltekit success success
unocss success success
vike failure failure
vite-plugin-pwa success success
vite-plugin-react success success
vite-plugin-react-pages success success
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success success
vite-setup-catalogue success success
vitepress success success
vitest success success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
4 participants