less-loader: @import inserts duplicates
If you have file foo:
@import 'base.less';
.foo { ... }
and file bar:
@import 'base.less';
.bar { ... }
…and you webpack both foo
and bar
into the same bundle.js
, the bundle will have two instances of all rules from base.less
. (this is true even if using the dedupe plugin).
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 10
- Comments: 30 (9 by maintainers)
The solution is to use
@import (reference)
:I don’t see this as “less’ behavior” at all. The default behavior for @import in less is “@import (once)”. So, in my opinion, this is the opposite of less’ behavior.
Not yet. PRs welcome though 😃
If less’ default behavior now is
@import (once)
and the issue still exists, we should revise it. At the time of writing, it was not less’ default behavior.If the imported file contains classes that you are using in your HTML, you should still import it somewhere without
(reference)
.Importing something with
@import (reference)
means that you are not interested in the css output, but in variables, mixins or css-classes because you are extending them.Would LOVE a fix for this!
I don’t think that we will fix this. It’s less’ behavior and webpack’s output should not be different than less’ output.
Is this a solution for 2.?
The
@import (reference)
way works great form me. Especially using with vue-router, there is a common App.vue for entrance, where I put the@import
common.less for once, and the rest pages, just import them with (reference).I agree with @tommck Default less behaviour is to include things just once
While the original issue here is related to sass/less/stylus, a fix/hack/patch for this is to use the
optimize-css-assets-webpack-plugin
in conjunction withextract-text-webpack-plugin
to produce, like this: https://gist.github.com/phun-ky/766e5ec9f75eac61c945273a951f0c0b.If you want to use this in dev, you will have to use a plugin like
write-file-webpack-plugin
to force webpack to write the file to disk in dev.