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(build-import-analysis): treeshaken dynamic import when injecting preload #14221

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

sun0day
Copy link
Member

@sun0day sun0day commented Aug 28, 2023

Description

fix #14145

refs rollup/rollup#4952

After this PR, vite can treeshaken following dynamic imports when injecting __vitePreload:

 const { foo } = await import('./treeshaken/treeshaken.js')
  const { bar, default: tree } = await import('./treeshaken/treeshaken.js')
  const baz1 = (await import('./treeshaken/treeshaken.js')).baz1
  const baz2 = (await import('./treeshaken/treeshaken.js')).baz2.log
  const baz3 = (await import('./treeshaken/treeshaken.js')).baz3?.log
  const baz4 = await import('./treeshaken/treeshaken.js').then(
    ({ baz4 }) => baz4,
  )
  const baz5 = await import('./treeshaken/treeshaken.js').then(function ({
    baz5,
  }) {
    return baz5
  })

Additional context


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 PR Title 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.

@stackblitz
Copy link

stackblitz bot commented Aug 28, 2023

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

Comment on lines 49 to 50
const dynamicImportTreeshakenRE =
/(\b(const|let|var)\s+(\{[^}.]+\})\s*=\s*await\s+import\([^)]+\))|(\(\s*await\s+import\([^)]+\)\s*\)(\??\.[^;[\s]+)+)|\bimport\([^)]+\)(\.then\([^{]*\{([^}]+)\})/g
Copy link
Contributor

Choose a reason for hiding this comment

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

The below case cannot be supported.

import('./deps.js')
  .then({ a } => {})

You need to add \s* before .then

IMO, using regexp to handle this might break many edge-case

Copy link
Member Author

@sun0day sun0day Aug 28, 2023

Choose a reason for hiding this comment

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

Yeah, regexp can hardly cover all the edge-cases, but it seems there's no better way to support dynamic import treeshaking within current logic. Parsing the ast can completely handle this issue but it's too expensive.

IMO, we could support the common usages of dynamic import for now, and then patch the related issues in the future.

@bluwy bluwy added the p3-significant High priority enhancement (priority) label Sep 18, 2023
@bluwy
Copy link
Member

bluwy commented Sep 18, 2023

IIUC this PR transforms dynamic imports with __vitePreload to still preserve the const { ... } = await import('...') syntax temporarily so it still works? It's a bit hard to see how it transforms without more comments or snapshots 😬

But I think the regex and implementation is fairly impressive. I'd lean to a more robust alternative still, but it's also harder. Ideally if __vitePreload is only injected in the render phase, but that makes the preload code hard to be chunked. So perhaps this is good enough for now.

@sun0day
Copy link
Member Author

sun0day commented Sep 18, 2023

IIUC this PR transforms dynamic imports with __vitePreload to still preserve the const { ... } = await import('...') syntax temporarily so it still works?

Yeah, that's what this pr mainly does. The regexp contains three sub-regexps related to three dynamic import formats, e.g:

  • const {foo} = await import('foo')
  • (await import('foo')).foo
  • import('foo').then(({foo})=>{})

After injecting __vitePreload, they will be transformed to something like:

  • const {foo} = await __vitePreload(async () => { const {foo} = await import('foo');return {foo}}, ...)
  • __vitePreload(async () => { const {foo} = await import('foo');return { foo }},...).then(({foo})=>{})
  • __vitePreload(async () => { const __vite_temp__ = (await import('foo')).foo; return { foo: __vite_temp__ }},...)).foo

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I'm open to trying this approach in the beta to see if it works, but I'll see what the others think about it too.

if (match[4]) {
dynamicImports[
dynamicImportTreeshakenRE.lastIndex - match[5]?.length - 1
] = { chains: match[5] }
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to make this:

const names = match[5].match(/\.([^.?]+)/)?.[1] || ''
... = { declarations: `const {${names}}`, names: : `{ ${names} }`  }

The regex is borrowed from line 386, which maybe could be simplified and extracted.

This way all cases are consistently typed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that would be better 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-significant High priority enhancement (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vite not tree-shaking dynamic imports
3 participants