简体   繁体   English

webpack2 | postcss构建警告

[英]webpack2 | postcss build warning

I have a postcss.config.js file: 我有一个postcss.config.js文件:

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

module.exports = {
  parser: 'postcss-scss',
    plugins: [
      require('postcss-smart-import')({
        addDependencyTo: webpack,
        path: [
          path.resolve(__dirname, 'src/common'),
          path.resolve(__dirname, 'src/common/styles'),
          path.resolve(__dirname, 'src/app1'),
          path.resolve(__dirname, 'src/app2'),
        ]
      }),
      require('precss'),
      require('autoprefixer'),
    ]
}

in webpack.conf.js I have simple definition: webpack.conf.js我有一个简单的定义:

{
    test: /\.(scss|sass|css)$/,
    exclude: /node_modules/,
    use: [
      'style-loader',
      'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]--[hash:base64:5]',
      'postcss-loader'
    ]
}

During a build I get a warning: 在构建期间,我收到警告:

WARNING in ./~/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]--[hash:base64:5]!./~/postcss-loader/lib!./src/common/shared/ui/Button.scss

(Emitted value instead of an instance of Error) postcss-extend: /Users/kania/projects/app-frontend/src/common/shared/ui/Button.scss:22:5: @extend is being used in an anti-pattern (extending things not yet defined). This is your first and final warning

@ ./src/common/shared/ui/Button.scss 4:14-210 13:2-17:4 14:20-216 @ ./src/common/shared/ui/Button.tsx @ ./src/common/shared/ui/index.ts (...)

In Button.scss I have a very simple definitions: Button.scss我有一个非常简单的定义:

@import 'shared/styles/buttons';
@import 'shared/styles/fonts';

.buttonContainer {
  display: flex;
}

.button {
  @extend %smallFont;
  padding: 0 2.5rem;
  flex: 1;

  &.style_primary {
    @extend %buttonPrimary;
  }

  &.style_secondary {
    @extend %buttonSecondary;
  }

  &.style_tertiary {
    @extend %buttonTertiary;
  }
}

Inside .button class I define 3 nested classes ( &.style_primary &.style_secondary and &.style_tertiary ). .button类中,我定义了3个嵌套类( &.style_primary &.style_secondary&.style_tertiary )。 I found out if 2 of them are commented everything works. 我发现其中有2条评论是否一切正常。 It looks like if I use more than one placeholder selector from one file it throws a warning... 似乎如果我从一个文件中使用多个占位符选择器,则会引发警告...

Placeholders are defined in imported files, files exist on defined location. 占位符在导入的文件中定义,文件存在于定义的位置。

I would appreciate any hint, how to solve this issue. 我将不胜感激如何解决此问题。

Used packages: 二手包装:

  • postcss-loader@^2.0.5 postcss-loader@^2.0.5

  • postcss-extend@^1.0.5 postcss-extend@^1.0.5

  • postcss-smart-import@^0.7.4 postcss-smart-import@^0.7.4

  • precss@^1.4.0 autoprefixer@^7.1.1 precss@^1.4.0 autoprefixer@^7.1.1

  • webpack@^2.5.1 webpack@^2.5.1

  • webpack-dev-server@^2.4.5 webpack-dev-server@^2.4.5

I use this command to run the build: webpack-dev-server --hot --progress --config webpack.dev.config.js 我使用此命令来运行构建: webpack-dev-server --hot --progress --config webpack.dev.config.js

After some hours spent on searching the reason of warning and not built styles for everything after the warning, I finally found the cause. 在花了几个小时搜索警告的原因之后,并没有为警告之后的所有内容构建样式,我终于找到了原因。

And the winner is: 最终获胜者是:

precss@^1.4.0

This is old package, last changes were added 2 years ago. 这是旧包,最近的更改是在2年前添加的。 It is not even a package, just gathered plugins for postcss to process styles. 它甚至不是一个包,只是为Postcss处理样式收集了插件。

I removed this package from my project and added few needed plugins postcss.conf.js : 我从项目中删除了此软件包,并添加了一些所需的插件postcss.conf.js

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

module.exports = {
  parser: 'postcss-scss',
    plugins: [
      require('postcss-smart-import')({
        addDependencyTo: webpack,
        path: [
          path.resolve(__dirname, 'src/common'),
          path.resolve(__dirname, 'src/app1'),
          path.resolve(__dirname, 'src/app2'),
        ]
      }),
      require('postcss-advanced-variables'),
      require('postcss-mixins'),
      require('postcss-nested'),
      require('postcss-nesting'),
      require('postcss-extend'),
      require('autoprefixer'),
    ]
};

works! 作品!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM