You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在浏览器原生JavaScript中引入UnoCSS transformer-directives?

浏览器中运行UnoCSS并启用@apply指令解决方案

问题背景

已实现无构建工具在浏览器运行UnoCSS,需支持@apply指令以实现类TailwindCSS的完整功能。该功能依赖UnoCSS的transformer-directives,但未找到可行的运行时引入方案,尝试加载ESM模块失败。

当前代码

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>unocss</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css">
  <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime@0.61.2/preset-wind.global.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime@0.61.2/full.global.js"></script>
  <script>
    ocssPresets = window.__unocss_runtime.presets
    window.__unocss = {
      rules: [
        // custom rules...
      ],
      presets: [
        unocssPresets.presetWind({
          theme: {
            fontFamily: {
              sans: ['Roboto', 'Graphik', 'sans-serif'],
              serif: ['Merriweather', 'serif']
            },
            extend: {

            }
          }
        })
      ],
      theme: {
        fontFamily: {
          sans: ['Roboto', 'Graphik', 'sans-serif'],
          serif: ['Merriweather', 'serif']
        },
        colors: {
          customBlue: '#548ab7',
          customBlueLight: 'rgba(188, 211, 241, 0.2)',
          customYellow: '#fffceb',
          backgroundColor: '#f4f4f4',
        }
      },
      transformers: [
        //<----- to have this I will need a runtime to include
        //transformerDirectives(),
      ],
      // ...
    }
  </script>
  <style type="text/css">
    @tailwind base;
    @tailwind components;
    @tailwind utilities;

    section {
      @apply mb-4 p-4 bg-gray-100 rounded bg-blue-200;
      border: solid 1px blue;
    }

    .prj>h4 {
      @apply font-bold flex justify-between;
      border: solid 1px green;
    }
  </style>
</head>
<body>
   <section class="prj">test<h4>heading</h4></section>
</body>
<html>

解决步骤

  • 引入全局UMD版本的transformer-directives,而非ESM模块,确保浏览器能直接获取全局变量
  • 保持UnoCSS runtime与transformer-directives版本一致(此处统一使用0.61.2版本)
  • 在UnoCSS配置的transformers数组中添加transformerDirectives()

修改后的完整代码

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>unocss</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css">
  <!-- 引入同版本的transformer-directives全局UMD文件 -->
  <script src="https://cdn.jsdelivr.net/npm/@unocss/transformer-directives@0.61.2/dist/index.global.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime@0.61.2/preset-wind.global.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime@0.61.2/full.global.js"></script>
  <script>
    const unocssPresets = window.__unocss_runtime.presets
    window.__unocss = {
      rules: [
        // custom rules...
      ],
      presets: [
        unocssPresets.presetWind({
          theme: {
            fontFamily: {
              sans: ['Roboto', 'Graphik', 'sans-serif'],
              serif: ['Merriweather', 'serif']
            },
            extend: {

            }
          }
        })
      ],
      theme: {
        fontFamily: {
          sans: ['Roboto', 'Graphik', 'sans-serif'],
          serif: ['Merriweather', 'serif']
        },
        colors: {
          customBlue: '#548ab7',
          customBlueLight: 'rgba(188, 211, 241, 0.2)',
          customYellow: '#fffceb',
          backgroundColor: '#f4f4f4',
        }
      },
      transformers: [
        // 启用transformer-directives
        window.unocss.transformerDirectives()
      ],
      // ...
    }
  </script>
  <style type="text/css">
    @tailwind base;
    @tailwind components;
    @tailwind utilities;

    section {
      @apply mb-4 p-4 bg-gray-100 rounded bg-blue-200;
      border: solid 1px blue;
    }

    .prj>h4 {
      @apply font-bold flex justify-between;
      border: solid 1px green;
    }
  </style>
</head>
<body>
   <section class="prj">test<h4>heading</h4></section>
</body>
</html>

内容的提问来源于stack exchange,提问作者raisercostin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 10:34:56