Shopify Hydrogen – 设置 tailwind

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

Shopify Hydrogen - setting up tailwind

问题

在本地开发中,我正在尝试在Shopify Hydrogen React项目(Remix)中设置Tailwind。

当前的Remix中使用了postCSS来集成Tailwind。已添加并配置了postcss-import。

postcss.config.js...

module.exports = {
  plugins: {
    'postcss-import': {},
    'tailwindcss/nesting': {},
    tailwindcss: {},
    'postcss-preset-env': {
      features: {'nesting-rules': false},
    },
  },
};

在我的App.css中,我有以下内容:

@tailwind base;
@import 'custom/base.css';

@tailwind components;
@import 'custom/components.css';

@tailwind utilities;
@import 'custom/utilities.css';

但问题是,尽管@Tailwind部分正常工作,但自定义的导入却不起作用。

因此,如果我将以下内容放在app.css中...

@base {
 /*在这里添加样式*/
}

...那么它将正常工作,但在任何@imported文件中都不起作用。

理想情况下,我希望能够拆分我的文件,而不是有一个庞大的单一app.css。

但如果我将这个@base {}代码放入另一个文件并导入它,它就不起作用。导入会悄悄失败。

组件和实用程序也有相同的问题。

remix.config.js...

...
future: {
    unstable_postcss: true,
    unstable_tailwind: true,
    v2_meta: true,
    v2_routeConvention: true,
    v2_errorBoundary: true,
    v2_normalizeFormMethod: true,
  },
...

有什么想法吗?谢谢。

更新

多亏了@stickyuser,现在问题清楚了...

因为它是PostCSS,导入需要放在最前面。但大多数Tailwind文档告诉你将base、components和utilities引用放在顶部(这样你不能只将你的覆盖放在下面,例如"@tailwind base",因为如果你首先有覆盖,你就不能覆盖,这会导致未知的结果)。所以你只需要将例如@tailwind base改成下面的导入语句。

这意味着Tailwind声明应该如下...

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

然后,你可以添加所有你的层覆盖...

/* 根或重置样式 */
@import 'root/root.css';

/* 基础层覆盖 */
@import 'base/base.css';
@import 'base/headers.css';

/* 组件层覆盖 */
@import 'components/components.css';
@import 'components/images.css';
@import 'components/buttons.css';

/* 实用程序层覆盖 */
@import 'utilities/utilities.css';

这也在官方的Tailwind文档中有解释,尽管需要一些知识才能理解。

https://tailwindcss.com/docs/using-with-preprocessors

最终的结果是,文件可以拆分,内部包含例如@layer base {},使项目更易管理。层次结构很重要,因为这将告诉Tailwind应用类的顺序。

英文:

I am trying to set up Tailwind in a Shopify Hydrogen React project (Remix) on local dev.

Tailwind in the current Remix is by way of postCSS.
Postcss-import is added and configured.

postcss.config.js...

module.exports = {
  plugins: {
    'postcss-import': {},
    'tailwindcss/nesting': {},
    tailwindcss: {},
    'postcss-preset-env': {
      features: {'nesting-rules': false},
    },
  },
};

In my App.css I have

@tailwind base;
@import 'custom/base.css';

@tailwind components;
@import 'custom/components.css';

@tailwind utilities;
@import 'custom/utilities.css';

But the problem ... while the @Tailwind includes work fine, the custom imports are not working.

So if I place the following within app.css...

@base {
 /*styles goes here*/
}

...then it will work fine but not in any @imported files.

Ideally I want to be able to split my files up rather than have one huge single app.css.

But if I place this @base {} code into another file and import it, it does not work. The import fails silently.

Same problem with components / utilities.

remix.config.js...

...
 future: {
    unstable_postcss: true,
    unstable_tailwind: true,
    v2_meta: true,
    v2_routeConvention: true,
    v2_errorBoundary: true,
    v2_normalizeFormMethod: true,
  },
...

Any ideas? Thanks.

UPDATE

Thanks to @stickyuser it's now clear what the problem is...

Because it's PostCSS, imports need to come first.
But most of the Tailwind documents tells you to put the base, components and utilities references at the top (so you can't just put your overrides below e.g. "@tailwind base" because you wouldn't be overriding if you had the overrides first and you would be inviting unknown results).
So you just need to change the e.g. @tailwind base to the import statement below.
This means the Tailwind declarations should look like this...

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

...then, you can add all your layer overrides...

/* Root or reset styles */
@import 'root/root.css';

/* Base layer overrides */
@import 'base/base.css';
@import 'base/headers.css';

/* Component layer overrides */
@import 'components/components.css';
@import 'components/images.css';
@import 'components/buttons.css';

/* Utility layer overrides */
@import 'utilities/utilities.css';

It's also explained in the official Tailwind documents, although requires some knowledge for the penny to drop.

https://tailwindcss.com/docs/using-with-preprocessors

The end result is that files can be split and inside contain the e.g. @layer base {} making the project more manageable. The layers are important to respect because this will indicate to Tailwind the order in which classes should be applied.

答案1

得分: 2

所有的 @import 声明都需要放在任何其他内容之前。

参考:https://www.npmjs.com/package/postcss-import

参见:“此插件尝试遵循CSS @import规范;@import声明必须在所有其他声明之前”。

注意:看起来你将能够混合使用 @tailwind@import 规则,但目前尚未发布:https://github.com/tailwindlabs/tailwindcss/pull/11239

英文:

All the @import statements need to go before anything else.

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

See: "This plugin attempts to follow the CSS @import spec; @import statements must precede all other statements"

NOTE: It looks like you will be able to mix @tailwind and @import rules but it is currently unreleased: https://github.com/tailwindlabs/tailwindcss/pull/11239

huangapple
  • 本文由 发表于 2023年6月8日 18:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430702.html
匿名

发表评论

匿名网友

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

确定