postCss无法在laravel外部使用laravel-mix,scss和tailwindcss

时间:2020-04-18 14:23:40

标签: npm laravel-mix postcss tailwind-css

我在使用tailwindcsspostCsslaravel-mix优化Scss时遇到麻烦。 npm run dev生成正确的CSS。但是,使用生产版本npm run prod不会导出我在HTML模板中使用的类。

package.json

{
  "name": "school",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  },
  "devDependencies": {
    "tailwindcss": "^1.2.0",
    "cross-env": "^7.0.2",
    "laravel-mix": "^5.0.4",
    "laravel-mix-purgecss": "^5.0.0-rc.1",
    "sass": "^1.26.3",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.11"
  }
}

laravel-mix.js

const mix = require('laravel-mix');
const tailwindcss = require('tailwindcss');
require('laravel-mix-purgecss');


mix.js('src/js/app.js', 'public/js')
    .sass('src/sass/app.scss', 'public/css')
    .options({
        processCssUrls: false,
        postCss: [tailwindcss('./tailwind.config.js')],
    })
    .purgeCss();

src / app.scss

我尝试过没有purgecss ignore,但也没有任何帮助 差异。

/* purgecss start ignore */
@tailwind  base;
@tailwind  components;
/* purgecss end ignore */
@tailwind  utilities;
//@tailwind screens;

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="public/css/app.css">
    <script src="public/js/app.js"></script>
</head>
<body class="bg-teal-400">

<div class="container">
    <h1 class="text-6xl text-orange-500">Hello</h1>
</div>

</body>
</html>

请查看this repository中的整个源代码。

问题:

如何使用taliwindcssscss的{​​{1}}和laravel-mix优化postCss项目?

1 个答案:

答案 0 :(得分:5)

您可以将 PostCSS 插件传递到 sass 方法中,如下面的代码示例所示。

// webpack.mix.js
let mix = require('laravel-mix');

mix
  .sass('assets/scss/app.scss', 'dist', {}, [
    require("tailwindcss"),
  ])
  .setPublicPath('web')
;