英文:
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
│   ├── 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: ".",
});
答案1
得分: 0
如果要使用相对路径,就不能使用/dist
文件夹。默认情况下,/dist
充当保存整个生产构建的文件夹。为了更好地理解这一点,请安装http-server
并在/dist
文件夹内构建后在其中运行它。它将在本地提供与 Web 服务器相同的服务,使用域名。
当你构建应用程序时,它会清空/dist
文件夹的内容,并重新填充所需文件以在生产环境中运行应用程序,收集并压缩所需的依赖项。
如果你想在构建时将特定文件完全复制到/dist
文件夹,而不希望它被解析(这是基本的复制),你必须将它放在/public
文件夹中。然后,你可以通过以/
开头的href
在代码中引用它(用于/public
的根目录)。
示例:href="/assets/style.css"
将引用/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="/assets/style.css"
would reference /public/assets/style.css
.
This works in both dev
(when serving) and prod
(in the deployed app).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论