为什么ExtractTextPlugin通过插件创建一个空的css文件?为什么sass loader在使用Webpack导入时会提供空对象?

时间:2017-01-24 01:50:47

标签: css webpack css-loader sass-loader extract-text-plugin

我已经将webpack配置为使用ExtractTextPlugin从sass生成单个css文件,并允许它通过webpack加载器导入在reactjs应用程序中可用。

通过react组件中的import语句检测sass文件的使用情况,并且正在生成输出文件,但生成的main.css文件为空,导入返回空样式对象,是否有人知道我做错了什么?

结构

client
  - app
    - components
      - navbar
        Navbar.js
        navbar.scss
  - public
    - styles
      main.scss
    bundle.js
    index.html

Webpack配置

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const BUILD_DIR = path.resolve(__dirname, 'public');
const APP_DIR = path.resolve(__dirname, 'app');

const config = {
   entry: APP_DIR + '/index.js',
   output: {
     path: BUILD_DIR,
     filename: 'bundle.js'
  },
  externals: {
    'cheerio': 'window',
    'react/lib/ExecutionEnvironment': true,
    'react/lib/ReactContext': true,
  },
  module : {
    loaders : [
      {
        test : /\.jsx?/,
        include : APP_DIR,
        loader : 'babel'
      },
      {
        test: /\.scss$/,
        include : APP_DIR,
        loader: ExtractTextPlugin.extract('css!sass')
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'My App'
    }),
    new ExtractTextPlugin('styles/main.css', {
      allChunks: true
    })
  ]
};

module.exports = config;

navbar.scss

.navbar {
  display: flex;

  .navbarBrand {

  }
}

Navbar.js

import React, { Component, PropTypes } from 'react'
import classnames from 'classnames'
import Login from '../login/Login'
import Logout from '../logout/Logout'
import styles from './navbar.scss';


export default class Navbar extends Component {

  render() {
    const { isAuthenticated, errors, onLogin, onLogout } = this.props;

    console.log(styles);
    const navBarStyles = classnames(styles.navbar);
    const navBarBrandStyles = classnames(styles.navbarBrand);

    return (
        <nav className={navBarStyles}>
          <div className='container-fluid'>
            <a className={navBarBrandStyles} href="#">User Management App</a>
            <div className='navbar-form'>

              {!isAuthenticated &&
                <Login
                    errors={errors}
                    onLoginClick={ (creds) => onLogin(creds) }
                />
              }

              {isAuthenticated &&
                <Logout onLogoutClick={() => onLogout()} />
              }

            </div>
          </div>
        </nav>
    )
  }
}

Navbar.propTypes = {
  isAuthenticated: PropTypes.bool.isRequired,
  errors: PropTypes.arrayOf(PropTypes.string),
  onLogin: PropTypes.func.isRequired,
  onLogout: PropTypes.func.isRequired
};

症状

装载机和进口(不再是问题,请参阅下面的答案)

  1. console.log(styles)= Object {}

  2. styles.navbar = undefined

  3. 插件

    1. public / styles / main.css为空(不再是问题,请参阅下面的答案)
    2. 问题

      1. 为什么在Narbar.js中导入样式时它是一个空对象?我配置错了什么? (我已找到解决方案,并在答案部分提供了以下答案)
      2. 为什么public / style / main.css为空? (我已找到解决方案,并在答案部分提供了以下答案)

2 个答案:

答案 0 :(得分:1)

sass-loader已修复。

关键是为css-loader添加 modules = true ,然后您可以从scss文件中导入样式。

{
            test: /\.scss$/,
            loader: extractCSS.extract('style', 'css?modules=true!sass?sourceMap=true')
          }

public / style / main.css已修复。

说实话,我不确定区别是什么,但现在填充了public / styles / main.css文件。

我做的唯一更改是webpack配置。

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const BUILD_DIR = path.resolve(__dirname, 'public');
const APP_DIR = path.resolve(__dirname, 'app');

let generateHtml = new HtmlWebpackPlugin({ title: 'My App' });
let extractCSS = new ExtractTextPlugin('styles/[name].css', { allChunks: true });

const config = {
  entry: APP_DIR + '/index.js',
  output: {
    path: BUILD_DIR,
    filename: 'bundle.js'
  },
  externals: {
    'cheerio': 'window',
    'react/lib/ExecutionEnvironment': true,
    'react/lib/ReactContext': true,
  },
  module : {
    loaders : [
      {
        test : /\.jsx?/,
        include : APP_DIR,
        loader : 'babel'
      },
      {
        test: /\.scss$/,
        loader: extractCSS.extract('style', 'css?modules=true!sass?sourceMap=true')
      }
    ]
  },
  plugins: [
    generateHtml,
    extractCSS
  ]
};

module.exports = config;

答案 1 :(得分:0)

  1. 至于空文件,以下是我在其中一个项目中使用此插件的方法: (Webpack@2.2,ExtractTextPlugin v2)
  2. 插件实例化

    const extractSCSS = new ExtractTextPlugin({
        filename: "[name].scss",
        allChunks: true
    });
    

    装载机

    {
        test: /\.(css|scss)$/,
        loader: extractSCSS.extract({
            loader: [
                {
                    loader: "css-loader",
                    options: { modules: true }
                },
                {
                    loader: "sass"
                }
            ],
            defaultLoader: "style-loader"
        })
    }
    

    插件

    plugins: [extractSCSS],
    

    您可以在此project

    中查看我对此进行抽样的完整Webpack配置