上一章讲的是装载模板,这一章讲的是装载样式css
$ mkdir 0x011-styling-loader $ cd 0x011-styling-loader $ npm init -y $ npm install --save-dev webpack $ touch ./src/index.js $ vim webpack.config.js // ./webpack.config.js const path = require('path'); module.exports = { entry : { 'index': ['./src/index.js'], }, output: { path : path.join(__dirname, 'dist'), filename: '[name].bundle.js' } ;
css-loader
装载css
安装依赖html
$ cnpm install --save-dev css-loader
添加style.css
node
$ vim ./src/style.css p {
} ```
引入style.css
webpack
// ./src/index.js var style = require("./style.css") console.log(style.toString())
打包并查看结果git
/* 2 */ /***/ (function(module, exports, __webpack_require__) { exports = module.exports = __webpack_require__(3)(false); exports.push([module.i, "p{color:red}", ""]); /***/ }), /* 3 */ /***/ (function(module, exports) { ... module.exports = function(useSourceMap) { var list = []; // return the list of modules as css string list.toString = function toString() { return this.map(function (item) { var content = cssWithMappingToString(item, useSourceMap); if(item[2]) { return "@media " + item[2] + "{" + content + "}"; } else { return content; } }).join(""); }; ....
能够看到,生成了两个新的模块,模块2包含咱们的style
文件中的内容,模块3导出了一个toString
,它的做用是能够将style.css
中的内容转化成字符串来使用,因此咱们在index.js
中能够使用style.toString()
来获得样式字符串,执行结果github
$ node ./dist/index.bundle.js p {
} ```
其余配置web
minimize
:压缩cssnpm
更多配置配置,请查阅webpack关于css-loader章节vim
style-loader
配合css-loader
插入安装依赖浏览器
$ cnpm install --save-dev css-loader
修改配置
{ test: /\.css$/, use : [ { loader : 'css-loader', options: {} }, { loader : 'css-loader', options: { minimize: true } } ] }
添加index.html
<html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./../dist/index.bundle.js"></script> </head> <body> <p>hello webpack</p> </body> </html>
浏览器打开./src/index.html
,能够看到咱们写的style.css
内容已经被插入到head
的style
标签中
其余更多配置请查阅webpack关于style-loader章节
file-loader
独立出style
文件安装依赖
$ cnpm install --save-dev file-loader
修改配置
{ loader : 'style-loader', options: {} }, { loader : 'file-loader', options: { name:'[name].[ext]' } },
打包并用浏览器打开./src/index.html
,能够看见,style.css
被以文件的形式导出并在head
中之外链的形式导入
其余更多配置查阅webpack关于style-loader章节
sass-loader
装载sass安装依赖
$ npm install sass-loader node-sass webpack --save-dev
修改配置
{ test: /\.(scss|css)$/, use : [{ loader: "style-loader" }, { loader: "css-loader" }, { loader: "sass-loader" }] }
打包并使用浏览器打开index.html
,能够看到,无论是style.css
仍是style.scss
都被style-loader
插入到了head
更多设置查阅关于webpack关于sass-loader章节
postcss-loder
搞定兼容性安装依赖
$ cnpm install --save-dev postcss-loader precss autoprefixer
添加配置
{ test: /\.(scss|css)$/, use : [ { loader: "style-loader" }, { loader: "css-loader" }, { loader: "postcss-loader" }, { loader: "sass-loader" }] }
添加postcss
配置
$ vim ./postcss.config.js // ./postcss.config.js const precss = require('precss'); const autoprefixer = require('autoprefixer'); module.exports = ({ file, options, env }) => ({ plugins: [precss, autoprefixer] })
打包并使用浏览器打开./src/index.html
,能够看到,自动给咱们添加了前缀。
其余更多配置请查阅webpack关于postcss-loder
章节