上文介绍了webpack入门,本文将详细介绍webpack实用配置javascript
以entry.js打包为bundle.js为例,出口的filename能够设置为[id]、[name]、[hash]、[chunkhash]等替换形式,以下所示css
var webpack = require('webpack'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: '[id]-[name]-[hash].js'//出口名称 } }
则出口文件为0-main-0c1dce21f6c5db455fb4.jshtml
若是index.html要引用打包后的js文件,因为文件名称不肯定,并很差解决。这时,就须要使用html-webpack-plugin插件。该插件并非内置插件,因此须要安装java
npm install html-webpack-plugin
HtmlWebpackPlugin简化了HTML文件的建立,以便为webpack包提供服务。这对于在文件名中包含每次会随着变异会发生变化的哈希的webpack bundle尤为有用node
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: '[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ title: 'match',//生成的html文件的标题为'match' filename: 'index.html'//生成的html文件名称为'index.html' }) ] }
经过以上的配置,若是在当前路径,index.html不存在,则生成;若是存在,则替换jquery
[注意]若是htmlwebpackplugin不进行配置,参数为空, plugins: [new HtmlWebpackPlugin()]。默认地,生成的html文件名称为'index.html',标题为'Webpack APP' webpack
【标签位置】web
htmlwebpackplugin插件的经常使用设置是设置script标签插入的位置,默认插入到body标签中,但可使用inject:'head',设置插入到head标签中正则表达式
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ inject:'head',//将script标签插入到head标签中 filename: 'index-[hash].html',//生成的html文件名称为'index.html' }) ] }
【图标设置】npm
设置favicon属性,能够设置网页的小图标
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ favicon:'./icon.ico' }) ] }
【压缩】
设置minify属性,能够压缩html文件,默认为false,即不压缩
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ minify:{ removeComments: true,//删除注释 collapseWhitespace:true//删除空格 } }) ] }
使用webpack打包后的index.html代码以下
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Webpack App</title></head><body><script type="text/javascript" src="js/0-main-8128c0c26a4449da7a05.js"></script></body></html>
HtmlWebpackPlugin除了提供模块版本号的功能,还可使用模板文件
例如,模板文件为template.html,内容以下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>template</title> </head> <body> <script src="test.js"></script> <div>test</div> </body> </html>
webpack配置文件以下
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ filename: 'index-[hash].html',//生成的html文件名称为'index.html' template:'template/template.html'//模板文件为'template.html' }) ] }
生成的index-[hash].html以'template.html'文件为模板
[注意]若是在htmlwebpackplugin中使用了模板,则指定title不会生效,由于要以模板的title为准
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ title:'test', filename: 'index-[hash].html',//生成的html文件名称为'index.html' template:'template/template.html'//模板文件为'template.html' }) ] }
【传参】
模块文件固然是能够传参的,通常地,使用ejs语法。例如,在模板文件中,使用<%= htmlWebpackPlugin.options.title %>,便可读取htmlWebpackPlugin插件中'title'属性的值
[注意]模板文件中的'htmlWebpackPlugin'是固定的,不能随意更改。与webpack.config.js文件中,require()该插件的命名无关
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ title:'test', template:'template/template.html',//模板文件为'template.html' dateData: new Date() }) ] } //template.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><%= htmlWebpackPlugin.options.title %></title> </head> <body> <div><%=htmlWebpackPlugin.options.dateData %></div> </body> </html>
【模板组件】
下面利用模板组件组合成一个html文件,以ejs模板语言为例
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ template:'template/template.html'}) ] } //template.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <% include template/header.html %> </div> <ul> <% var arr = [1,2,3,4,5] %> <% for(var i = 0; i < arr.length; i++){ %> <li><%=arr[i] %></li> <% } %> </ul> <div> <% include template/footer.html %> </div> </body> </html> //header.html <div>我是头部</div> //footer.html <div>我是尾部</div>
运行结果报错,提示子模板加载失败
这是由于HtmlWebpackPlugin插件并不具有ejs模板语言全部的功能,其中一个就是不能识别<%include %>语句,这时须要安装一个ejs-compiled-loader
npm install ejs-compiled-loader
安装完成后,修改配置文件以下,表示使用ejs-compiled-loader来编译template.html
[注意]该插件中的include路径相对于webpack配置文件的位置,而不是模板文件template.html的位置
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ template:'ejs-compiled-loader!template/template.html'}) ] }
结果以下
对于多页面来讲,通常地,有多个入口文件。不一样的html页面输出对应不一样的入口文件。 插件plugins()是一个数组,每new一个HtmlWebpackPlugin(),就能够输出一个html页面。这里有两个重要的属性:chunks和excludeChunks,chunks表示所包含的入口文件,excludeChunks表示要排除的入口文件
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: { a:'./src/js/a.js', b:'./src/js/b.js', c:'./src/js/c.js' }, output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ filename:'a.html', template:'src/template/template.html', title:'this is a', chunks:['a'] }), new HtmlWebpackPlugin({ filename:'b.html', template:'src/template/template.html', title:'this is b', chunks:['b'] }), new HtmlWebpackPlugin({ filename:'c.html', template:'src/template/template.html', title:'this is c', excludeChunks:['a','b'] }), ] }
结果以下
//a.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>this is a</title> </head> <body> <div></div> <script type="text/javascript" src="js/2-a-9828ea84bd8c12c19b5f.js"></script></body> </html> //b.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>this is b</title> </head> <body> <div></div> <script type="text/javascript" src="js/1-b-9828ea84bd8c12c19b5f.js"></script></body> </html> //c.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>this is c</title> </head> <body> <div></div> <script type="text/javascript" src="js/0-c-9828ea84bd8c12c19b5f.js"></script></body> </html>
在前面的例子中,都是以连接的形式引入入口文件的。有时,为了追求性能,会将其处理为内联的形式。这里就须要安装一个扩展插件html-webpack-inline-source-plugin,专门用来处理入口文件内联的
$ npm install --save-dev html-webpack-inline-source-plugin
该插件的使用很简单,使用require()语句引入后,在插件plugins()新建一个html-webpack-inline-source-plugin对象,而后在html-webpack-plugin对象中添加inlineSource属性便可
inlineSource: '.(js|css)$' // embed all javascript and css inline
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); module.exports = { entry: './entry.js', output:{ path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, plugins: [ new HtmlWebpackPlugin({ inlineSource: '.(js|css)$' }), new HtmlWebpackInlineSourcePlugin() ] }
结果以下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Webpack App</title> </head> <body> <script type="text/javascript">/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { document.write('It works.') /***/ }) /******/ ]);</script></body> </html>
下面使用babel来进行es最新标准的代码向es5代码的转换,首先须要安装babel核心程序,及babel-loader
npm install babel-loader babel-core
在使用babel-loader进行代码转换以前,要先了解到ecmascript标准变化很快,且浏览器支持状况不一样。因此,出现了'es2015'、'es2016'、'es2017'、'latest'、'env(new)'等多个不一样的标准。这时,要须要来选择从哪一个标准进行转换,须要安装插件babel-preset-env
npm install babel-preset-env
在 webpack 配置对象中,须要添加 babel-loader 到 module 的 loaders 列表中。webpack的配置文件以下所示
const path = require('path');
module.exports = { entry:{ app:'./src/app.js', }, output:{ path:path.resolve(__dirname,'src'), filename: '[name].bundle.js' }, module: { rules: [{ test: /\.js$/, use: { loader: 'babel-loader', options: { presets: ['env'] } } }] }, }
关于path有两种写法,除了上面的配置文件的写法外,另外一种写法以下所示。可是,尽可能不要使用__dirname + '/src'的写法,在某些参数中,该写法没法生效
path: __dirname + "/src"
在命令行中运行webpack命令进行打包,打包过程以下
打包前的文件为app.js,内容以下
() => {
return a + b; }; Array.from('1,2,3'); new Set;
打包后的文件为app.bundle.js,主要内容以下
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) { "use strict"; (function () { return a + b; }); Array.from('1,2,3'); new Set(); /***/ }) /******/ ]);
通过babel转换后的js文件存在两个问题:
一、打包速度较慢
二、部分ES2017的新语法没有转换为ES5的代码
下面对这两个问题分别进行处理
【打包速度】
loader的test属性表示该loader必须知足的条件,上面代码中使用/\.js$/ 来匹配,也许会去编译 node_modules 目录或者其余不须要的源代码。这样会大大增长webpack的编译时间
要排除 node_modules,就要使用 loaders 配置的 exclude 选项,表示哪些除外,exclude:/node_modules/
module.exports = {
entry:{
app:'./src/app.js', }, output:{ path:__dirname+'/src/', filename: '[name].bundle.js' }, module: { rules: [{ test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['env'] } } }] }, }
[注意]exclude除了支持正则表达式外,还支持字符串形式,写法以下。若是用__dirname +'/node_modules'的形式则不会生效
const path = require('path');
exclude:path.resolve(__dirname, "node_modules")
打包过程以下
当node-modules文件部分较大时,速度提高会更明显
除了exclude选项,还可使用include选项,可以明确被打包的文件时,使用include将使打包速度更快
module.exports = {
entry:{
app:'./src/app.js', }, output:{ path:__dirname+'/src/', filename: '[name].bundle.js' }, module: { rules: [{ test: /\.js$/, include: /src/, use: { loader: 'babel-loader', options: { presets: ['env'] } } }] }, }
[注意]include的另外一种写法以下所示
const path = require('path');
include: path.resolve(__dirname, 'src')
打包过程以下
耗费时间有所减少
cacheDirectory选项值默认为false,当为true时,指定的目录将用来缓存 loader 的执行结果。以后的 webpack 构建,将会尝试读取缓存,来避免在每次执行时,可能产生的、高性能消耗的 Babel 从新编译过程
const path = require('path');
module.exports = { entry: { app: './src/app.js', }, output: { path: path.resolve(__dirname, 'src'), filename: '[name].bundle.js' }, module: { rules: [{ test: /\.js$/, include: path.resolve(__dirname, 'src'), use: { loader: 'babel-loader', options: { presets: ['env'], cacheDirectory:true } } }] }, }
耗费时间减小了100ms,效果很好
解决了babel编译速度后,下面来解决ES新语法不被转换的问题
【babel-polyfill】
Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,好比Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise等全局对象,以及一些定义在全局对象上的方法(好比Object.assign
)都不会转码。
举例来讲,ES6在Array
对象上新增了Array.from
方法。Babel就不会转码这个方法。若是想让这个方法运行,必须使用babel-polyfill。
babel-polyfill是一个全局垫片,为开发应用准备的
npm install babel-polyfill
在脚本头部加入下面代码便可使用
import 'babel-polyfill';
app.js的文件内容以下
import 'babel-polyfill';
() => { return a + b; }; Array.from('1,2,3'); new Set;
由下图所示,转换后的文件大小超过了200kb
【babel-plugin-transform-runtime】
至关于babel-polyfill来讲,babel-plugin-transform-runtime更加适用,它是一个局部垫片,为开发框架准备
npm install babel-plugin-transform-runtime babel-runtime
app.js文件以下所示
() => {
return a + b; }; Array.from('1,2,3'); new Set;
配置文件以下所示
const path = require('path');
module.exports = { entry: { app: './src/app.js', }, output: { path: path.resolve(__dirname, 'src'), filename: '[name].bundle.js' }, module: { rules: [{ test: /\.js$/, include: path.resolve(__dirname, 'src'), use: { loader: 'babel-loader', options: { presets: ['env'], cacheDirectory:true, plugins: ['transform-runtime'] } } }] }, }
转换过程以下所示
转换后的文件app.bundle.js主要内容以下所示
(function () {
return a + b; }); (0, _from2.default)('1,2,3'); new _set2.default();
在webpack入门博文中由介绍过CSS插件的简单使用,接下来将详细介绍
首先,要安装css-loader和style-loader,css-loader用于读取并加载css文件,style-loader将它插入到页面中
[特别注意]在处理css时,最好不要使用include、exclude等属性。include、exclude属性是加快babel转换速度的,和css没什么关系,并且会添乱
npm install css-loader style-loader
//app.js require('./css/common.css'); //common.css body{margin: 0;background-color: red} //webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/app.js', output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', 'css-loader' ] } ] }, plugins: [ new HtmlWebpackPlugin({}) ] }
效果以下
【自动前缀】
页面加载CSS每每并不像上面的状况这么简单,须要处理不少问题,其中一个就是浏览器前缀问题。对于某些属性来讲,好比transform,不一样浏览器的版本对其支持程度不一样,浏览器前缀也不一样。这时,就须要可以根据实际状况,自动增长前缀,而postcss-loader就是这样的工具,并且功能要强大的多
首先,先安装postcss-loader
npm install postcss-loader
而后,安装postcss的自动前缀的插件autoprefixer
npm install autoprefixer
配置以下
//common.css body{transform: scale(0);background-color: red} //app.js require('./css/common.css'); //webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/app.js', output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', 'css-loader', { loader: 'postcss-loader', options: {plugins: [require('autoprefixer')]} } ] } ] }, plugins: [ new HtmlWebpackPlugin({}) ] }
结果以下
若是css文件中出现@import,则有两种处理方式,一种是将postcss文件单独写成配置文件postcss.config.js
//common.css @import './flex.css'; body{transform: scale(0);background-color: red} //flex.css body{display:flex;} //app.js require('./css/common.css'); //webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/app.js', output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', { loader: 'css-loader', options: {importLoaders: 1} }, 'postcss-loader' ] } ] }, plugins: [ new HtmlWebpackPlugin({}) ] } //postcss.config.js module.exports = { plugins:[require('autoprefixer')] }
结果以下
另外一种须要安装postcss-import插件
npm install postcss-import
//common.css @import './flex.css'; body{transform: scale(0);background-color: red} //flex.css body{display:flex;} //app.js require('./css/common.css'); //webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/app.js', output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', { loader: 'css-loader', options: {importLoaders: 1 } }, { loader: 'postcss-loader', options: {plugins: [ require('postcss-import'), require('autoprefixer') ] } } ] } ] }, plugins: [ new HtmlWebpackPlugin({}) ] }
结果以下
【sass】
首先,须要安装sass-loader及node-sass
[注意]关于node-sass安装的问题移步至此
npm install sass-loader node-sass
因为sass-loader中已经自带了关于@import处理的问题。因此,不须要css-loader及postcss-loader的额外处理
//layer.scss @import './flex.scss'; body{ background-color:green; div{ width: 400px; } } //flex.scss .flex{display:flex;} //app.js require('./components/layer/layer.scss'); //webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/app.js', output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.scss$/, use:[ 'style-loader', 'css-loader', { loader: 'postcss-loader', options: {plugins: [require('autoprefixer')]} }, 'sass-loader' ] } ] }, plugins: [ new HtmlWebpackPlugin({}) ] }
结果以下
【分离CSS】
默认地,CSS做为模块资源被打包到入口js文件中。有时,须要把CSS文件分离出来,这时就须要用到extract-text-webpack-plugin插件
npm install extract-text-webpack-plugin
该插件的配置以下
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = { entry: './src/app.js', output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.scss$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use:[ 'css-loader', { loader: 'postcss-loader', options: {plugins: [require('autoprefixer')]} }, 'sass-loader' ] }) } ] }, plugins: [ new HtmlWebpackPlugin({}), new ExtractTextPlugin("styles.css") ] }
结果以下,该插件将入口文件中引用的 *.css,移动到独立分离的 CSS 文件。所以,你的样式将再也不内嵌到 JS bundle 中,而是会放到一个单独的 CSS 文件(即 styles.css)当中。 若是样式文件大小较大,这会作更快提早加载,由于 CSS bundle 会跟 JS bundle 并行加载
webpack在处理图片、音乐、电影等资源文件时,须要使用file-loader
npm install file-loader
默认状况下,使用file-loader生成的文件的文件名就是文件内容的MD5哈希值并保留原始扩展名
file-loader的配置项以下所示
name [hash].[ext] 为文件配置自定义文件名模板 context this.options.context 配置自定义文件 context,默认为 webpack.config.js context publicPath __webpack_public_path__ 为文件配置自定义 public 发布目录 outputPath 'undefined' 为文件配置自定义 output 输出目录 useRelativePath false 若是但愿为每一个文件生成一个相对 url 的 context 时,应该将其设置为 true emitFile true 默认状况下会生成文件,能够经过将此项设置为 false 来禁止(例如,使用了服务端的 packages)
以引入图片资源例,有如下几种状况
一、经过css文件的background属性引入
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', 'css-loader' ] }, { test:/\.(png|jpg|gif|svg)$/i, use:'file-loader' } ] }, plugins: [ new HtmlWebpackPlugin() ] } //entry.js require('./src/css/common.css'); //common.css body{background: url('../img/eg_bulbon.gif')}
结果以下
二、经过模板html文件img标签引入,这时须要使用${require('')}将相对路径包裹一次
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname,//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', 'css-loader' ] }, { test:/\.(png|jpg|gif|svg)$/i, use:'file-loader' } ] }, plugins: [ new HtmlWebpackPlugin({ template:'template/template.html' }) ] } //template.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <img src="${require('../src/img/eg_bulbon.gif')}" alt=""> </body> </html>
结果以下
三、若模板使用ejs-compiled-loader插件,则没法使用${require('')}语句,须要使用HtmlWebpackPlugin传参来构造绝对路径
//webpack.config.js var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './entry.js', //入口文件 output: { path: __dirname + '/dist',//出口路径 filename: 'js/[id]-[name]-[hash].js'//出口名称 }, module:{ rules:[ { test:/\.css$/, use:[ 'style-loader', 'css-loader' ] }, { test:/\.(png|jpg|gif|svg)$/i, use:'file-loader' } ] }, plugins: [ new HtmlWebpackPlugin({ template:'ejs-compiled-loader!template/template.html', file:__dirname }) ] } //template.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <% include template/header.html %> </div> </body> </html> //header.html <img src="<%=htmlWebpackPlugin.options.file%>\src\img\eg_bulbon.gif" alt="">
结果以下
【file-loader参数】
文件名模板占位符有以下几种
[ext] 资源扩展名 [name] 资源的基本名称 [path] 资源相对于 context 查询参数或者配置的路径 [hash] 内容的哈希值,默认为十六进制编码的 md5 [<hashType>:hash:<digestType>:<length>] 可选配置 其余的 hashType, 即 sha1, md5, sha256, sha512 其余的 digestType, 即 hex, base26, base32, base36, base49, base52, base58, base62, base64 length 字符的长度 [N] 当前文件名按照查询参数 regExp 匹配后得到到第 N 个匹配结果
{ test:/\.(png|jpg|gif|svg)$/i, use:[{ loader:'file-loader', options: { name:'[name]-[hash:5].[ext]' } }] }
或者
{ test:/\.(png|jpg|gif|svg)$/i, use:['file-loader?name=[name]-[hash:5].[ext]'] }
结果以下
【url-loader】
url-loader功能相似于file-loader,可是在文件大小(单位byte)低于指定的限制时,能够返回一个dataURL
能够经过传递查询参数(query parameter)来指定限制(默认为不限制)
若是文件大小超过限制,将转为使用 file-loader,全部的查询参数也会传过去
npm install url-loader
图片的大小为1.1kb,下面将限制设置为2000,则图片将以base64格式传递
{ test:/\.(png|jpg|gif|svg)$/i, use:['url-loader?limit=2000'] }
结果以下
若是将限制大小设置为1000,图片以src的形式传递
{ test:/\.(png|jpg|gif|svg)$/i, use:[{ loader:'url-loader', options: { limit:1000, name:'[name]-[hash:5].[ext]' } }] }
【image-webpack-loader】
使用image-webpack-loader来压缩图片
npm install image-webpack-loader
image-webpack-loader的配置项以下
options: {
mozjpeg: {
progressive: true, quality: 65 }, optipng: { enabled: false, }, pngquant: { quality: 80, speed: 4 }, gifsicle: { interlaced: false, }, webp: { quality: 75 } }
插件一张大小为4.1kb的名称为'm.jpg'的图片,配置以下
{ test:/\.(png|jpg|gif|svg)$/i, use:[ 'url-loader?limit=1000&name=[name]-[hash:5].[ext]', 'image-webpack-loader' ] }
结果以下所示,生成大小为3.28kb,名称为'm-c7083.jpg'的图片
【雪碧图】
在webpack中自动生成雪碧图,须要使用postcss-sprits插件
npm install postcss-sprites
配置很是简单
"plugins": { "postcss-sprites": { spritePath: 'dist/assets/imgs/sprites/' } } }
若是是加载的远程CDN库,则在HTML文件内直接使用script标签引入便可
<script src="https://cdn.bootcss.com/jquery/3.3.1/core.js"></script>
这样,在文件中能够直接使用jQuery
若是jQuery是经过npm保存到本地,则须要使用ProvidePlugin插件来自动加载模块,而没必要处处 import
或 require
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' })
而后在咱们任意源码中:
// in a module $('#item'); // <= 起做用 jQuery('#item'); // <= 起做用 // $ 自动被设置为 "jquery" 输出的内容
若是jQuery是保存在一个自定义的目录中的,则须要还须要设置别名
resolve:{ alias:{ jquery$:path.resolve(__dirname,'src/libs/jquery.min.js') } }
除了使用providePlugin,还可使用imports-loader
module: { rules: [ { test: path.resolve(__dirname,"src/app.js"), use: [ loader: 'imports-loader', options: {$:'jquery'} ] } ] }
使用webpack-dev-server的proxy功能,能够代理远程接口。实际上,它使用的是http-proxy-middleware插件
经常使用参数以下
target:代理指向的地址
changeOrigin:改变源URL(默认false)
headers:设置http请求头
pathRewrite:重定向接口请求
logLevel:控制台显示信息
在 localhost:3000
上有后端服务的话,能够这样启用代理:
proxy: { "/api": "http://localhost:3000" }
若是服务器给出500错误,则须要添加changeOrigin
proxy: { "/api": { target: "http://localhost:3000", changeOrigin: true } }
下面将使用webpack搭建一个实用的开发环境
var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = { entry: './src/app.js',//入口文件 output:{ path: __dirname,//出口路径 filename: 'js/[name].bundle.js'//出口名称 }, module:{ rules:[ { test:/\.scss$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use:[ 'css-loader', { loader: 'postcss-loader', //自动添加前缀 options: {plugins: [require('autoprefixer')]} }, 'sass-loader' ] }) }, { test:/\.js$/, include:/\.\/src/, use:{ loader: 'babel-loader', //将最新标准的js代码翻译为es5代码 options:{presets: ['env']} } }, { test:/\.(png|jpg|gif|svg)$/i, use:[ //当图片大小大于1000byte时,以[name]-[hash:5].[ext]的形式输出 //当图片大小小于1000byte时,以baseURL的形式输出 'url-loader?limit=1000&name=[name]-[hash:5].[ext]', //压缩图片 'image-webpack-loader' ] } ] }, plugins: [ //使用模板生成html文件 new HtmlWebpackPlugin({template:'ejs-compiled-loader!template/template.html'}), //分离出css到style.css new ExtractTextPlugin("style.css") ] }