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(plugin-legacy): chunk may not exist #3886

Merged
merged 1 commit into from
Jun 25, 2021
Merged

Conversation

y1d7ng
Copy link
Contributor

@y1d7ng y1d7ng commented Jun 21, 2021

Description

fix #3670

Additional context

import xxx from './xxx?worker&inline'

worker&inline will cause rollup to compile the web worker script.

const bundle = await rollup.rollup({
input: cleanUrl(id),
plugins: config.plugins as Plugin[]
})
try {
const { output } = await bundle.generate({

The option plugins contains vite:build-html plugin. chunk.facadeModuleId === id makes an undefined chunk.

const chunk = Object.values(bundle).find(
(chunk) =>
chunk.type === 'chunk' &&
chunk.isEntry &&
chunk.facadeModuleId === id
) as OutputChunk | undefined

I think that the worker plugin's rollup compile should not use vite:build-html as a plugin, but considering that chunk may not exist under other conditions. I added a judgment in plugin-legacy's transformIndexHtml.


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.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • 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).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

I think this fix is ok as a workaround until the real issue is solved. It makes sense at least because it is documented that chunk can be undefined here.

Also, the real fix for worker&inline doesn't seems trivial. I think it should be resolved though because other issues could appear.

In this commit, instead of resolving new plugins for each worker, the same plugins from the resolved config started to be reused. This means that caches are shared between the main bundle and the bundle performed for the worker (before inlining the result). The bundle of the worker should be isolated from other bundles. This is why in this line

for (const [id, html] of processedHtml) {
processedHtml includes the main bundle HTML while processing the worker bundle.

But this was probably not even properly working before while regenerating the plugins, since some caches depends on the resolved config (maybe we need to key the cache on the plugin+resolvedConfig to enable the previous pattern). Or the resolved config could be cloned so new caches are kept for the worker bundle.

@y1d7ng
Copy link
Contributor Author

y1d7ng commented Jun 22, 2021

@patak-js Thanks for your explanation. I'm inclined to the solution you proposed ( the resolved config could be cloned so new caches are kept for the worker bundle).

@patak-dev patak-dev requested a review from antfu June 22, 2021 08:30
@antfu antfu merged commit dd5931d into vitejs:main Jun 25, 2021
@y1d7ng y1d7ng deleted the hotfix/3670 branch June 28, 2021 14:38
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
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.

[@vitejs/plugin-legacy] Cannot read property 'fileName' of undefined with ?worker&inline
3 participants