Vite 无法加载位于 public 目录中的 CSS 文件。

huangapple go评论82阅读模式
英文:

Vite unable to load CSS file located in public directory

问题

I've got the following project:

├── README.md
├── dist
│    ├── components
│    │    ├── checkbox.css
│    │    └── input.css
│    ├── styles.css
│    └── styles.js
├── docs
│    ├── index.html
│    ├── main.css
│    ├── package.json
│    ├── tailwind.config.js
│    ├── vite.config.js
│    └── yarn.lock
├── package.json
├── src
│    ├── components
│    │    ├── checkbox.css
│    │    ├── input.css
│    │    ├── postcss.config.js
│    │    └── tailwind.config.js
│    └── index.js
├── visable.yaml
└── yarn.lock

Inside the docs/index.html file I reference two CSS files:

    <link rel="stylesheet" href="./main.css" />
    <link rel="stylesheet" href="../dist/styles.css" />

The main file is loaded, but not the styles.css:

Here's my vite.config:

import { defineConfig } from "vite";

export default defineConfig({
  server: {
    open: "index.html",
  },
  publicDir: "dist",
  root: ".",
});
英文:

I've got the following project:

├── README.md
├── dist
│&#160;&#160; ├── components
│&#160;&#160; │&#160;&#160; ├── checkbox.css
│&#160;&#160; │&#160;&#160; └── input.css
│&#160;&#160; ├── styles.css
│&#160;&#160; └── styles.js
├── docs
│&#160;&#160; ├── index.html
│&#160;&#160; ├── main.css
│&#160;&#160; ├── package.json
│&#160;&#160; ├── tailwind.config.js
│&#160;&#160; ├── vite.config.js
│&#160;&#160; └── yarn.lock
├── package.json
├── src
│&#160;&#160; ├── components
│&#160;&#160; │&#160;&#160; ├── checkbox.css
│&#160;&#160; │&#160;&#160; ├── input.css
│&#160;&#160; │&#160;&#160; ├── postcss.config.js
│&#160;&#160; │&#160;&#160; └── tailwind.config.js
│&#160;&#160; └── index.js
├── visable.yaml
└── yarn.lock

Inside the docs/index.html file I reference two CSS files:

    &lt;link rel=&quot;stylesheet&quot; href=&quot;./main.css&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;../dist/styles.css&quot; /&gt;

The main file is loaded, but not the styles.css:
Vite 无法加载位于 public 目录中的 CSS 文件。

Here's my vite.config:

import { defineConfig } from &quot;vite&quot;;

export default defineConfig({
  server: {
    open: &quot;index.html&quot;,
  },
  publicDir: &quot;dist&quot;,
  root: &quot;.&quot;,
});


答案1

得分: 0

如果要使用相对路径,就不能使用/dist文件夹。默认情况下,/dist充当保存整个生产构建的文件夹。为了更好地理解这一点,请安装http-server并在/dist文件夹内构建后在其中运行它。它将在本地提供与 Web 服务器相同的服务,使用域名。

当你构建应用程序时,它会清空/dist文件夹的内容,并重新填充所需文件以在生产环境中运行应用程序,收集并压缩所需的依赖项。

如果你想在构建时将特定文件完全复制到/dist文件夹,而不希望它被解析(这是基本的复制),你必须将它放在/public文件夹中。然后,你可以通过以/开头的href在代码中引用它(用于/public的根目录)。

示例:href=&quot;/assets/style.css&quot; 将引用/public/assets/style.css

这在dev(在提供服务时)和prod(在已部署的应用程序中)中都有效。

英文:

If you want to use relative paths you can't use /dist folder. By default, /dist acts as the folder holding your entire production build. To better understand this, install http-server and run it inside /dist folder, after you build. It will serve on local exactly what a web server would, on a domain.

When you build the app, it wipes out the contents of /dist folder and re-populates it with all the files needed to run the app in production, gathering and minifying required dependencies.

If you want a particular file to be copied verbatim to /dist folder when building, without it being parsed at all (it's a basic copy), you have to place it in /public folder. You can then reference it in your code by starting the href with / (for the root of /public).

Example: href=&quot;/assets/style.css&quot; would reference /public/assets/style.css.

This works in both dev (when serving) and prod (in the deployed app).

huangapple
  • 本文由 发表于 2023年3月15日 20:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定