这是我的第一个 Tailwind CSS 项目,从 CDN 开始,但我并不总是有互联网,所以我尝试使用 PostCSS 安装它,并且我使用 Vite 作为我的服务器。
关注了 CodeWithHarry 的这段视频https://www.youtube.com/watch?v=aUunolbb1xU&list=PLu0W_9lII9ahwFDuExCpPFHAK829Wto2O&index=3
我首先启动了该项目
npm init -y
Run Code Online (Sandbox Code Playgroud)
并安装了所需的软件包
npm install -D tailwind postcss autoprefixer vite
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式启动 Tailwind CSS
npx tailwindcss init -p
Run Code Online (Sandbox Code Playgroud)
我还在 input.css 文件中输入了 @tailwind 指令。
但是当我跑的时候:
npm start
Run Code Online (Sandbox Code Playgroud)
我的 vite 服务器向我打招呼时出现以下错误:
npm init -y
Run Code Online (Sandbox Code Playgroud)
我的index.html:
npm install -D tailwind postcss autoprefixer vite
Run Code Online (Sandbox Code Playgroud)
我的 tailwind.config.js:
npx tailwindcss init -p
Run Code Online (Sandbox Code Playgroud)
我的 postcss.config.js:
npm start
Run Code Online (Sandbox Code Playgroud)
我的package.json:
[plugin:vite:css] Loading PostCSS Plugin failed: Cannot find module 'tailwindcss'
Require stack:
- C:\projects\2 Shidhu\twproject\noop.js …Run Code Online (Sandbox Code Playgroud) 这是我到目前为止尝试过的:
\n通过安装npm install postcss-nesting --save-dev
设置 vite.config.js:
\nimport { fileURLToPath, URL } from 'url';\nimport { defineConfig } from 'vite';\nimport vue from '@vitejs/plugin-vue';\nimport postcssNesting from 'postcss-nesting';\n\nexport default defineConfig({\n plugins: [\n vue(),\n postcssNesting\n ],\n \n resolve: {\n alias: {\n '@': fileURLToPath(new URL('./src', import.meta.url))\n }\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n但它\xe2\x80\x99s 不起作用。设置 PostCSS 以便我可以使用 CSS 嵌套的正确方法是什么\xe2\x80\x99?
\nTailwindcss 有明确的文档说明如何为所有 tailwind 类生成前缀,以更好地将特定应用程序的样式与其上下文隔离。但是,我似乎无法找到关于如何将现有代码转换为使用前缀的直接答案。文档(https://tailwindcss.com/docs/configuration)展示了如何生成“tw-”类,但没有展示如何修改现有代码库以将此“tw-”前缀添加到代码本身。我有很多这样的类,手动检查并向现有类添加“tw-”前缀是不可行的。我错过了什么吗?如何转换现有代码库以使用生成的类前缀,而无需自己手动编辑所有 Tailwind 类?
在Sass中,我可以在一个单独的文件中定义我的变量_variables.scss,然后在任何地方使这些变量可用@import variables;.(实际上变量在首次导入后是全局可用的.)使用PostCSS,我可以使用postcss-simple-vars或postcss-css-variables定义单个文件的局部变量.我想在一个文件中定义所有/大多数变量,然后@import在我需要使用它们的任何地方定义该文件.
我知道PostCSS插件可以为插件配置预定义的变量,但是我不能为给定的背景计算对比色.
编辑:我可能不清楚我的问题是什么.有没有办法在CSS文件中定义PostCSS变量,使变量不是全局的,但可以"导入"到另一个CSS文件中?如上所述,我可以使用Sass,但是所有变量都是有效的全局变量(不理想).否则在PostCSS中我可以在每个样式表中定义我需要它们的变量(重点是什么?),或者我可以在静态JavaScript结构中定义它们(静态,因为它们不能引用其他变量).
我刚刚升级到 Gulp v4,想知道为什么我的 gulpfile 不再工作了。
我尝试了新文档的新代码,但没有按计划运行,因为我使用的是“纯”postcss。
所以我用谷歌搜索我的问题并发现了这个问题:Gulp 错误:监视任务必须是一个函数
但是,这也不是我的问题的解决方案,尽管我收到了相同的错误消息Error: watching src/styles/**/*.scss: watch task has to be a function
我目前有这个代码
var gulp = require('gulp');
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');
gulp.task('default', function () {
gulp.watch('src/styles/**/*.scss', ['styles']);
});
gulp.task('styles', function () {
var processors = [
autoprefixer({
browsers: ['last 3 versions', 'ie > 9']
}),
cssnano()
];
gulp.src('src/styles/main.scss')
.pipe(sass().on('error', sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('dist/files/styles/'));
});
Run Code Online (Sandbox Code Playgroud)
当我更改
它时
gulp.watch('src/styles/**/*.scss', ['styles']);
,
gulp.watch('src/styles/**/*.scss', gulp.series('styles')); …
我目前正在使用Rollup和PostCSS。如何定位我想要处理的特定 scss 文件,而不必使用 @import 'source/scss/main.scss' in mysource/js/entry.js`
例如做类似的事情......
// rollup.config.js
...
postcss({
from: 'source/scss/main.scss'
minimize: true,
sourceMap: true,
extract : 'build/css/main.css'
}),
...
Run Code Online (Sandbox Code Playgroud)
而不是在 source/js/entry.js
//source/js/entry.js
import '../scss/main.scss'; // don't want to import SCSS from within JS file
import { someJSmodule } from './someJSmodule.js';
import { anotherJSmodule } from './anotherJSmodule.js';
Run Code Online (Sandbox Code Playgroud)
这是我的当前 rollup.config.js
// rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import copy from 'rollup-plugin-copy';
import postcss from 'rollup-plugin-postcss';
export …Run Code Online (Sandbox Code Playgroud) 在 svelte 中使用postcss和插件postcss-nesting非常简单——添加postcss-nesting到svelte.config.jspreprocess中的配置是唯一需要的:
import preprocess from "svelte-preprocess";
import postcssNesting from "postcss-nesting";
const config = {
preprocess: preprocess({
postcss: {
plugins: [
postcssNesting()
]
}
}),
// ...
};
export default config;
Run Code Online (Sandbox Code Playgroud)
现在我可以使用“&”语法定义嵌套 CSS。
例如,当我在example.svelte文件中有此内容时:
<div class="example">
One <span class="nested">two</span> three
</div>
<style lang="postcss">
.example {
color: red;
& .nested {
text-decoration: underline;
}
}
</style>
Run Code Online (Sandbox Code Playgroud)
但是,我无法找到eslint接受它的方法。它报告在 & 字符之后需要一个标识符。
*以下问题与我之前提出的问题相关,因此如果我重复,请提前道歉,但我仍然无法解决我的问题。
\n我正在尝试让 Storybook 与 Tailwind CSS 一起使用,但到目前为止没有成功。这些是我遵循的步骤:
\nmodule.exports = {\n "stories": [\n "../stories/**/*.stories.mdx",\n "../stories/**/*.stories.@(js|jsx|ts|tsx)"\n ],\n "addons": [\n "@storybook/addon-links",\n "@storybook/addon-essentials",\n "@storybook/addon-postcss"\n ],\n "framework": "@storybook/react"\n}\nRun Code Online (Sandbox Code Playgroud)\n尽管做了所有这些,我没有看到 Tailwind 对应用程序中的故事组件\xe2\x80\x94 产生任何影响。
\n我尝试通过在 Storybook 组件中放入一个小元素来测试 Tailwind 是否有效。我没有看到它按预期呈现:
\n<h1 className="text-3xl font-bold underline">\n Tailwind Works!\n</h1>\nRun Code Online (Sandbox Code Playgroud)\n链接到 Github 存储库: …
我在带有 Webpack 的 Angular 项目中使用 CSS 模块。我已经用postcss-modules转换了.css和.scss文件中的类名。
然后使用posthtml-css-modules我更改了 html 元素中 class 属性的值,用于由 postcss-modules.
我可以说一切正常。
现在,我有一个新的挑战要解决。
Angular 有一项功能,允许您class 根据条件动态更改html 元素中的值:
https://angular.io/api/common/NgClass
例如,我可以这样做:
<div [className]="myVar ? 'myClass1' : 'myClass2' " >
如果myVar = true,则 html 元素将是:
<div class="myClass1" >
如果myVar = false,则 html 元素将是:
<div class="myClass2" >
就像我不知道myVar在编译期间的值是什么(因为 的值myVar取决于用户操作)我无法设置值<div css-module="myClass1" > 或<div css-module="myClass2" >为了散列myClass1or的类名myClass2。
但是(这是我的解决方案)...
如果我可以调用相同的函数[hash:base64:5] …
我使用postcss、postcss-css-modules和posthtml-css-modules在 Angular 应用程序中实现 CSS 模块。我还使用了@angular-builders/custom-webpack来实现这一点。
现在,我想对我的自定义 Angular 库做同样的事情。但是,我不能使用@angular-builders/custom-webpack,因为 Angular 库是使用ng-packagr构建的。
所以,@angular-builders/custom-webpack不能使用:https : //github.com/just-jeb/angular-builders/issues/356
另一方面,ng-packagr不支持postcss:https : //github.com/ng-packagr/ng-packagr/issues/1471
我已阅读,它可以延长汇总配置(就是使用编译器NG-packagr在构建结束)在NG-packagr:
https://github.com/ng-packagr/ng-packagr/blob/master/docs/DESIGN.md#rollup-config
但我没有找到任何文档来实现这一点。
有人知道怎么做吗?
我认为的其他解决方案,它使所有样式全局化并使用scss-bundle和 postcss编译它们,就像我在这里所做的那样:NodeJs Script that compiles scss files failed because of postcss rule for undefined variables
如果我可以使用lodash,我将能够用散列类名替换类名,就像这里建议的那样:在 JavaScript / TypeScript 文件中使用 [hash:base64:5]
但要做到这一点,我需要知道如何在ng-packagr的构建中调用lodash。
有人知道怎么做吗?
任何其他解决方案都非常受欢迎。 …
postcss ×10
tailwind-css ×3
angular ×2
npm ×2
vite ×2
autoprefixer ×1
css ×1
css-loader ×1
css-modules ×1
eslint ×1
gulp ×1
javascript ×1
next.js ×1
ng-packagr ×1
reactjs ×1
rollup ×1
rollupjs ×1
storybook ×1
svelte ×1
typescript ×1
vue.js ×1
vuejs3 ×1
webpack ×1