css - PostCSS 和 Webpack 配置

标签 css webpack webpack-2 postcss cssnext

我真的很沮丧,因为我找不到关于这个主题的任何有用资源。

我只想看我的 .css 文件,使用 post css 插件来转换它们,最后将它们导出到我的/public 文件夹,就像我已经对我的 .jsx 文件所做的那样

这是我的网络包配置

    const path = require('path');
const webpack = require('webpack');

var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: path.resolve('src/index.jsx'),
    output: {
        path: path.resolve('public'),
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['*', '.js', '.jsx']
    },
    devServer: {
        publicPath: "/",
        contentBase: "./public"
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                modules: true,
                                localIdentName: '[name]__[local]___[hash:base64:5]'
                            }
                        }, {
                            loader: 'postcss-loader',
                            options: {
                                plugins: function() {
                                    return [require('lost'), require('postcss-cssnext'), require('postcss-import')]
                                }
                            }
                        }
                    ]
                })
            }, {
                test: /\.jsx?$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['es2015', 'react']
                        }
                    }
                ],
                exclude: /(node_modules|bower_components)/
            }
        ]
    },
    plugins: [new ExtractTextPlugin("main.css")]
}

最佳答案

我假设你正在使用 webpack2

如果你想单独导出你的 css 文件,你需要 ExtractTextPlugin。这是我的 css 加载器,可以正常工作

我在 webpack 配置中定义了 post css 插件,因为这样它就留在了一个地方。希望这有助于:

var ExtractTextPlugin = require('extract-text-webpack-plugin');
...
...
module.exports = {
...
...
    module: {
        rules: [
...
...
            {
                test: /\.css$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract(
                    { fallback: 'style-loader',
                    use: [{
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            localIdentName:'[name]__[local]___[hash:base64:5]'
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: function() {
                                return [
                                    require('autoprefixer')
                                ]
                            }
                        }
                    },
                ]
            })
        },
}

关于css - PostCSS 和 Webpack 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42988207/

相关文章:

javascript - jQuery:在状态和类之间切换

javascript - Bourbon Refills Navigation JavaScript 错误 : Uncaught TypeError: undefined is not a function

typescript - 尝试将 typescript 库与 webpack 捆绑时导出未定义

webpack - 仅针对 Webpack 中的特定入口点输出样式表而不生成任何脚本?

javascript - 知道点击了哪个用户创建的 div

node.js - Electron中检测 Node 环境的方法

javascript - 如何发布 Vue.js 组件库?

angular - 窗口未使用 Angular4 + AoT + Webpack2 定义

html - css 模块样式被 css 框架覆盖

html - CSS垂直对齐不同的文本大小