question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

use the plugin with vite-plugin-import-style, but style not loaded

See original GitHub issue

config as follows:

// main.ts
import { createApp } from 'vue'
// import { Button } from 'ant-design-vue'
// import Antd from 'ant-design-vue'
import App from './App.vue'

import 'virtual:windi.css'

const app = createApp(App);

// app.use(Button)
// app.use(Antd)

app.mount('#root', true)
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ViteComponents, {
  AntDesignVueResolver
} from 'vite-plugin-components'
// import ViteCompression from 'vite-plugin-compression'
import ViteStyleImport from 'vite-plugin-style-import'
import WindiCSS from 'vite-plugin-windicss'

// https://vitejs.dev/config/
export default defineConfig({
  // 1. If you are using the ant-design series, you need to configure this
  // 2. Make sure less is installed in the dependency `yarn add less -D`
  css: {
    preprocessorOptions: {
      less: {
        // modifyVars: {},
        javascriptEnabled: true,
      },
    },
  },
  plugins: [
    vue(),

    // https://github.com/antfu/vite-plugin-components
    ViteComponents({
      globalComponentsDeclaration: true,
      customComponentResolvers: [
        AntDesignVueResolver(),
      ]
    }),

    // https://github.com/windicss/vite-plugin-windicss
    WindiCSS(),

    // https://github.com/anncwb/vite-plugin-compression
    // ViteCompression(),

    // https://github.com/anncwb/vite-plugin-style-import
    ViteStyleImport({
      libs: [
        {
          libraryName: 'ant-design-vue',
          esModule: true,
          resolveStyle: (name) => {
            return `ant-design-vue/es/${name}/style/index`;
          },
        }
      ]
    }),
  ]
})
<template>
  <a-button type="primary">Ant Button</a-button>
</template>

style not generate

image

what should i do

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ngekodingcommented, Jun 7, 2021

You can use customComponentResolvers with sideEffects option to import the style (no need to use ViteStyleImport).

Here is my config:

// Import kebabCase for formatting
import ViteComponents, { kebabCase } from 'vite-plugin-components'

ViteComponents({
  customComponentResolvers: [
    (name) => {
      if (name.match(/^A[A-Z]/)) { // Ant Design Vue
        const importName = name.slice(1)
        const dirName = kebabCase(importName) // e.g. date-picker
        return { 
          importName, 
          path: 'ant-design-vue/es',
          sideEffects: `ant-design-vue/es/${dirName}/style`,
        }
      } 
    }
  ]
})

The full example of this can be found here https://github.com/ngekoding/vite-project-explore

0reactions
ngekodingcommented, Jun 11, 2021

@agoni1212 Ah you are right, after trying that component I got errors. Actually currently I use ElementUI library, so don’t know that problem before, maybe I need make a test for adv next time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vbenjs/vite-plugin-style-import - GitHub
Why only import styles. Because vite itself has imported the component library on demand, only the style is not on demand, so just...
Read more >
Features | Vite
In this case, the processed CSS string is returned as the module's default export as usual, but the styles aren't injected to the...
Read more >
vite-plugin-federation doesn't load the styles of my components
This is my vite.conf.ts: import federation from "@originjs/vite-plugin-federation"; import { ...
Read more >
Vite error ( not showing CSS styles) - Laracasts
I have hosted an app to a Server, and everything is fine except that the Vite file causes two errors on my Browser....
Read more >
vite-plugin-style-import - npm
Start using vite-plugin-style-import in your project by running `npm i ... the style is not on demand, so just import the style on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found