使用 webpack 将顺风 css 与 sass 相结合



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

我的设置如下:

布局.scss

@import "~tailwindcss/preflight.css";
@import "~tailwindcss/components.css";
.my-class {
    @apply text-blue;    
    @apply border-red;
}
@import "~tailwindcss/utilities.css";

条目.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}

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

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

提前谢谢。

哇,

所以在更多地摆弄加载器之后,我让它工作了:-(供将来参考:

我在 SCSS 文件的 css-loader 中添加了options: { importLoaders: 1 },并从我的 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' }
                ]
            }            
        ]
    }
};

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

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

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

最新更新