上一章講的是Dll
,這一章講的是ExtractTextWebpackPlugin
,依舊是沒有任何關係。javascript
這個插件用來將css
導出到單獨文件css
此次涉及到loader
的使用,能夠暫時忽略這方面配置html
初始化項目java
$ mkdir 0x008-extract-text-webpack-plugin $ cd 0x008-extract-text-webpack-plugin $ cnpm init -y $ cnpm install webpack css-loader style-loader extract-text-webpack-plugin --save-dev
配置webpack
,先不使用插件webpack
$ vim webpack.config.js // webpack.config.js const path = require('path'); const webpack = require('webpack'); module.exports = { entry : { 'index': ['./src/index.js'] }, output : { path : path.join(__dirname, 'dist'), filename: '[name].bundle.js' }, module : { rules: [ { test: /\.css$/, use:[ { loader: "style-loader" }, { loader: "css-loader" } ] } ] } };
添加入口文件和樣式文件git
$ vim ./src/index.js // ./src/index.js require('./../style2.css') $ vim style1.css p{ color: red; } $ vim style2.css p{ font-size: 30px; }
打包並查看打包結果index.bundle.js
,能夠發現,css
被打包到了js
裏面,以字符串的形式存在,而且整個index.bundle.js
比日常打了很多。github
// module exports.push([module.i, "p{\n color: red;\n}", ""]); // exports
此時若是有html
引用該js
web
$ vim ./src/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="./../dist/index.bundle.js"></script> </head> <body> <p>text</p> </body> </html>
5 瀏覽器打開index.html
,就會發現css
以style
的形式被插到了head
npm
修改配置vim
const path = require('path'); const webpack = require('webpack'); const ExtractTextPlugin = require("extract-text-webpack-plugin"); module.exports = { entry : { 'index': ['./src/index.js'] }, output : { path : path.join(__dirname, 'dist'), filename: '[name].bundle.js' }, module : { rules: [ { test: /\.css$/, // use:[ // { loader: "style-loader" }, // { loader: "css-loader" } // ] use : ExtractTextPlugin.extract({ fallback: "style-loader", use : "css-loader" }) } ] }, plugins: [ new ExtractTextPlugin("styles.css"), ] };
打包並查看dist
,能夠發現,index.bundle.js
文件恢復了正常,而且多出來一個style.css
文件。
$ webpack // ./dist/style.css p{ color: red; }p{ font-size: 30px; }
HtmlWebpackPlugin
插件自動插入index.html
修改配置
const path = require('path'); const webpack = require('webpack'); const ExtractTextPlugin = require("extract-text-webpack-plugin"); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry : { 'index': ['./src/index.js'] }, output : { path : path.join(__dirname, 'dist'), filename: '[name].bundle.js' }, module : { rules: [ { test: /\.css$/, // use:[ // { loader: "style-loader" }, // { loader: "css-loader" } // ] use : ExtractTextPlugin.extract({ fallback: "style-loader", use : "css-loader" }) } ] }, plugins: [ new ExtractTextPlugin("styles.css"), new HtmlWebpackPlugin({ title : 'extract-text-webpack-plugin', filename: 'index.html', template: path.resolve(__dirname, 'src/index.html'), inject : 'head' }) ] };
打包並查看./dist/index.html
,它以link
的形式被插入頭部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link href="styles.css" rel="stylesheet"> <script type="text/javascript" src="index.bundle.js"></script> </head> <body><p>text</p></body> </html>
添加入口
entry : { 'index': ['./src/index.js'], 'index2': ['./src/index2.js'] }
修改插件命名方式
new ExtractTextPlugin("[name].css"),
打包並查看dist
$ ls ./dist index.bundle.js index.css index.html index2.bundle.js index2.css