webpack react 单独打包 CSS
webpack react 单独打包 CSS
webpack require css的方法,默认会把css 打入到js文件中,加载顺序有问题,如果需要打出独立的css文件
操作步骤:
step1:
安装 webpack plugin 插件
npm install extract-text-webpack-plugin --save
step2:
修改 webpack.config.js 配置
引用plugin
var ExtractTextPlugin = require("extract-text-webpack-plugin");
config的module 中:
{ test: /\.less$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader","less-loader") },
最后plugins添加
plugins: [ new ExtractTextPlugin("./css/[name][hash:8].css") ]
问题
Q: Module build failed: ReferenceError: window is not defined.
A: 修改loader
错误示范: loader: ExtractTextPlugin.extract("style-loader!css-loader!less-loader")
正确示范: loader: ExtractTextPlugin.extract("style-loader", "css-loader","less-loader")
参考文章:
1. https://webpack.github.io/docs/stylesheets.html