Tailwind PostCSS内联注入样式,而不是生成CSS文件



我使用PUG+Tailwind+PostCSS作为静态单页HTML文件,该文件是使用基本NPM脚本生成的(如下所示(

我希望最终的HTML将顺风样式直接内联在index.html页面中,而不是作为.css文件生成,后者在index.html文件中引用。

不使用外部文件:

link(rel="stylesheet" href="styles.css")

我想要这个:

<style>
// all my (tailwind) CSS here
</style>

显然,注入必须在Pug编译之后进行,也编译了顺风。

我的问题是,是否有Tailwind插件或PostCSS cliplugin/setting

我找了几个小时都没找到。

以下是我的NPM脚本:

"start": "npm run build && npm run css",
"build": "pug -O src/data/index.js -P -w src/html/index.pug -o dist",
"css": "postcss -o ./dist/styles.css ./src/css/*.css --watch",

您可以始终禁用css"清洁";,独立于pug编译css,并使用webpack将它们链接在一起。

请参阅此问题和此webpack插件。

希望这一次能有所帮助。至少它会起作用。

您只需在执行任何构建命令后将css文件注入到html中,脚本如下:

#!/bin/bash
# inject_css.sh
cp index.html output.html
echo "<style>" >> output.html
cat dist/styles.css >> output.html
echo "</style>" >> output.html

然后,在NPM脚本中的构建规则之后添加此脚本,如下所示:

"start": "npm run build && npm run css && ./inject_css.sh"

>>将文本追加到文件中。在这种情况下,我们添加一个打开的样式标记,然后连接output.css,最后关闭样式标记,您应该将所有编译的css代码直接放在HTML的样式标记中。

您可以使用Play CDN

使用Play CDN在浏览器中尝试Tailwind,无需任何构建步骤。Play CDN仅用于开发目的,并非生产的最佳选择。

<!doctype html>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script> 👈 Inject it here
</head>
<body>
<h1 class="text-3xl font-bold underline">  
Hello world!
</h1>
</body>
</html>

最新更新