自定义 Tailwind CSS 时引用默认颜色

Posted

技术标签:

【中文标题】自定义 Tailwind CSS 时引用默认颜色【英文标题】:Referencing Default Colors When Customizing Tailwind CSS 【发布时间】:2020-06-20 19:00:09 【问题描述】:

我正在尝试使用他们的tailwind.config.js 文件来extend 顺风 css 的调色板。我的目标是创建一组可以在设置网站样式时使用的主要颜色和次要颜色。我想通过引用默认顺风主题中已经存在的颜色来做到这一点。基于documentation,我虽然可以使用以下方法(但它不起作用):

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = 
  theme: 
    extend: 
      colors: 
        primary: 
          light: defaultTheme.colors['400'],
          default: defaultTheme.colors['500'],
          dark: defaultTheme.colors['600']
        
      
    
  ,
  variants: ,
  plugins: []


这是问题所在,在我的样式表中,我有以下内容:

.navbar 
  ...
  @apply bg-primary-dark;
  ...

我的期望是这会起作用 - 但它不会。我收到一个错误(见下文)。

知道我做错了什么以及如何解决吗?

谢谢。

错误信息

> startup-reporter@1.0.0 dev C:\laragon\www\sage\wp-content\themes\startup-reporter
> npm run development


> startup-reporter@1.0.0 development C:\laragon\www\sage\wp-content\themes\startup-reporter
> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js 

98% after emitting SizeLimitsPlugin

 ERROR  Failed to compile with 2 errors  9:18:34 PM
 error  in ./src/assets/css/style.css

Syntax Error: ModuleBuildError: Module build failed (from ./node_modules/postcss-loader/src/index.js):SyntaxError

(9:3) `@apply` cannot be used with `.bg-primary-dark` because `.bg-primary-dark` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that 
`.bg-primary-dark` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

   7 | .navbar 
   8 |   width: 100%;
>  9 |   @apply bg-primary-dark;
     |   ^
  10 |   @apply flex flex-wrap relative;
  11 |   @apply font-semibold;


 error  in ./src/assets/css/style.css

Syntax Error: SyntaxError

(9:3) `@apply` cannot be used with `.bg-primary-dark` because `.bg-primary-dark` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that 
`.bg-primary-dark` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

   7 | .navbar 
   8 |   width: 100%;
>  9 |   @apply bg-primary-dark;
     |   ^
  10 |   @apply flex flex-wrap relative;
  11 |   @apply font-semibold;



 @ ./src/assets/css/style.css 2:14-150


ERROR in ./src/assets/css/style.css
Module build failed (from ./node_modules/css-loader/index.js):
ModuleBuildError: Module build failed (from ./node_modules/postcss-loader/src/index.js):
SyntaxError

(9:3) `@apply` cannot be used with `.bg-primary-dark` because `.bg-primary-dark` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that 
`.bg-primary-dark` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

   7 | .navbar 
   8 |   width: 100%;
>  9 |   @apply bg-primary-dark;
     |   ^
  10 |   @apply flex flex-wrap relative;
  11 |   @apply font-semibold;

    at C:\laragon\www\sage\wp-content\themes\startup-reporter\node_modules\webpack\lib\NormalModule.js:316:20
    at C:\laragon\www\sage\wp-content\themes\startup-reporter\node_modules\loader-runner\lib\LoaderRunner.js:367:11
    at C:\laragon\www\sage\wp-content\themes\startup-reporter\node_modules\loader-runner\lib\LoaderRunner.js:233:18
    at context.callback (C:\laragon\www\sage\wp-content\themes\startup-reporter\node_modules\loader-runner\lib\LoaderRunner.js:111:13)
    at C:\laragon\www\sage\wp-content\themes\startup-reporter\node_modules\postcss-loader\src\index.js:207:9
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
 @ ./src/assets/css/style.css

ERROR in ./src/assets/css/style.css (./node_modules/css-loader??ref--8-2!./node_modules/postcss-loader/src??postcss0!./src/assets/css/style.css)
Module build failed (from ./node_modules/postcss-loader/src/index.js):
SyntaxError

(9:3) `@apply` cannot be used with `.bg-primary-dark` because `.bg-primary-dark` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that 
`.bg-primary-dark` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.

   7 | .navbar 
   8 |   width: 100%;
>  9 |   @apply bg-primary-dark;
     |   ^
  10 |   @apply flex flex-wrap relative;
  11 |   @apply font-semibold;

 @ ./src/assets/css/style.css 2:14-150
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! startup-reporter@1.0.0 development: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`       
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the startup-reporter@1.0.0 development script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.    

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\moshe\AppData\Roaming\npm-cache\_logs\2020-03-08T19_18_34_238Z-debug.log        
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! startup-reporter@1.0.0 dev: `npm run development`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the startup-reporter@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.    

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\moshe\AppData\Roaming\npm-cache\_logs\2020-03-08T19_18_34_295Z-debug.log        
PS C:\laragon\www\sage\wp-content\themes\startup-reporter>

【问题讨论】:

【参考方案1】:

我有同样的问题,新版本目前是 2,我的存根文件在旧版本上运行良好;

经过调查发现,默认配置不再包含全彩,而是继承自:

const  colors  = require('tailwindcss/defaultTheme')

尝试导出整个 colors.js 文件,如:

const colors = require('tailwindcss/colors')

这对我来说效果很好。

【讨论】:

至少在 Nuxtjs 上仍然会在顺风版本 2 和 3 上显示关于颜色命名更改的警告。如何解决这个问题?

以上是关于自定义 Tailwind CSS 时引用默认颜色的主要内容,如果未能解决你的问题,请参考以下文章

NextJS - 无法在 Tailwind CSS 中使用自定义颜色

Tailwind CSS 自定义颜色应用于文本而不是 ReactJS 中的背景

不符合 Tailwind 自定义颜色

Tailwind 自定义颜色在 Angular 生产构建中不起作用

在 Tailwind 中扩展颜色

可以从 CSS 中引用顺风颜色吗?