2016-12-16 48 views
1

我正在使用react,es6和webpack构建一个draftjs编辑器模块。它工作正常,但是当我运行构建时,我得到了由Babel编译的所有JS文件,但是,我的CSS没有。 这是我如何导入我的模块内的CSS:当我构建模块时,CSS不会编译到Webpack中

import '../styles.css'; 

这里是我的webpack.config:

var ExtractTextPlugin = require('extract-text-webpack-plugin'); 

module.exports = { 
    output: { 
    path: path.join(__dirname, 'lib'), 
    filename: 'drafty.js', 
    libraryTarget: 'commonjs2', 
    }, 
    module: { 
    loaders: [ 
     { 
     test: /\.css$/, 
     loader: ExtractTextPlugin.extract('style-loader', 'css-loader?modules&importLoaders=1&localIdentName=drafty[local]__[hash:base64:5]!postcss-loader'), 
     }, 
    ], 
    }, 
    plugins: [ 
    new ExtractTextPlugin('drafty.css', { allChunks: true }) 
    ] 
}; 

我的CSS的毛刺:

@import url(../node_modules/draft-js-linkify-plugin/lib/plugin.css); 
@import url(../node_modules/draft-js-emoji-plugin/lib/plugin.css); 
@font-face { 
    font-family: 'Material Icons'; 
    font-style: normal; 
    font-weight: 400; 
} 

i { 
    font-family: 'Material Icons' !important; 
    speak: none; 
    font-style: normal; 
    font-weight: normal; 
    font-variant: normal; 
    text-transform: none; 
    line-height: 1; 
    display: inline-block; 
    -webkit-font-smoothing: antialiased; 
    -moz-osx-font-smoothing: grayscale; 
} //... and more stuff 

这里是我的依赖(请参阅我已经安装了style-loader,css-loader和postcss-loader)

"dependencies": { 
    "draft-js": "^0.9.1", 
    "draft-js-emoji-plugin": "^2.0.0-beta9", 
    "draft-js-export-html": "^0.5.2", 
    "draft-js-linkify-plugin": "^2.0.0-beta9", 
    "draft-js-plugins-editor": "^2.0.0-beta9", 
    "draftjs-utils": "^0.3.2", 
    "extract-text-webpack-plugin": "^1.0.1", 
    }, 
    "devDependencies": { 
    "babel-cli": "^6.18.0", 
    "babel-core": "^6.20.0", 
    "babel-eslint": "^7.1.1", 
    "babel-loader": "^6.2.9", 
    "babel-preset-stage-0": "^6.16.0", 
    "classnames": "^2.2.5", 
    "style-loader": "^0.13.1", 
    "css-loader": "^0.26.1", 
    "babel-plugin-react-transform": "^2.0.2", 
    "babel-plugin-transform-class-properties": "^6.19.0", 
    "babel-polyfill": "^6.20.0", 
    "babel-preset-es2015": "^6.18.0", 
    "babel-preset-react": "^6.16.0", 
    "eslint": "^3.12.1", 
    "eslint-config-airbnb": "^13.0.0", 
    "eslint-loader": "^1.6.1", 
    "eslint-plugin-import": "^2.2.0", 
    "eslint-plugin-jsx-a11y": "2.2.3", 
    "eslint-plugin-react": "^6.8.0", 
    "rimraf": "^2.5.4", 
    "postcss-loader": "^1.2.1", 
    "webpack": "^1.14.0" 
    }, 
    "peerDependencies": { 
    "react": "^15.4.1", 
    "react-dom": "^15.4.1" 
    } 

我的生成命令:

WEBPACK_CONFIG=$(pwd)/webpack.config.js BABEL_DISABLE_CACHE=1 BABEL_ENV=production NODE_ENV=production ./node_modules/.bin/babel --out-dir='lib' src && clear 

谢谢。

回答

1

我想问题是你已经添加到你的css-loadercss-modules

尝试以下,

loaders: [ 
     { 
     test: /\.css$/, 
     loader: ExtractTextPlugin.extract('style-loader', 'css-loader!postcss-loader'), 
     }, 
    ], 

如果你真的想用css-modules,请执行下列操作

import styles from '../style.css' 

... 
... 
return <div classname={styles.myClass}>Hello</div> 

了解更多关于css-moduleshere

希望这有助于!

+0

谢谢。奇迹般有效。 :) – MattGA

相关问题