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

2. http://stackoverflow.com/questions/28223040/window-not-defined-error-when-using-extract-text-webpack-plugin-react

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM