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.

Vue build lib doubles css code

See original GitHub issue

Describe the bug

I’m creating component library for using in non-vue application, simple component:

<template>
<h1>{{ message }}</h1>
</template>

<script lang="ts">
import {defineComponent, ref} from 'vue';

export default defineComponent({
    setup() {
        let message = ref('ok')
        return {
            message
        }
    }
})

</script>

<style>
h1 {
    color: red;
}
</style>

and vite.config.ts:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path');

export default defineConfig({
    build: {
        lib: {
            name: "Test",
            entry: path.resolve(__dirname, `src/views/Test/Test.vue`),
            formats: ['iife'],
            fileName: (format) => "[name].js",
        },
        rollupOptions: {
            external: ['vue'],
            output: { globals: { 'vue': 'Vue', } }
        }
    },
    plugins: [
        vue()
    ]
})

produces two files: Test.js and style.css, but in Test.js I have a copy of style.css content in variable which is never used:

var Test=function(e){"use strict";var p=(()=>`h1{color:red}`)(), .....

how can I prevent it ? I’ve tested every setting, every format (but I prefer iife), it is always inside JS. Maybe I can use it in component without load external css but how ?

Reproduction

https://github.com/dany28/vue3-library-test

System Info

System:
    OS: macOS 12.4
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 68.78 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    npm: 8.5.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 102.0.5005.115
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-vue: ^2.3.3 => 2.3.3 
    vite: ^2.9.12 => 2.9.12

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sapphi-redcommented, Jun 30, 2022

Thanks for digging it down.

Ok, in 2.9.9 it works with this code:

Yes this is affected by #8471 which included from v2.9.10.

About the actual bug: is it possible that the /* #__PURE__ */ comment is somehow ignored by esbuild? or that its stripped away before the minifier runs?

Yes. For esm builds, esbuild works well. But for iife and umd builds, it turns out to be ignored (https://github.com/evanw/esbuild/issues/639). Terser works because it handles this case too.

I have created a PR (https://github.com/evanw/esbuild/pull/2360) to esbuild to fix this, but I might revert #8471 for v2.

0reactions
sapphi-redcommented, Jul 3, 2022

Closing as it is fixed by #8874 (v2) and #8896 (v3).

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue-cli build a lib SSR - CSS issue - Stack Overflow
My final solution is just setting up vue-cli-service to build the code as a library like so. And no need to change anything...
Read more >
Build Targets - Vue CLI
When you run vue-cli-service build , you can specify different build targets via the --target option. This allows you to use the same...
Read more >
Build a CSS Theme Switcher in Vue JS | by Nevin Katz - Medium
Build a CSS theme switcher with Vue JS, a Javascript framework. Use properties, methods, and Vue bindings such as v-for, v-on, and v-model....
Read more >
Vue Components as Custom Elements - Máximo Mussini
The first step is to create a Vue component that we would like to use as a custom element. In this example, we...
Read more >
How To Generate a Vue.js Single Page App With the Vue CLI
The browser cannot read this, so when the project is built, Node will compile all of your SCSS or LESS code to traditional...
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