当传递到功能时,vite无法解决文件路径

发布于 2025-02-12 01:05:30 字数 1642 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文