当传递到功能时,vite无法解决文件路径
我正在使用vite捆绑应用程序,在我的一个子页面之一的index.html中,我具有以下功能:
var scriptTag = document.createElement("script");
scriptTag.src = "main.dart.js"; // change to app
scriptTag.type = "application/javascript"; // change to app
document.body.append(scriptTag);
在Web控制台中给出以下错误:
http://localhost:5000/app/main.dart.js net::ERR_ABORTED 404 (Not Found)
n @ app.e9ec45f4.js:1
(anonymous) @ app.e9ec45f4.js:1
setTimeout (async)
(anonymous) @ app.e9ec45f4.js:1
load (async)
(anonymous) @ app.e9ec45f4.js:1
Vite无法解决此路径。我假设这是因为Vite将它们识别为函数的字符串输入,而不是对文件的引用。 有什么方法可以使Vite解决非IMPORT功能中使用的路径?
以下是我目录结构中的相关文件:
src:
- index.html
- app:
- index.html
- main.dart.js
这是我的vite.config.js:
export default defineConfig({
root: "src",
publicDir: "../public",
build: {
emptyOutDir: true,
outDir: "../dist/web",
rollupOptions: {
input: {
main: "src/index.html",
app: "src/app/index.html",
},
external: ["assets", "splash/style.css"],
},
// optimizeDeps: {
// exclude: ["app/flutter_service_worker.js"],
// },
},
resolve: {
alias: {
vue: "vue/dist/vue.esm-bundler.js",
},
},
define: {
"process.env": process.env,
},
plugins: [
VitePWA({
strategies: "injectManifest",
srcDir: "app",
filename: "flutter_service_worker.js",
injectRegister: "null",
}),
],
});
谢谢
I am using Vite to bundle an application, and in the index.html of one of my subpages, I have a function as follows:
var scriptTag = document.createElement("script");
scriptTag.src = "main.dart.js"; // change to app
scriptTag.type = "application/javascript"; // change to app
document.body.append(scriptTag);
Which gives the following error in the web console:
http://localhost:5000/app/main.dart.js net::ERR_ABORTED 404 (Not Found)
n @ app.e9ec45f4.js:1
(anonymous) @ app.e9ec45f4.js:1
setTimeout (async)
(anonymous) @ app.e9ec45f4.js:1
load (async)
(anonymous) @ app.e9ec45f4.js:1
Vite is not resolving this paths. I am assuming this is because Vite recognizes them as string inputs to a function and not a reference to a file. Is there any way to get Vite to resolve paths that are used in non-import functions?
Below are the relevant files in my directory structure:
src:
- index.html
- app:
- index.html
- main.dart.js
Here is my vite.config.js:
export default defineConfig({
root: "src",
publicDir: "../public",
build: {
emptyOutDir: true,
outDir: "../dist/web",
rollupOptions: {
input: {
main: "src/index.html",
app: "src/app/index.html",
},
external: ["assets", "splash/style.css"],
},
// optimizeDeps: {
// exclude: ["app/flutter_service_worker.js"],
// },
},
resolve: {
alias: {
vue: "vue/dist/vue.esm-bundler.js",
},
},
define: {
"process.env": process.env,
},
plugins: [
VitePWA({
strategies: "injectManifest",
srcDir: "app",
filename: "flutter_service_worker.js",
injectRegister: "null",
}),
],
});
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论