我有以下代码来读取我的Node fs项目中的文件。
const files: FileSystemTree = {
"Component.tsx": {
file: {
contents: fs.readFileSync(
`../../apps/components/${name.toLowerCase()}/${name.charAt(0).toUpperCase()}${name.slice(1)}.tsx`
)
}
}
};
这是整个类别的一部分:
import {WebContainer, FileSystemTree} from "@webcontainer/api";
import fs from "fs";
export default class Container {
private mContainer!: WebContainer;
constructor() {
this.CreateContainer();
this.ScaffoldViteApp();
this.StartDevServer();
}
private CreateContainer = async () => {
try {
this.mContainer = await WebContainer.boot();
} catch (err) {
throw new Error(JSON.stringify(err));
}
return this.mContainer;
};
private ScaffoldViteApp = async () => {
try {
this.mContainer.spawn("npm", [
"create",
"vite",
"container-example -- --template react-swc-ts"
]);
} catch (err) {
throw new Error(JSON.stringify(err));
}
return this.mContainer;
};
private StartDevServer = async () => {
try {
this.mContainer.spawn("npm", ["run", "dev"]);
} catch (err) {
throw new Error(JSON.stringify(err));
}
return this.mContainer;
};
public SetComponent(name: string) {
const files: FileSystemTree = {
"Component.tsx": {
file: {
contents: fs.readFileSync(
`../../apps/components/${name.toLowerCase()}/${name.charAt(0).toUpperCase()}${name.slice(1)}.tsx`
)
}
}
};
console.log(fs.readFileSync(
`../../apps/components/${name.toLowerCase()}/${name.charAt(0).toUpperCase()}${name.slice(1)}`
))
try {
this.mContainer.fs.mkdir("components");
} catch (err) {
throw new Error(JSON.stringify(err));
}
try {
this.mContainer.mount(files, {mountPoint: "components"});
} catch (err) {
throw new Error(JSON.stringify(err));
}
return this.mContainer;
}
public GetIframe() {
return this.mContainer.on("server-ready", (port, url) => {
return url;
});
}
public WriteComponent(name: string, content: string) {
const files: FileSystemTree = {
"Component.tsx": {
file: {
contents: content
}
}
};
try {
this.mContainer.mount(files, {mountPoint: "components"});
} catch (err) {
throw new Error(JSON.stringify(err));
}
return this.mContainer;
}
}
当我使用vitest运行时:
Container.SetComponent()
我得到一个错误:
Error: ENOENT: no such file or directory, open '../../apps/components/example/Example'
然而,在命令单击路径时,它确实存在。我用一个试过同样的东西
.tsx
最终没有结果。我还尝试使用ts节点和tsx运行该文件,得到了相同的结果。以下是我的项目结构:
.
âââ README.md
âââ apps
â âââ components
â â âââ example
â â â âââ Example.tsx
â â â âââ stories
â â â â âââ Example.stories.tsx
â â â âââ tests
â â â âââ example.spec.tsx
â â âââ project.json
â âââ docs
â â âââ README.md
â â âââ astro.config.mjs
â â âââ node_modules
â â â âââ @astrojs
â â â â âââ check -> ../../../../node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@astrojs/check
â â â â âââ starlight -> ../../../../node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/@astrojs/starlight
â â â âââ astro -> ../../../node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/astro
â â â âââ sharp -> ../../../node_modules/.pnpm/[email protected]/node_modules/sharp
â â â âââ typescript -> ../../../node_modules/.pnpm/[email protected]/node_modules/typescript
â â âââ package.json
â â âââ project.json
â â âââ public
â â â âââ favicon.svg
â â âââ src
â â âââ assets
â â â âââ houston.webp
â â âââ content
â â â âââ config.ts
â â â âââ docs
â â â âââ components
â â â â âââ example.mdx
â â â âââ guides
â â â â âââ example.md
â â â âââ index.mdx
â â âââ env.d.ts
â âââ storybook
â âââ node_modules
â âââ package.json
â âââ project.json
â âââ tsconfig.json
â âââ tsconfig.storybook.json
âââ docs
â âââ assets
â â âââ highlight.css
â â âââ icons.js
â â âââ icons.svg
â â âââ main.js
â â âââ navigation.js
â â âââ search.js
â â âââ style.css
â âââ functions
â â âââ default.html
â âââ index.html
â âââ modules.html
â âââ types
â âââ Props.html
âââ eslint.config.js
âââ index.css
âââ nx.json
âââ package.json
âââ packages
â âââ docs-sandbox
â â âââ createContainer.ts
â â âââ project.json
â â âââ tests
â â âââ createContainer.spec.ts
â âââ etech-ui-dev
â â âââ project.json
â âââ etech-ui-utils
â âââ project.json
âââ pnpm-lock.yaml
âââ pnpm-workspace.yaml
âââ postcss.config.cjs
âââ reactDocgen.ts
âââ tailwind.config.js
âââ tests
â âââ setup.ts
âââ tsconfig.astro.json
âââ tsconfig.json
âââ tsconfig.node.json
âââ tsconfig.react.json
âââ types
â âââ eslint-plugin-react.d.ts
âââ vite.config.ts