我已经将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
};
症状
装载机和进口(不再是问题,请参阅下面的答案)
console.log(styles)= Object {}
styles.navbar = undefined
插件
问题
答案 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)
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配置