2017-08-17 12 views
1

编译我有与编译的WebPack为了CSS的问题。错误的CSS规则,以便与的WebPack

我目前依赖使用这些包:

  • “CSS加载器”: “^ 0.28.4”,
  • “式装载机”: “^ 0.18.2”,
  • “上海社会科学院装载机”: “^ 6.0.6”,
  • “上海社会科学院资源加载器”: “^ 1.3.0”,
  • “的WebPack”: “^ 3.5.5”,

这里是我的webpack.config.js

const { alias } = require('./webpack/common.js'); 

const path = require('path'); 
const webpack = require('webpack'); 
const Dashboard = require('webpack-dashboard'); 
const DashboardPlugin = require('webpack-dashboard/plugin'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 

const nodeEnv = process.env.NODE_ENV || 'development'; 
const isProd = nodeEnv === 'production'; 

const sourcePath = path.join(__dirname, './src'); 
const staticPath = path.join(__dirname, './dist'); 

const commonCssOptions = { 
    sass: { 
    loaders: ['sass-loader', 'sass-resources-loader'], 
    }, 
    context: path.resolve(__dirname, '.'), 
    output: { 
    path: 'dist', 
    }, 
}; 

const plugins = [ 
    new webpack.optimize.CommonsChunkPlugin({ 
    name: 'vendor', 
    minChunks: Infinity, 
    filename: 'vendor.bundle.js', 
    }), 
    new webpack.DefinePlugin({ 
    'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }, 
    }), 
    new webpack.NamedModulesPlugin(), 
    new ExtractTextPlugin({ filename: 'css/bundle.css', disable: false, allChunks: true }), 
    new webpack.ContextReplacementPlugin(
    /moment[/\\]locale/, 
    /(en-gb)\.js/ 
), 
]; 

if (isProd) { 
    plugins.push(
    new webpack.optimize.OccurrenceOrderPlugin(), 
    new webpack.LoaderOptionsPlugin({ 
     minimize: true, 
     debug: false, 
     options: commonCssOptions, 
    }) 
); 
} else { 
    const dashboard = new Dashboard(); 
    plugins.push(
    new webpack.HotModuleReplacementPlugin(), 
    new webpack.LoaderOptionsPlugin({ 
     options: commonCssOptions, 
    }), 
    new DashboardPlugin(dashboard.setData) 
); 
} 

module.exports = { 
    devtool: isProd ? false : 'cheap-module-source-map', 
    entry: { 
    js: './src/index.js', 
    vendor: [ 
     'babel-polyfill', 
     'bootstrap-loader', 
     'classnames', 
     'react', 
     'react-dom', 
     'react-redux', 
     'redux', 
     'react-router', 
     'react-router-dom', 
     // 'moment', 
    ], 
    }, 
    output: { 
    path: staticPath, 
    publicPath: '/', 
    filename: '[name].bundle.js', 
    }, 
    module: { 
    rules: [ 
     { 
     test: /\.html$/, 
     exclude: /node_modules/, 
     use: { 
      loader: 'file-loader', 
      query: { 
      name: '[name].[ext]', 
      }, 
     }, 
     }, 
     { 
     test: /\.s?css$/, 
     exclude: /node_modules/, 
     use: [ 
      'style-loader', 
      { 
      loader: 'css-loader', 
      options: { 
       importLoaders: 2, 
       modules: true, 
       localIdentName: '[name]__[local]_[hash:base64:5]', 
      }, 
      }, 
      { 
      loader: 'sass-loader', 
      options: { 
       includePaths: [ 
       path.join(__dirname, './components-lib/src/assets/styles'), 
       ], 
      }, 
      }, 
      { 
      loader: 'sass-resources-loader', 
      options: { 
       resources: [ 
       './components-lib/src/assets/styles/_variables.scss', 
       './components-lib/src/assets/styles/_mixins.scss', 
       ], 
      }, 
      }, 
      'postcss-loader', 
     ], 
     }, 
     { 
     test: /\.jsx?$/, 
     exclude: /node_modules/, 
     use: [ 
      'babel-loader', 
     ], 
     }, 
     { 
     test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/, 
     loader: 'file-loader', 
     query: { 
      name: '[name].[ext]', 
     }, 
     }, 
    ], 
    }, 
    resolve: { 
    extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.jsx', '.js'], 
    modules: [ 
     'node_modules', 
     sourcePath, 
    ], 
    alias, 
    }, 
    plugins, 
    devServer: { 
    contentBase: './src', 
    historyApiFallback: true, 
    host: '0.0.0.0', 
    port: 3000, 
    compress: isProd, 
    inline: !isProd, 
    hot: !isProd, 
    quiet: true, 
    stats: { 
     assets: true, 
     children: false, 
     chunks: false, 
     hash: false, 
     modules: false, 
     publicPath: false, 
     timings: true, 
     version: false, 
     warnings: false, 
     colors: { 
     green: '\u001b[32m', 
     }, 
     performance: { 
     hints: false, 
     }, 
    }, 
    }, 
    externals: { 
    cheerio: 'window', 
    'react/lib/ExecutionEnvironment': true, 
    'react/lib/ReactContext': true, 
    }, 
}; 

初始加载我有错的CSS为了

enter image description here

但在炎热的重新加载顺序变成正确

enter image description here

我的组件库是一个git子模块(如果它是很重要的)

回答

0

我想是因为事情得到重新写入的方式,排序必将改变即新的东西将在底端。我也注意到Webpack不能保证CSS的顺序。我无法找到'webpack'解决方案,但我不确定是否有解决方案。可能是你想听的机器人,对不起!

我已经解决了它是通过使用smaccs/BEM记号或者和/或保证写CSS很少/从来没有过写其他的CSS的唯一途径。例如,如果你需要使用一个“修饰”改变从白色背景为红色,那么这实际上是两个修改和默认的“基地”类没有背景设定在所有。这样你可以保证订购无所谓。 TBH,这原来是一个更可读和可维护的写作CSS的方式,但我离题了!