Vue-loader: CSS duplicate

Created on 21 Jun 2016  路  9Comments  路  Source: vuejs/vue-loader

[email protected]

Component A:

<template>
  <div></div>
</template>
<style lang="scss">
@import "~bulma";

/* do something */
</style>

Component B:

<template>
  <div></div>
</template>
<style lang="scss">
@import "~bulma";

/* do something *
</style>

Problem:

The bulma library will be import twice.

How to fix this?

Real show case, see http://vue-admin.fundon.me/#!/ui/notifications.

screen shot 2016-06-22 at 3 31 39 am

Most helpful comment

I ran into the same issue, cssnano did indeed remove it when compiling for production. @yyx990803 is there anyway to achieve de-duplication in development mode?

All 9 comments

Are you using mixins from the lib? If not you should import it only once in your root component.

If not, this should be deduped by cssnano when you are building for production.

I also encountered the problem. I split WeUI's less styles into Vux's every component, so some less files are imported more than once. This works fine when developing. But after building, the duplicate styles remain. So I simply created a script to compress it(142k -> 90k). But I wonder if there is a easier way?

const cssnano = require('cssnano')
const fs = require('fs')
const path = require('path')

const code = fs.readFileSync(path.resolve(__dirname, '../dist/vux.css'))
cssnano.process(code.toString(), {zindex: false}).then(function (result) {
  fs.writeFileSync(path.resolve(__dirname, '../dist/vux.min.css'), result.css)
})

I try to config vue loader's postcss option, but it doesnot work.

vue: {
    postcss: [require('cssnano')({zindex:false})]
 }

@airyland I sloved it.

Using mixins from the lib.
Like https://github.com/fundon/vue-admin/blob/master/src/components/Sidebar.vue#L78.

@fundon Hello, the provided URL gives error 404, can you explain the solution?
Edit:
Well, I found it. the solution is to import only the variables, so it won't affect the size when it gets compiled

I ran into the same issue, cssnano did indeed remove it when compiling for production. @yyx990803 is there anyway to achieve de-duplication in development mode?

@c4milo when using scss or sass, import the variables and mixins only. For styles, you can import them once in the main file

I would also like to know how to avoid dublicate styles in development.

@christophwolff as I said, separate your variables from your styles, import your variables everywhere you need them. Import your styles once in the root component. Good luck.

@mrg0lden Thank you. But my Problem is, i want to use the grid system from bootstrap in some components. And these are mixins. @include media-breakpoint-up i.e. So there is no other way around that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lijialiang picture lijialiang  路  3Comments

birdgg picture birdgg  路  3Comments

jorgy343 picture jorgy343  路  3Comments

amorphine picture amorphine  路  3Comments

githoniel picture githoniel  路  3Comments