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

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

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

  1. <template>
  2. <div class="text-center">
  3. <button type="button" class="my-btn">Success</button>
  4. </div>
  5. </template>
  6. <style scoped>
  7. .my-btn {
  8. @extend btn btn-success;
  9. }
  10. </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:

  1. /*this will work*/
  2. .my-class {
  3. color: red;
  4. background: pink;
  5. }
  6. .my-btn {
  7. @extend .my-class;
  8. }
  9. /*this will not work*/
  10. .my-btn {
  11. @extend .btn;
  12. }
英文:

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

  1. &lt;template&gt;
  2. &lt;div class=&quot;text-center&quot;&gt;
  3. &lt;button type=&quot;button&quot; class=&quot;my-btn&quot;&gt;Success&lt;/button&gt;
  4. &lt;/div&gt;
  5. &lt;/template&gt;
  6. &lt;style scoped&gt;
  7. .my-btn {
  8. @extend btn btn-success;
  9. }
  10. &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:

  1. /*this will work*/
  2. .my-class {
  3. color: red;
  4. background: pink;
  5. }
  6. .my-btn {
  7. @extend .my-class;
  8. }
  9. /*this will not work*/
  10. .my-btn {
  11. @extend .btn;
  12. }

答案1

得分: 0

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

HelloWorld.vue文件中:

  1. <style>
  2. /* 不需要 "node_modules" */
  3. @import 'bootstrap/dist/css/bootstrap.css';
  4. .my-class {
  5. color: red;
  6. background: pink;
  7. }
  8. .my-btn {
  9. @extend .btn, .btn-success;
  10. }
  11. </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:

  1. &lt;style&gt;
  2. /* no &quot;node_modules&quot; needed */
  3. @import &#39;bootstrap/dist/css/bootstrap.css&#39;;
  4. .my-class {
  5. color: red;
  6. background: pink;
  7. }
  8. .my-btn {
  9. @extend .btn, .btn-success;
  10. }
  11. &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:

确定