简体   繁体   English

Vite Vue 3 库构建不隐式包含 dist/style.css

[英]Vite Vue 3 library build doesn't implicitly include dist/style.css

I built a library project (Vue 3, Vite) and I want to include it in a host project via package.json .我构建了一个库项目(Vue 3,Vite),我想通过package.json将它包含在一个宿主项目中。

But I faced a problem where I can import the components and run a simple programme with those imported components but their styles are gone.但是我遇到了一个问题,我可以导入组件并使用这些导入的组件运行一个简单的程序,但它们的样式已经消失了。

Please let me know what is wrong with my config.请让我知道我的配置有什么问题。 It doesn't make sense when I have to manually import css into my host project.当我必须手动将 css 导入到我的宿主项目中时,这没有任何意义。

Just to clarify, I don't have any .css source files in my project.澄清一下,我的项目中没有任何.css源文件。 style.css was compiled from my *.vue components style.css是从我的*.vue组件编译而来的


This is the vite.config.ts for my library project.这是我的图书馆项目的vite.config.ts Everything that I need exported is in src/ .我需要导出的所有内容都在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,
  }
} )

And this is the relevant part of my package.json这是我的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"
  },
  ...
}

The structure of my dist/ folder after running npm run build is as follows:运行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

You need to manually import your CSS because you are shipping JS and CSS files independently in your package.您需要手动导入 CSS,因为您在包中独立地传送 JS 和 CSS 文件。 If you don't want to manually import your CSS, you need to package your SFC files for npm .如果您不想手动导入 CSS,则需要为 npm 打包 SFC 文件 This is the document for Vue 2, but its idea can totally apply to Vue 3.这是 Vue 2 的文档,但它的想法完全适用于 Vue 3。

Here are some points to note:这里有几点需要注意:

  • You must ship your .vue files along with your npm package by NOT adding the /src folder to the .npmignore file您必须通过不将/src文件夹添加到.npmignore文件来将.vue文件与 npm 包一起提供
  • In your host project, import your .vue file directly from your package import YourComponent from 'your-package/src/your-component.vue'在你的宿主项目中,直接从你的包中导入你的.vue文件import YourComponent from 'your-package/src/your-component.vue'
  • This approach will not work for anyone who wishes to use the component directly in a browser via the <script> tag, anyone who uses a runtime-only build or build processes which don't understand what to do with .vue files此方法不适用于希望通过<script>标签直接在浏览器中使用该组件的任何人、使用仅运行时构建或不了解如何处理.vue文件的构建过程的任何人
  • Some components might provide side effects like directives, or extend other libraries with additional functionality某些组件可能会提供诸如指令之类的副作用,或者使用附加功能扩展其他库
  • Manually importing CSS files will never be a bad idea and almost all the Vue packages I know use that approach手动导入 CSS 文件绝不是一个坏主意,我知道的几乎所有 Vue 包都使用这种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM