Closed laterdayi closed 10 months ago
Please share a minimal reproducible example.
import {
MeshBasicMaterial,
MeshStandardMaterial,
Mesh,
PerspectiveCamera,
Raycaster,
Scene,
Texture,
TextureLoader,
WebGLRenderer,
Vector2,
Color
} from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
fetch("./machine-room.ts")
.then(response => {
response.text().then(e => {
console.log(e);
});
})
What I got instead
import {
MeshBasicMaterial,
PerspectiveCamera,
Raycaster,
Scene,
TextureLoader,
WebGLRenderer,
Vector2
} from "/.vitepress/cache/deps/three.js?v=90219ecf";
import { OrbitControls } from "/.vitepress/cache/deps/three_examples_jsm_controls_OrbitControls__js.js?v=90219ecf";
import { GLTFLoader } from "/.vitepress/cache/deps/three_examples_jsm_loaders_GLTFLoader__js.js?v=90219ecf";
How can I prevent import "path" from being converted when fetch reads the contents of a file
Thank you for your help @brc-dd
It's getting compiled by vite. Try moving that file to public directory? This won't work anyway during builds, there will be no ./machine-room.ts
in dist.
You can also do this:
import machineRoomCode from './machine-room.ts?raw'
console.log(machineRoomCode)
or if you want that code in your markdown file, simply use https://vitepress.dev/guide/markdown#import-code-snippets
My scenario is that I put the ts vue file in docs, fetch the file content, and then hand it over to stackblitz to dynamically generate the project, but response.text() will convert the path of import to the path in the.vitepres cache. There is no address that can be configured to prevent paths from being escaped
I want to import "three" module in the fetch response.text file instead of import ".vitepress cache".
Importing the code as raw should work for your case. Alternatively, use a build time data loader - https://vitepress.dev/guide/data-loading -- you'll need to do fs.readFile there and return the data. fetch
ing won't work.
Thank you very much for your help. Is there a vite or vitepress configuration that can prevent file import path from being converted
@laterdayi Do something like this: https://stackblitz.com/edit/vite-vuzman?file=docs/index.md
raw is OK, but I want to loop all files through import.meta-.glob,
how can I dynamically import form "*? raw "
const files = import.meta.glob(".. /machine-room/**/*");
const obj = {}
for (const path in files) {
// How can I import *.raw dynamically here
obj[path] = result
}
https://vitejs.dev/guide/features.html#glob-import-as
import.meta.glob('../machine-room/**/*', { as: 'raw', eager: true })
I made it. Thank you so much for your help
@brc-dd I use const files = import.meta-. glob("../machine-room/*/", { as: "raw" }); , but it does not contain this file, is there a way to solve it?
Which file? machine-room.ts
? You'll need to modify your glob pattern for that. Can you share the directory structure?
Describe the bug
After fetch(file) response.text(), the import path of the file content is incorrect
Be converted to
Reproduction
*
Expected behavior
After fetch(file) response.text(), the import path of the file content is incorrect
After fetch(file) response.text() After that, keep the original import "*"
System Info
Additional context
No response
Validations