![](/img/trans.png)
[英]vite: importing css/sass into component should not also include styles in style.css
[英]Vite Vue 3 library build doesn't implicitly include dist/style.css
我构建了一个库项目(Vue 3,Vite),我想通过package.json
将它包含在一个宿主项目中。
但是我遇到了一个问题,我可以导入组件并使用这些导入的组件运行一个简单的程序,但它们的样式已经消失了。
请让我知道我的配置有什么问题。 当我必须手动将 css 导入到我的宿主项目中时,这没有任何意义。
澄清一下,我的项目中没有任何.css
源文件。 style.css
是从我的*.vue
组件编译而来的
这是我的图书馆项目的vite.config.ts
。 我需要导出的所有内容都在src/
中。
// Library project
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import typescript from '@rollup/plugin-typescript';
const path = require("path")
// https://vitejs.dev/config/
export default defineConfig( {
plugins: [{
...typescript( { tsconfig: "./tsconfig.json" } ),
apply: "build",
declaration: true,
declarationDir: "types/",
rootDir: "/",
}, vue()],
resolve: { alias: { "@": path.resolve(__dirname, "./src") } },
build: {
lib: {
entry: path.resolve(__dirname, "src/index.ts"),
name: "gsd-vue-egui",
// fileName: (format) => `gsd-vue-egui.${format}.js`,
},
rollupOptions: {
external: ["vue"],
output: {
// Provide global variables to use in the UMD build
// Add external deps here
globals: { vue: "Vue" },
},
},
},
server: {
port: 30001,
}
} )
这是我的package.json
的相关部分
{
"name": "gsd-vue-egui",
"private": true,
"version": "0.0.0",
"scripts": {
"preinstall": "npx npm-force-resolutions",
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"test": "npm run test:unit",
"test:unit": "jest --config=jest.config.js test",
"lint:css": "stylelint --formatter verbose --config .stylelintrc \".\" --fix",
"lint:eslint": "eslint --ext js,vue,ts \".\" --fix",
"lint": "npm run lint:css && npm run lint:eslint"
},
...
}
运行npm run build
后我的dist/
文件夹的结构如下:
dist/
|-components/
| |-Button.vue.d.ts
|-App.vue.d.ts
|-MyLibraryName.es.js
|-MyLibraryName.umd.js
|-index.d.ts
|-main.d.ts
|-style.css
您需要手动导入 CSS,因为您在包中独立地传送 JS 和 CSS 文件。 如果您不想手动导入 CSS,则需要为 npm 打包 SFC 文件。 这是 Vue 2 的文档,但它的想法完全适用于 Vue 3。
这里有几点需要注意:
/src
文件夹添加到.npmignore
文件来将.vue
文件与 npm 包一起提供.vue
文件import YourComponent from 'your-package/src/your-component.vue'
<script>
标签直接在浏览器中使用该组件的任何人、使用仅运行时构建或不了解如何处理.vue
文件的构建过程的任何人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.