PostCSS在使用vite中的postcss-extend-rule的@extend时找不到全局CSS bootstrap类。

huangapple go评论64阅读模式
英文:

PostCSS can't find global CSS bootstrap classes when using @extend from postcss-extend-rule in vite

问题

I'm working on a project with (vite, vue 3, ts, and bootstrap) and I can't get "@extend" to work through postCSS.

This is a sample project i created to show as an example of what i want to do: Github repo

if you look at the file src/components/HelloWorld.vue You will see that there is a button with two bootstrap classes ("btn" and "btn-success).

What I want to do is through postCSS implement the @extend functionality to achieve something like this

<template>
  <div class="text-center">
    <button type="button" class="my-btn">Success</button>
  </div>
</template>

<style scoped>
  .my-btn {
    @extend btn btn-success;
  }
</style>

But I can't get this to work, I'm new into postCSS and I don't quite understand what configuration I'm missing to achieve what I want to do.

I have already tried these plugins

https://www.npmjs.com/package/postcss-nesting

https://www.npmjs.com/package/postcss-nested

https://www.npmjs.com/package/postcss-apply

but it seems none of them makes the trick.

Any ideas ?

EDIT: I used @extend prop since is the one I think should be the keyword, but maybe is something like @apply, not sure really.

EDIT 2: I was able to make it work using postcss-extend-rule but only if the extended class was on the same vue file style scope. I think the problem here is to make postCSS able to find bootstrap global classes.

example:

/*this will work*/
.my-class {
  color: red;
  background: pink;
}

.my-btn {
  @extend .my-class;
}

/*this will not work*/
.my-btn {
  @extend .btn;
}
英文:

I'm working on a project with (vite, vue 3, ts, and bootstrap) and I can't get "@extend" to work through postCSS.

This is a sample project i created to show as an example of what i want to do: Github repo

if you look at the file src/components/HelloWorld.vue You will see that there is a button with two bootstrap classes ("btn" and "btn-success).

What I want to do is through postCSS implement the @extend functionality to achieve something like this

&lt;template&gt;
  &lt;div class=&quot;text-center&quot;&gt;
    &lt;button type=&quot;button&quot; class=&quot;my-btn&quot;&gt;Success&lt;/button&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;style scoped&gt;
  .my-btn {
    @extend btn btn-success;
  }

&lt;/style&gt;

But I can't get this to work, I'm new into postCSS and I don't quite understand what configuration I'm missing to achieve what I want to do.

I have already tried these plugins

https://www.npmjs.com/package/postcss-nesting

https://www.npmjs.com/package/postcss-nested

https://www.npmjs.com/package/postcss-apply

but it seems none of them makes the trick.

Any ideas ?

EDIT: I used @extend prop since is the one I think should be the keyword, but maybe is something like @apply, not sure really.

EDIT 2: I was able to make it work using postcss-extend-rule but only if the extended class was on the same vue file style scope. I think the problem here is to make postCSS able to find bootstrap global classes.

example:

/*this will work*/
.my-class { 
  color: red; 
  background: pink; 
} 

.my-btn { 
  @extend .my-class; 
}


/*this will not work*/
.my-btn { 
  @extend .btn; 
}

答案1

得分: 0

需要在您扩展其选择器的文件中导入bootstrap.css,否则PostCSS将无法处理它们。

HelloWorld.vue文件中:

<style>
/* 不需要 "node_modules" */
@import 'bootstrap/dist/css/bootstrap.css';

.my-class {
  color: red;
  background: pink;
}

.my-btn {
  @extend .btn, .btn-success;
}
</style>

PostCSS在使用vite中的postcss-extend-rule的@extend时找不到全局CSS bootstrap类。

英文:

You need to import bootstrap.css in the file where you @extend its selectors. Otherwise, PostCSS will not be able to process them.

In HelloWorld.vue:

&lt;style&gt;
/* no &quot;node_modules&quot; needed */
@import &#39;bootstrap/dist/css/bootstrap.css&#39;;

.my-class {
  color: red;
  background: pink;
}

.my-btn {
  @extend .btn, .btn-success;
}
&lt;/style&gt;

PostCSS在使用vite中的postcss-extend-rule的@extend时找不到全局CSS bootstrap类。

huangapple
  • 本文由 发表于 2023年5月21日 13:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定