webpack-2 - PostCSS 中更好的类名

标签 webpack-2 postcss

我正在使用 Webpack 2 构建我的应用程序。我使用 PostCSS 2 作为 CSS 模块。这是我的 Webpack CSS 导入配置:

  {
    test: /\.css$/,
    exclude: /(node_modules)/,
    use: [
      'style-loader',
      {
        loader: 'css-loader',
        options: {
          modules: true,
          importLoaders: 1,
        },
      },
      {
        loader: 'postcss-loader',
        options: {
          plugins: loader => [
            require("postcss-import")({
              path: './js',
              addDependencyTo: webpack,
            }),
            require('postcss-cssnext')(),
          ],
        },
      },
    ],
  },

现在的问题是,这会生成非常难看的类名。使用 React 的示例:

import React, {Component} from 'react';
import styles from './Element.css';

export default class Element extends Component{
  render(){
    return (
      <div className={styles.myElement}>This is an example.</div>
    );
  }
}

渲染为:

<div class="_1DHVkmCxFFQMFYac-L_MIg">This is an example.</div>

现在,这在生产中很好,但在开发中,像 class="myElement--_1DHVkmCxFFQMFYac-L_MIg" 这样的东西会更好。我找到了GitHub issue讨论这个问题,但由于我是 Webpack 的新手,我不知道如何实现他们的建议。无论我尝试什么都会导致错误,Webpack 无法解析该模块。

我尝试将 localIdentName: '[local]--[hash:base64:5]' 添加到 postcss-loader 选项,但这没有任何作用。

那么,如何在生成的类名中保留原始类名呢?

最佳答案

将此行添加到 css-loader 解决了它:

localIdentName: debug ? '[name]_[local]___[hash:base64:10]' : undefined,

关于webpack-2 - PostCSS 中更好的类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44087393/

相关文章:

css - SCSS 中创建-扩展类名的未知@规则?

css - Webpack + postcss + 主题 + 热重载

javascript - 使用整个 npm 模块创建异步 WebPack CommonsChunk

heroku - Webpack Config 没有导出对象

jquery - 尽管安装了 JQuery 和 Bootstrap,但工具提示仍不起作用

postcss - 如何使用postcss压缩tailwind中的文件大小?

postcss - 运行顺风/postcss

javascript - 错误 'NODE_ENV' 未定义 no-undef eslint

javascript - webpack:具有 webworker 和多个 block 的 commonchunksplugin

css - PostCSS 和 Webpack 配置