更新2021-09-08: TL; Tailwind博士没有应用于HAML模板。
将一个项目迁移到shakapacker,后者在幕后使用webpack@v6和postcss。除了TailwindCSS之外,所有其他前端CSS资产似乎都在正确加载。
为了简化调试,我从主文件中删除了除Tailwind之外的所有其他CSS导入。我还创建了下面的测试HTML页面,以查看是否应用了Tailwind CSS样式。
test.html
...
<div class="max-w-sm rounded overflow-hidden shadow-lg">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">
The Coldest Sunset
</div>
<p class="text-gray-700 text-base">
Lorem ipsum
</p>
</div>
</div>
...当我看到这个页面,它看起来很简单,只是文本-没有阴影,填充,字体权重等应用。当我查看生成的CSS输出时,似乎只加载了Tailwind的重置样式,而没有其他任何内容。
您可以在这里看到输出:http://codebin.org/view/bba03ee2。
更新2022年8月11日
正如下面的注释所建议的那样,我运行了yarn run tailwindcss -i ./app/assets/packs/styles/application.scss -o ./output.css,它产生了以下错误:
.../app/assets/packs/styles/application.scss:4:1: Unknown word
You tried to parse SCSS with the standard CSS parser; try again with the postcss-scss parser这个错误指向了第一个注释行:// @tailwind "base";,这很有意义,因为// double forward-slash comments是一个SASS/SCSS特性,而不是标准的CSS。
但是,我认为包括sass和sass-loader将根据shakapacker安装说明来处理。
正因为如此,我在webpack.config.js中显式地指定了加载器(参见下面),并且错误仍在发生。
我已经把所有相关文件都列在下面了。
配置文件
application.scss (导入所有CSS资产):
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
// @tailwind "base";
// @tailwind "components";
// @tailwind "utilities";使用@import或@tailwind会产生相同的结果。
application.js (进口application.scss)
我只包括了相关代码行。
...
import "./styles/application.scss"
...packages.json
{
"name": "app",
"private": true,
"dependencies": {
"@babel/core": "7",
"@babel/plugin-transform-runtime": "7",
"@babel/preset-env": "7",
"@babel/runtime": "7",
"@rails/actioncable": "^7.0.3-1",
"@rails/activestorage": "^7.0.3-1",
"@rails/ujs": "^7.0.3-1",
"babel-loader": "8",
"coffee-loader": "^4.0.0",
"coffeescript": "^2.7.0",
"compression-webpack-plugin": "9",
"jquery": "^3.6.0",
"jquery-ujs": "^1.2.3",
"moment": "^2.29.4",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-loader": "^7.0.1",
"postcss-preset-env": "^7.7.2",
"select2": "^4.1.0-rc.0",
"shakapacker": "6.5.0",
"tailwindcss": "^3.1.8",
"terser-webpack-plugin": "5",
"tributejs": "^5.1.3",
"turbolinks": "^5.2.0",
"webpack": "5",
"webpack-assets-manifest": "5",
"webpack-cli": "4",
"webpack-dev-server": "^4.9.3",
"webpack-merge": "5"
},
"version": "0.1.0",
"babel": {
"presets": [
"./node_modules/shakapacker/package/babel/preset.js"
]
},
"browserslist": [
"defaults"
],
"devDependencies": {
"autoprefixer": "^10.4.8",
"css-loader": "^6.7.1",
"mini-css-extract-plugin": "^2.6.1",
"postcss": "^8.4.16",
"sass": "^1.54.3",
"sass-loader": "^13.0.2",
"style-loader": "^3.3.1",
"tailwindcss": "^3.1.8"
}
}webpack.config.js
const { webpackConfig, merge } = require('shakapacker')
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
const sassLoaderConfig = {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader",
],
},
],
},
};
const options = {
resolve: {
extensions: ['.mjs', '.js', '.sass', ".scss", ".css", ".module.sass", ".module.scss", ".module.css", ".png", ".svg", ".gif", ".jpeg", ".jpg"]
}
}
module.exports = merge(webpackConfig, sassLoaderConfig, options);postcss.config.js
module.exports = {
plugins: [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009'
},
stage: 3
})
]
}tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./app/**/*.html',
'./app/**/*.html.slim',
'./app/**/*.html.erb',
'./app/**/*.js'
],
theme: {
extend: {},
},
plugins: [],
}webpacker.yml
# Note: You must restart bin/webpacker-dev-server for changes to take effect
default: &default
source_path: app/assets
# You can have a subdirectory of the source_path, like 'packs' (recommended).
# Alternatively, you can use '/' to use the whole source_path directory.
source_entry_path: /packs
# If nested_entries is true, then we'll pick up subdirectories within the source_entry_path.
# You cannot set this option to true if you set source_entry_path to '/'
nested_entries: true
public_root_path: public
public_output_path: packs
cache_path: tmp/webpacker
webpack_compile_output: true
# See https://github.com/shakacode/shakapacker#deployment
webpacker_precompile: true
# Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
# manifest_path: public/packs/manifest.json
# Additional paths webpack should look up modules
# ['app/assets', 'engine/foo/app/assets']
additional_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Select loader to use, available options are 'babel' (default), 'swc' or 'esbuild'
webpack_loader: 'babel'
# Set to true to enable check for matching versions of shakapacker gem and NPM package - will raise an error if there is a mismatch or wildcard versioning is used
ensure_consistent_versioning: false
# Select whether the compiler will use SHA digest ('digest' option) or most most recent modified timestamp ('mtime') to determine freshness
compiler_strategy: digest
extract_css: true
development:
<<: *default
compile: true
compiler_strategy: mtime
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
# Hot Module Replacement updates modules while the application is running without a full reload
hmr: false
# If HMR is on, CSS will by inlined by delivering it as part of the script payload via style-loader. Be sure
# that you add style-loader to your project dependencies.
#
# If you want to instead deliver CSS via <link> with the mini-extract-css-plugin, set inline_css to false.
# In that case, style-loader is not needed as a dependency.
#
# mini-extract-css-plugin is a required dependency in both cases.
inline_css: true
# Defaults to the inverse of hmr. Uncomment to manually set this.
# live_reload: true
client:
# Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
overlay: true
# May also be a string
# webSocketURL:
# hostname: "0.0.0.0"
# pathname: "/ws"
# port: 8080
# Should we use gzip compression?
compress: true
# Note that apps that do not check the host are vulnerable to DNS rebinding attacks
allowed_hosts: "all"
pretty: true
headers:
'Access-Control-Allow-Origin': '*'
static:
watch:
ignored: '**/node_modules/**'
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Cache manifest.json for performance
cache_manifest: true发布于 2022-09-08 15:50:42
所以,在到处搜寻之后,罪魁祸首就在tailwind.config.js文件中。我没有提到我使用的是HAML模板,我忘了在顺风配置中添加*.haml扩展。
我将更新原来的问题,以便将HAML作为问题的一部分。
解决办法如下。
const plugin = require('tailwindcss/plugin')
module.exports = {
content: [
...
'./app/views/**/*.html.haml',
'./app/views/**/*.haml',
...
],
plugins: [
...
]
}发布于 2022-08-18 06:07:17
我所能做的就是解释我所知道的,也许有什么东西会把你推向正确的方向。
$ rails _6.1.6.1_ new shaka --skip-javascript
$ cd shaka
# https://github.com/shakacode/shakapacker#installation
$ bin/bundle add shakapacker
$ bin/rails webpacker:install
# https://tailwindcss.com/docs/installation
$ bin/yarn add tailwindcss
$ bin/yarn tailwindcss init
$ bin/rails g controller Home index更新几个文件,这样我们就可以测试所有内容:
// app/views/home/index.html.erb
<h1 class="text-red-500">Am I red?</h1>
// app/views/layouts/application.html.erb
// only need the asset pipline, for now
<%= stylesheet_link_tag "tailwind.bundle" %>
// tailwind.config.js
// update content to include views
content: [ "./app/views/**/*.html.erb" ],
// alternatively, since we need all the views anyway, just get them all,
// it'll grab a few unneeded files, like .jbuilder, no big deal:
// content: [ "./app/views/**/*" ],
// app/javascript/styles/tailwind.css
@tailwind utilities;现在,tailwindcss还没有集成到shakapacker、webpack或sass中。我们可以运行为什么风命令来编译我们的样式,并让链轮服务他们。
$ bin/yarn tailwindcss -i ./app/javascript/styles/tailwind.css -o ./app/assets/stylesheets/tailwind.bundle.css --watch
# start the server and open http://localhost:3000/home/index
$ bin/rails s
# you should see a red title您也可以使用@import其他文件(内部使用postcss-import):
// app/javascript/styles/tailwind.css
@import "./imported.css";
@tailwind utilities;
// app/javascript/styles/imported.css
h1 { background-color: yellow; }这就是尾风车自己所做的事情(v3.1.8)。其他功能是通过重写postcss配置来添加的。
https://tailwindcss.com/docs/using-with-preprocessors#using-post-css-as-your-preprocessor
// postcss.config.js
module.exports = {
plugins: {
"postcss-import": {}, // process @import
"tailwindcss/nesting": {}, // add sass like nesting
tailwindcss: {}, // then let tailwind do its thing
}
}// app/javascript/styles/imported.css
body {
h1 {
background-color: green;
}
}使用--postcss标志覆盖postcss配置:
$ bin/yarn tailwindcss --postcss postcss.config.js -i ./app/javascript/styles/tailwind.css -o ./app/assets/stylesheets/tailwind.bundle.css --watch顺便说一句,这就是cssbundling rails的方法。如果您需要通过webpack导入css,则需要更改一些内容。
// app/javascript/application.js
import "./styles/tailwind.css"如果您尝试运行bin/webpack-dev-server,您将得到:
ERROR in ./app/javascript/styles/tailwind.css 1:0
Module parse failed: Unexpected character '@' (1:0)Webpack不知道如何处理css。添加css加载程序:
$ bin/yarn add css-loader mini-css-extract-plugin将样式表包添加到布局中:
# app/views/layouts/application.html.erb
# don't need this
# stylesheet_link_tag "application", "tailwind.bundle", media: "all"
<%= javascript_pack_tag "application" %>
<%= stylesheet_pack_tag "application" %>启动bin/webpack-dev-server,您应该得到相同的结果--红/绿标题。如果您查看生成的css文件:
/*!****************************************************************************************************************************************************************************************!*\
!*** css ./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[2].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[2].use[2]!./app/javascript/styles/tailwind.css ***!
\****************************************************************************************************************************************************************************************/
body h1 { background-color: green; }
.inline { display: inline; }
.text-red-500 { --tw-text-opacity: 1; color: rgb(239 68 68 / var(--tw-text-opacity)); }
/*# sourceMappingURL=application.css.map*/顶部的评论显示,tailwind.css由postcss-loader处理,然后由css-loader处理。
https://webpack.js.org/concepts/loaders#configuration
我们知道我们的postcss配置是有效的,它有尾翼连接,尾部/嵌套和后置-导入。所以你得到了同样的结果。
如果你不得不使用sass,只要把它打在上面,一切都应该仍然有效。首先,将每个css重命名为scss。您应该从webpack那里得到一个错误,因为它不知道如何处理scss。所以我们需要sass装载机
$ bin/yarn add sass-loader sassSass做@导入和嵌套,因此不再需要这些:
"postcss-import": {},
"tailwindcss/nesting": {},在sass完成之后,只剩下处理@tailwind、@layer、@apply和其他顺风指令了。
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
}
}这就是我所知道的一切。而且,我根本没有碰过webpack的任何吐露,沙卡柏做了很多自动。我试过使用assets/packs目录,结果是一样的。注意进口的顺序:
https://tailwindcss.com/docs/using-with-preprocessors#build-time-imports
我不知道谁在这个堆栈中做导入,应该是sass。不过,css-加载程序也知道如何@导入。如果您离开postcss-import,它也知道如何@import。我敢打赌他们每个人的做法都不一样。
我跳过的东西:autoprefixer,style-loader,css-minimizer-webpack-plugin。
https://stackoverflow.com/questions/73311322
复制相似问题