【问题标题】:Combining tailwind css with sass using webpack使用 webpack 将顺风 css 与 sass 结合起来
【发布时间】:2019-08-31 13:50:19
【问题描述】:

我正在努力让 Tailwind CSS 与 SASS 和 Webpack 一起工作。似乎tailwind的postcss配置在处理@tailwind preflight@tailwind components@tailwind utilities方面并没有真正做任何事情

我的设置如下:

layout.scss

@import "~tailwindcss/preflight.css";
@import "~tailwindcss/components.css";

.my-class {
    @apply text-blue;    
    @apply border-red;
}

@import "~tailwindcss/utilities.css";

entry.js

import '../css/src/layout.scss';

postcss.config.js

const tailwindcss = require('tailwindcss');
const purgecss = require('@fullhuman/postcss-purgecss');
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');

module.exports = {
    plugins: [
        tailwindcss('./tailwind.js'),
        cssnano({
            preset: 'default',
        }),
        purgecss({
            content: ['./views/**/*.cshtml']
        }),
        autoprefixer
    ]
 }

webpack.config.js

// NPM plugins
const autoprefixer = require('autoprefixer');
const WebpackNotifierPlugin = require('webpack-notifier');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');

module.exports = {
    entry: {
        main: './scripts/entry.js'
    },
    output: {
        filename: '[name].bundle.js',
        publicPath: './'
    },
    watch: false,
    externals: {
        jquery: 'jQuery'
    },
    mode: 'development',
    plugins: [
        // Notify when build succeeds
        new WebpackNotifierPlugin({ alwaysNotify: true }),

        // Extract any CSS from any javascript file to process it as LESS/SASS using a loader
        new MiniCssExtractPlugin({
            fileame: "[name].bundle.css"
        }),

        // Minify CSS assets
        new OptimizeCSSAssetsPlugin({}),

        // Use BrowserSync plugin for file changes. I.e. if a CSS/SASS/LESS file changes, the changes will be injected directly in the browser with no page load
        new BrowserSyncPlugin({
            proxy: 'mysite.local',
            open: 'external',
            host: 'mysite.local',
            port: 3000,
            files: ['./dist/main.css', './views', './tailwind.js']
        },
            {
                // disable reload from the webpack plugin since browser-sync will handle CSS injections and JS reloads
                reload: false
            })
    ],
    module: {
        rules: [
            {
                // Transpile ES6 scripts for browser support
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            },            
            {
                test: /\.(png|jpg|gif|svg|eot|ttf|woff)$/,
                use: [
                    {
                        loader: 'file-loader'
                    }
                ]
            },       
            {
                // Extract any SCSS content and minimize
                test: /\.scss$/,
                use: [                    
                    MiniCssExtractPlugin.loader,
                    { loader: 'css-loader' },
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: () => [autoprefixer()]
                        }
                    },
                    {
                        loader: 'sass-loader',
                        options: {                            
                            plugins: () => [autoprefixer()]
                        }
                    } 
                ]
            },
            {
                // Extract any CSS content and minimize
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    { loader: 'css-loader', options: { importLoaders: 1 } },
                    { loader: 'postcss-loader' }
                ]
            }            
        ]
    }
};

当我运行 Webpack 时,一切运行正常,但 /dist/main.css 的内容是:

@tailwind preflight;@tailwind components;@tailwind utilities;.my-class{@apply text-blue;@apply border-red}

我怀疑它与加载程序的(顺序)有关,但我似乎无法弄清楚为什么它没有得到正确处理。

有谁知道我在这里做错了什么? :-)

提前致谢。

【问题讨论】:

    标签: css webpack sass tailwind-css


    【解决方案1】:

    有一个名为tailwindcss-transpiler的扩展,它可以将你的layout.tailwind.scss文件编译成纯CSS文件。它还优化了功能。希望对你有用。

    对于 VS 代码 https://marketplace.visualstudio.com/items?itemName=sudoaugustin.tailwindcss-transpiler

    对于原子 https://atom.io/packages/tailwindcss-transpiler

    【讨论】:

      【解决方案2】:

      哇,所以在进一步摆弄装载机之后,我让它工作了 :-) 以供将来参考:

      我将options: { importLoaders: 1 } 添加到 SCSS 文件的 css-loader 并从我的 webpack.config.js 文件的 postcss-loader 中删除:plugins: () => [autoprefixer()]

      完整、更新的 webpack.config.js 文件:

      // NPM plugins
      const autoprefixer = require('autoprefixer');
      const WebpackNotifierPlugin = require('webpack-notifier');
      const MiniCssExtractPlugin = require("mini-css-extract-plugin");
      const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
      const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
      
      module.exports = {
          entry: {
              main: './scripts/entry.js'
          },
          output: {
              filename: '[name].bundle.js',
              publicPath: './'
          },
          watch: false,
          externals: {
              jquery: 'jQuery'
          },
          mode: 'development',
          plugins: [
              // Notify when build succeeds
              new WebpackNotifierPlugin({ alwaysNotify: true }),
      
              // Extract any CSS from any javascript file to process it as LESS/SASS using a loader
              new MiniCssExtractPlugin({
                  fileame: "[name].bundle.css"
              }),
      
              // Minify CSS assets
              new OptimizeCSSAssetsPlugin({}),
      
              // Use BrowserSync plugin for file changes. I.e. if a CSS/SASS/LESS file changes, the changes will be injected directly in the browser with no page load
              new BrowserSyncPlugin({
                  proxy: 'mysite.local',
                  open: 'external',
                  host: 'mysite.local',
                  port: 3000,
                  files: ['./dist/main.css', './views', './tailwind.js']
              },
                  {
                      // disable reload from the webpack plugin since browser-sync will handle CSS injections and JS reloads
                      reload: false
                  })
          ],
          module: {
              rules: [
                  {
                      // Transpile ES6 scripts for browser support
                      test: /\.js$/,
                      exclude: /node_modules/,
                      use: {
                          loader: 'babel-loader',
                          options: {
                              presets: ['@babel/preset-env']
                          }
                      }
                  },            
                  {
                      test: /\.(png|jpg|gif|svg|eot|ttf|woff)$/,
                      use: [
                          {
                              loader: 'file-loader'
                          }
                      ]
                  },       
                  {
                      // Extract any SCSS content and minimize
                      test: /\.scss$/,
                      use: [                       
                          MiniCssExtractPlugin.loader,
                          { loader: 'css-loader', options: { importLoaders: 1 } },                    
                          {
                              loader: 'postcss-loader'                        
                          },                    
                          {
                              loader: 'sass-loader',
                              options: {                            
                                  plugins: () => [autoprefixer()]
                              }
                          }                    
                      ]
                  },
                  {
                      // Extract any CSS content and minimize
                      test: /\.css$/,
                      use: [
                          MiniCssExtractPlugin.loader,
                          { loader: 'css-loader', options: { importLoaders: 1 } },
                          { loader: 'postcss-loader' }
                      ]
                  }            
              ]
          }
      };
      

      【讨论】:

        猜你喜欢
        • 2018-06-18
        • 1970-01-01
        • 2017-02-23
        • 2018-04-08
        • 2021-02-18
        • 2013-12-12
        • 1970-01-01
        • 2017-02-18
        • 2018-11-19
        相关资源
        最近更新 更多