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

Can not dynamically import a raw file in production mode. #4252

Closed
6 tasks done
leon-kfd opened this issue Jul 15, 2021 · 5 comments
Closed
6 tasks done

Can not dynamically import a raw file in production mode. #4252

leon-kfd opened this issue Jul 15, 2021 · 5 comments
Labels
duplicate This issue or pull request already exists pending triage

Comments

@leon-kfd
Copy link

Describe the bug

Can not dynamically import a raw file in production mode.
But it can work in dev mode.

Main code:
image

Run build & server, it happen:
image

Reproduction

https://github.com/leon-kfd/vite-dynamic-import-raw-file

Reproduction:

yarn
npm run build
npm run serve

Click load file button, will throw error in console.

System Info

System:
    OS: Windows 10 10.0.18362
    CPU: (6) x64 Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz
    Memory: 6.24 GB / 15.86 GB
  Binaries:
    Node: 14.15.1 - D:\Software\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.8 - D:\Software\nodejs\npm.CMD
  Browsers:
    Chrome: 91.0.4472.124
    Edge: Spartan (44.18362.1.0)
    Internet Explorer: 11.0.18362.1
  npmPackages:
    @vitejs/plugin-vue: ^1.2.5 => 1.2.5
    vite: ^2.4.2 => 2.4.2

Used Package Manager

yarn

Logs

no error log

Validations

@dejour
Copy link
Contributor

dejour commented Jul 15, 2021

current situation is glob pattern transformed by @rollup/plugin-dynamic-import-vars (./source/file*.md?raw) can't match any files, maybe you could work around this by using a js file which return markdown string.

@Sociosarbis
Copy link
Contributor

Sociosarbis commented Jul 15, 2021

The problem originates from @rollup/plugin-dynamic-import-vars in build mode.

This plugin converts dynamic import expression with variables (which is not static) to string (say string A) with asterisks .

Then glob with string A to search the actual files to import.

But glob does not support query (?raw in this case).

You can visit here for the related code.

The following is what this plugin would generate.
image

I think vite can implement its own plugin-dynamic-import-vars to fix the bug, as the current one doesn't seem to be flexible enough to handle more future cases.

@leon-kfd At this time, consider use vite-plugin-plain-text (which is included in the awesome-vue collection) as a workaround.

@mesqueeb
Copy link

mesqueeb commented Nov 8, 2021

I have the same issue. +1 for supporting this in Vite and VitePress.

@mesqueeb
Copy link

mesqueeb commented Nov 8, 2021

duplicate of #3222

@theonelucas
Copy link

My temporary "solution" was to just fetch the content with a get and read it as text. As I'm fetching svg icons, I couldn't just use them as the src of an image cause that way they wouldn't be editable. It's a horrible hack, but at least it's the same behavior in dev and prod...

    const response = await fetch(`${this.CDN}/assets/icons/${this.icon}.svg`);
    const text = await response.text();`

@Niputi Niputi added the duplicate This issue or pull request already exists label Nov 30, 2021
@Niputi Niputi closed this as completed Nov 30, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Dec 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists pending triage
Projects
None yet
Development

No branches or pull requests

6 participants