Code Monkey home page Code Monkey logo

taro-plugin-tailwind's Introduction

taro-plugin-tailwind

Taro 接入 tailwindcss 插件,支持大多数小程序(已测试微信、支付宝、百度小程序) / H5,React Native 暂未测试。

安装

在 Taro 项目根目录下安装

$ npm i taro-plugin-tailwind --save-dev
$ # 或使用 yarn
$ yarn add -D taro-plugin-tailwind

使用

引入插件

请确保 Taro CLI 已升级至 Taro 3 的最新版本(3.5+),确保 taro-plugin-tailwind 版本在 v1.3.0 及以上。

修改项目 config/index.js 中的 plugins 配置如下:

const config = {
    /// ...
    plugins: [
        // ...其余插件
        'taro-plugin-tailwind',
    ],
    /// ...
    /// 亦或是传入具体参数:
    plugins: [
        // ...其余插件
        ['taro-plugin-tailwind', {
            // 具体参数为 tailwind postcss 配置项,见:https://github.com/tailwindlabs/tailwindcss/blob/master/types/config.d.ts#L352
        }]
    ],
};

生成配置

执行 taro tailwind --init 生成必要的配置文件:

$ taro tailwind --init // 默认生成 mini, h5 两种配置文件且必须存在
$ taro tailwind --init weapp,tt,swan // 生成其它平台以 (,) 分隔

在项目主 CSS 文件(如 style.css / main.css)引入 tailwindcss

@tailwind base;
@tailwind components;
@tailwind utilities;

由此即可在项目任意位置使用 tailwindcss,并不再需要手动引入其它文件。

参数

插件接受如下参数:

参数项 类型 是否可选 用途
config Object tailwindcss 的 PostCSS 可选配置(Config)。

注意事项及限制

小程序

小程序不支持使用反斜杠和冒号作为类名,因此默认配置文件 mini.config.js 中,冒号、反斜杠 修改成使用下划线 _(参考 taro-tailwind

<View className="w-1/3"></View>

应该写成:

<View className="w-1_3"></View>

配置文件中 separator 也设置为 _configuration#separator)并且 preflight 选项应该始终保持关闭,不加载 modern-normalize

此外,部分小程序平台由于所支持的选择器非常有限(如微信小程序,详见 WXSS | 微信开放文档),不能使用 tailwindcss 的某些特性(如 Attributify Modespace-* 等),并非插件问题,有此类需求可考虑使用其它优秀方案:mini-program-tailwind

常见问题

Q:为什么 Intellisense 失效了?

A:根据所使用的版本选择安装 Tailwind CSS Intellisense

Q:保存后新样式没有生效?

A:请暂时设置 NODE_ENVproduction 解决此问题,例:NODE_ENV=production npm run build:weapp -- --watch,具体参见示例

taro-plugin-tailwind's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar jimmylv avatar pcdotfan avatar zhuchentong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

taro-plugin-tailwind's Issues

MODULE_NOT_FOUND

taro tailwind --init

node:internal/modules/cjs/loader:924
  const err = new Error(message);
              ^

Error: Cannot find module './dist/index.js'

Info:
👽 Taro v3.2.1
[email protected]

小程序需要重新 build 才能生效, h5 报错

根据文档安装了插件,发现如下问题:

  1. 安装插件后,小程序运行正常,但是开发模式,实时预览是不生效的。需要在命令行重新运行 npm run dev:weapp
  2. 本来是想, h5 实时预览会正常,运行 npm run dev:h5 会直接报错
🙅   Failed to compile.

  Error: Child compilation failed:
  Module parse failed: Unexpected token (1:0)
  File was processed with these loaders:
   * ./node_modules/html-webpack-plugin/lib/loader.js
   * ./node_modules/windicss-webpack-plugin/dist/loaders/transform-template.js
  You may need an additional loader to handle the result of these loaders.
  > <!DOCTYPE html>
  | <html>
  | <head>:
  SyntaxError: Unexpected token (1:0)

最新版的有问题,编译出的css 中带有星号通配符

我在package.json里是这样声明这个依赖的:^1.1.7,所以在最新的安装中,npm 安装了 1.2.8 版本的 taro-plugin-tailwind,然后构建后发现 app.wxss 中包含星号通配符,导致开发者工具报错。

然后我就固定了版本为 1.1.7,再次构建后,这个问题就没有了。

使用 Vue 中的 transition 组件的时候编译错误

如题,当使用 Vue 的 transition 组件的时候,transition 组件被识别成了一个 class ? 里面还自动关联了一些 CSS3 transition 的内容?盲猜是不是词法解析的问题?

image

wxss 中多出了如下代码:

image

最终导致编译通不过:

image
麻烦大佬帮忙看一下呢,感谢!

版本信息
"@tarojs/taro": "3.3.15"
"vue": "2.6.14"
"taro-plugin-tailwind": "1.2.3"

Taro3.5.3 使用webpack5编译报错

Taro3.5.3 使用webpack5编译报错, 使用React(v18)框架,taro-plugin-tailwind最新版本, 报错如下:

1661177445(1)

请问这个问题要怎么解决呢?

小数点单位的类名运行报错

你好,刚发现类名无法使用小数点。
使用小数点后小程序会报错:

index.js:

image

编译后的 app.wxss:

image

报错信息:

image

Info:
👽 Taro v3.1.4
"taro-plugin-tailwind": "^1.1.3"

执行 taro tailwind --init 生成配置文件报错

1. config/index.js

const config = {
  projectName: "mini",
  date: "2022-4-16",
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2,
  },
  sourceRoot: "src",
  outputRoot: "dist",
  plugins: ['taro-plugin-tailwind'],
  defineConstants: {},
  copy: {
    patterns: [],
    options: {},
  },
  framework: "react",
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {},
      },
      url: {
        enable: true,
        config: {
          limit: 1024, // 设定转换尺寸上限
        },
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: "module", // 转换模式,取值为 global/module
          generateScopedName: "[name]__[local]___[hash:base64:5]",
        },
      },
    },
  },
  h5: {
    publicPath: "/",
    staticDirectory: "static",
    postcss: {
      autoprefixer: {
        enable: true,
        config: {},
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: "module", // 转换模式,取值为 global/module
          generateScopedName: "[name]__[local]___[hash:base64:5]",
        },
      },
    },
  },
};

module.exports = function (merge) {
  if (process.env.NODE_ENV === "development") {
    return merge({}, config, require("./dev"));
  }
  return merge({}, config, require("./prod"));
};

2. package.json

{
  "name": "mini",
  "version": "1.0.0",
  "private": true,
  "description": "mini",
  "templateInfo": {
    "name": "taro-hooks",
    "typescript": true,
    "css": "sass"
  },
  "scripts": {
    "build:weapp": "taro build --type weapp",
    "build:swan": "taro build --type swan",
    "build:alipay": "taro build --type alipay",
    "build:tt": "taro build --type tt",
    "build:h5": "taro build --type h5",
    "build:rn": "taro build --type rn",
    "build:qq": "taro build --type qq",
    "build:jd": "taro build --type jd",
    "build:quickapp": "taro build --type quickapp",
    "dev:weapp": "npm run build:weapp -- --watch",
    "dev:swan": "npm run build:swan -- --watch",
    "dev:alipay": "npm run build:alipay -- --watch",
    "dev:tt": "npm run build:tt -- --watch",
    "dev:h5": "npm run build:h5 -- --watch",
    "dev:rn": "npm run build:rn -- --watch",
    "dev:qq": "npm run build:qq -- --watch",
    "dev:jd": "npm run build:jd -- --watch",
    "dev:quickapp": "npm run build:quickapp -- --watch"
  },
  "browserslist": [
    "last 3 versions",
    "Android >= 4.1",
    "ios >= 8"
  ],
  "author": "",
  "dependencies": {
    "@babel/runtime": "^7.7.7",
    "@tarojs/cli": "3.4.6",
    "@tarojs/components": "3.4.6",
    "@tarojs/plugin-framework-react": "3.4.6",
    "@tarojs/react": "3.4.6",
    "@tarojs/runtime": "3.4.6",
    "@tarojs/taro": "3.4.6",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "taro-hooks": "latest"
  },
  "devDependencies": {
    "@babel/core": "^7.8.0",
    "@tarojs/mini-runner": "3.4.6",
    "@tarojs/webpack-runner": "3.4.6",
    "@types/react": "^17.0.2",
    "@types/webpack-env": "^1.13.6",
    "@typescript-eslint/eslint-plugin": "^4.15.1",
    "@typescript-eslint/parser": "^4.15.1",
    "babel-plugin-import": "^1.13.3",
    "babel-preset-taro": "3.4.6",
    "eslint": "^6.8.0",
    "eslint-config-taro": "3.4.6",
    "eslint-plugin-import": "^2.12.0",
    "eslint-plugin-react": "^7.8.2",
    "eslint-plugin-react-hooks": "^4.2.0",
    "stylelint": "9.3.0",
    "taro-plugin-tailwind": "^1.2.8",
    "typescript": "^4.2.3"
  },
  "engines": {
    "node": ">=12.0.0"
  }
}

3. 终端命令

▶ taro tailwind --init
👽 Taro v3.4.6

/Users/apple/Desktop/mini/node_modules/taro-plugin-tailwind/dist/chain.js:11
    ctx.onBuildStart(() => {
        ^

TypeError: ctx.onBuildStart is not a function
    at exports.default (/Users/apple/Desktop/mini/node_modules/taro-plugin-tailwind/src/chain.ts:23:9)
    at exports.default (/Users/apple/Desktop/mini/node_modules/taro-plugin-tailwind/src/index.ts:8:23)
    at Kernel.initPlugin (/usr/local/lib/node_modules/@tarojs/cli/node_modules/@tarojs/service/dist/Kernel.js:111:16)
    at Kernel.resolvePlugins (/usr/local/lib/node_modules/@tarojs/cli/node_modules/@tarojs/service/dist/Kernel.js:86:18)
    at Kernel.initPresetsAndPlugins (/usr/local/lib/node_modules/@tarojs/cli/node_modules/@tarojs/service/dist/Kernel.js:74:14)
    at Kernel.<anonymous> (/usr/local/lib/node_modules/@tarojs/cli/node_modules/@tarojs/service/dist/Kernel.js:261:18)
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/@tarojs/cli/node_modules/@tarojs/service/dist/Kernel.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/local/lib/node_modules/@tarojs/cli/node_modules/@tarojs/service/dist/Kernel.js:4:12)

~/Desktop/mini  master ✗  

想问一下是依赖版本问题吗?我这边按照README文件的步骤来的,但是无法生成配置文件

webstorm提示

请问怎么才能添加webstorm的智能提示呢?webstorm插件已经安装了

升级到 NutUI-3.1.x 之后,全局css样式不生效

这些样式虽然可以在 virtual:windi.css 文件中找到,但是并不会对页面元素生效。

/* windicss layer base */
*, ::before, ::after {
  box-sizing: border-box;
  border-width: 0;
  border-style: solid;
  border-color: #e5e7eb;
}

* {
  --tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
  --tw-ring-offset-width: 0px;
  --tw-ring-offset-color: #fff;
  --tw-ring-color: rgba(59, 130, 246, 0.5);
  --tw-ring-offset-shadow: 0 0 #0000;
  --tw-ring-shadow: 0 0 #0000;
  --tw-shadow: 0 0 #0000;
}

之前用 taro-3.2.x 版本的时候是正常的。

Taro v3.6.8 taro tailwind --init 失败

👽 Taro v3.6.8

Error: Cannot find module './dist/index.js'
Require stack:

  • D:\WWW\jz.test\app\tt\node_modules\taro-plugin-tailwind\index.js
  • D:\WWW\jz.test\app\tt\node_modules@tarojs\service\dist\utils\index.js
  • D:\WWW\jz.test\app\tt\node_modules@tarojs\service\dist\Kernel.js
  • D:\WWW\jz.test\app\tt\node_modules@tarojs\service\dist\index.js
  • D:\WWW\jz.test\app\tt\node_modules@tarojs\service\index.js
  • D:\WWW\jz.test\app\tt\node_modules@tarojs\cli\dist\cli.js
  • D:\WWW\jz.test\app\tt\node_modules@tarojs\cli\bin\taro
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1028:15)
    at Function.Module._load (node:internal/modules/cjs/loader:873:27)
    at Module.require (node:internal/modules/cjs/loader:1100:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object. (D:\WWW\jz.test\app\tt\node_modules\taro-plugin-tailwind\index.js:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Object.newLoader [as .js] (D:\WWW\jz.test\app\tt\node_modules\pirates\lib\index.js:121:7)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12) {
    code: 'MODULE_NOT_FOUND',

NODE:v16.20.1
平台:windows

taro3.0.18尝试失败

你好,我在taro项目中尝试了这个插件 但是编译后tailwind的样式并没有生效,看到编译后的app.wxss并没有包含tailwind的类。所以怀疑tailwindcss没有被处理打包。具体操作步骤如下:
1.npm install taro-plugin-tailwind --save-dev
2.引入插件
3.taro tailwind --init生成配置文件
4.src中创建tailwind.src.css文件
5.参数配置(这一步没做!!!);
6.在组件的classname中添加tailwindcss的类;
7.npm run build:weapp
除了参数配置之外都与README一致,也不清楚启动这个插件还需要什么其他配置。麻烦指导一下@pcdotfan, 感谢!

webpack-chain里plugin是undefined是为什么?

this.store Map(3) {
'init' => [Function (anonymous)],
'plugin' => undefined,
'args' => [ { scan: [Object], config: '.taro-plugin-tailwind/h5.config.js' } ]
}
TypeError: Cannot read property '__expression' of undefined
at Object.toConfig (D:\fe\git\ltzx\wopingan\node_modules\webpack-chain\src\Plugin.js:46:38)
at D:\fe\git\ltzx\wopingan\node_modules\webpack-chain\src\Config.js:125:61
at Array.map ()
at module.exports.toConfig (D:\fe\git\ltzx\wopingan\node_modules\webpack-chain\src\Config.js:125:40)
at D:\fe\git\ltzx\wopingan\node_modules@tarojs\webpack-runner\src\index.ts:130:38
at Generator.next ()
at fulfilled (D:\fe\git\ltzx\wopingan\node_modules@tarojs\webpack-runner\dist\index.js:5:58)

安装了没有提示,也没有效果

按照文档步骤安装后,添加 CSS 类名然后重新运行项目没有效果。
使用提供的示例代码运行也报错:

yarn dev:weapp                               
yarn run v1.22.5
$ NODE_ENV=production npm run build:weapp -- --watch
'NODE_ENV' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

info:
OS: WIN 10
taro: 3.1.4
taro-plugin-tailwind: "^1.1.2"

自定义"spacing"字段不生效

一、package.json 文件如下

"dependencies": {
    "@babel/runtime": "^7.7.7",
    "@tarojs/cli": "^3.1.4",
    "@tarojs/components": "3.1.4",
    "@tarojs/react": "3.1.4",
    "@tarojs/runtime": "3.1.4",
    "@tarojs/taro": "3.1.4",
    "autoprefixer": "^9.8.6",
    "postcss": "^7.0.35",
    "qs": "^6.10.1",
    "react": "^17.0.0",
    "react-dom": "^17.0.0",
    "sa-sdk-miniprogram": "^1.14.7",
    "tailwindcss": "^2.0.1-compat",
    "taro-plugin-tailwind": "^1.1.12",
    "taro-ui": "^3.0.0-alpha.3"
  },
  "devDependencies": {
    "@babel/core": "^7.8.0",
    "@tarojs/mini-runner": "3.1.4",
    "@tarojs/webpack-runner": "3.1.4",
    "@types/react": "^17.0.2",
    "@types/webpack-env": "^1.13.6",
    "@typescript-eslint/eslint-plugin": "^4.15.1",
    "@typescript-eslint/parser": "^4.15.1",
    "babel-preset-taro": "3.1.4",
    "eslint": "^7.2.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.2.0",
    "eslint-config-taro": "3.1.4",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "^4.2.0",
    "patch-package": "^6.4.7",
    "taro-iconfont-cli": "^3.2.1",
    "typescript": "^4.1.0"
  }

二、安装
npm i taro-plugin-tailwind --save

三、配置文件(config/index.js)

 plugins: [
        [
            'taro-plugin-tailwind', // 插件详情详情: https://taro-ext.jd.com/plugin/view/5fbb3a0799370e09266e2d68
            {
                scan: {
                    dirs: ['./src'], // 只扫描 src 目录下的文件
                    exclude: ['dist/**/*'], // 排除 dist 目录
                },
                // 具体参数见:https://github.com/windicss/vite-plugin-windicss/blob/main/packages/plugin-utils/src/options.ts#L10
            },
        ],
    ],

四、生成配置
image

五、引入 windi.css
image

六、在项目根目录下自定义tailwind.config.js配置文件
image

七、使用
image

八、在控制台中查看
iamge

taro 3.3.12 打包出问题了吧

UnhandledPromiseRejectionWarning: TypeError: Cannot read property '__expression' of undefined
at Object.toConfig (/Users/nick/Documents/workspace/ics.taro/node_modules/webpack-chain/src/Plugin.js:43:38)
at /Users/nick/Documents/workspace/taro/node_modules/webpack-chain/src/Config.js:125:61
at Array.map ()
at module.exports.toConfig (/Users/nick/Documents/workspace/taro/node_modules/webpack-chain/src/Config.js:125:40)
at /Users/nick/Documents/workspace/taro/node_modules/@tarojs/mini-runner/src/index.ts:37:61

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.