vite icon indicating copy to clipboard operation
vite copied to clipboard

fix: escape characters in dynamic import (fix #11824)

Open sun0day opened this issue 2 years ago • 4 comments

Description

fix #11824

Additional context


What is the purpose of this pull request?

  • [x] Bug fix
  • [ ] New Feature
  • [ ] Documentation update
  • [ ] Other

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

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

sun0day avatar Jan 28 '23 12:01 sun0day

Thanks for the PR!

I digged this down and found that dynamicImportToGlob returns ./(foo)/*.js when ./(foo)/${name}.js is passed. https://github.com/rollup/plugins/blob/bc5cf91a042b88e14a1da9ba25f501d92591ca8a/packages/dynamic-import-vars/src/dynamic-import-to-glob.js#L79 But I expect this function to return ./\(foo\)/*.js instead.

I guess @rollup/plugin-dynamic-import-vars has a similar bug because of this reason. I think it's better to fix plugin-dynamic-import-vars.

sapphi-red avatar Feb 01 '23 08:02 sapphi-red

Thanks for the PR!

I digged this down and found that dynamicImportToGlob returns ./(foo)/*.js when ./(foo)/${name}.js is passed. https://github.com/rollup/plugins/blob/bc5cf91a042b88e14a1da9ba25f501d92591ca8a/packages/dynamic-import-vars/src/dynamic-import-to-glob.js#L79 But I expect this function to return ./\(foo\)/*.js instead.

I guess @rollup/plugin-dynamic-import-vars has a similar bug because of this reason. I think it's better to fix plugin-dynamic-import-vars.

Yeah. I've aware of that. But the thing is that not only dynamicImportVars needs to support that but also importMetaGlob . e.g:

import.meta.glob(`../(parenthesis)/**/*.js`)

sun0day avatar Feb 01 '23 09:02 sun0day

In that case, I expect users to escape the parenthesis. Unlike import(`./foo/${mod}.js`), import.meta.glob recieves a glob pattern and not a import specifier.

The glob matching is done via fast-glob - check out its documentation for supported glob patterns. https://vitejs.dev/guide/features.html#glob-import:~:text=The%20glob%20matching%20is%20done%20via%20fast%2Dglob%20%2D%20check%20out%20its%20documentation%20for%20supported%20glob%20patterns.

So in the example you showed, I expect users to write import.meta.glob(`../\(parenthesis\)/**/*.js`).

sapphi-red avatar Feb 01 '23 10:02 sapphi-red

Alright. Let's submit this bug to @rollup/plugin-dynamic-import-vars upstream. You can close this PR

sun0day avatar Feb 01 '23 10:02 sun0day

Let's close this one in favour of fixing upstream then. Thanks for starting the work on this!

bluwy avatar Apr 01 '23 12:04 bluwy