vite vue 3库构建不暗中包含dist/style.css

发布于 2025-02-05 16:24:40 字数 2282 浏览 4 评论 0 原文

我构建了一个库项目(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"
  },
  ...
}

我的 dist/文件夹的结构,在运行 npm run build 如下:

dist/
|-components/
|  |-Button.vue.d.ts
|-App.vue.d.ts
|-MyLibraryName.es.js
|-MyLibraryName.umd.js
|-index.d.ts
|-main.d.ts
|-style.css

I built a library project (Vue 3, Vite) and I want to include it in a host project via 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.

Just to clarify, I don't have any .css source files in my project. style.css was compiled from my *.vue components


This is the vite.config.ts for my library project. Everything that I need exported is in 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

{
  "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:

dist/
|-components/
|  |-Button.vue.d.ts
|-App.vue.d.ts
|-MyLibraryName.es.js
|-MyLibraryName.umd.js
|-index.d.ts
|-main.d.ts
|-style.css

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2025-02-12 16:24:40

您需要手动导入CSS,因为您是在包装中独立运送JS和CSS文件。如果您不想手动导入CSS,则需要将您的SFC文件打包用于NPM 。这是VUE 2的文档,但其想法可以完全适用于VUE 3。

请注意:

  • 您必须通过不添加 .vue 文件以及NPM软件包。 >/src 文件夹 .npmignore
  • 在主机项目中的文件,直接从软件包直接导入您的 .vue 文件 package/src/your-component.vue'
  • 此方法将不适用于希望通过< script> 标记直接在浏览器中使用该组件的任何人,仅运行时构建或构建过程,这些过程不了解如何使用 .vue 文件
  • 某些组件可能会提供副作用,例如指令,或扩展其他库,并使用
  • 手动导入CSS文件的其他功能,永远不会是坏主意和我知道使用的几乎所有VUE软件包

You need to manually import your CSS because you are shipping JS and CSS files independently in your package. If you don't want to manually import your CSS, you need to package your SFC files for npm. This is the document for Vue 2, but its idea can totally apply to 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
  • In your host project, import your .vue file directly from your package 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
  • 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文