webpack

这篇博客详细介绍了Webpack的使用,包括安装、配置loader(如css-loader、style-loader、less-loader等)、配置vue相关、使用常见插件(BannerPlugin、HtmlWebpackPlugin、UglifyjsWebpackPlugin)以及设置本地服务器和配置文件抽离。通过实例展示了如何进行模块打包和优化。

指令集合

新项目命令
cnpm init 初始化,生成package.json
cnpm install 若package.json 需要依赖其他包 则需要cnpm install
cnpm install webpack@3.6.0 --save-dev 安装本地webpack
cnpm install --save-dev css-loader
cnpm install style-loader --save-dev
cnpm install --save-dev less-loader less
cnpm install --save-dev url-loader
cnpm install --save-dev file-loader
cnpm install --save-dev babel-loader@7 babel-core babel-preset-es2015
cnpm install vue --save //安装vue
cnpm install --save-dev vue-loader vue-template-compiler
在package.json中 将vue-loader 改为 “vue-loader”: “^13.0.0”,
然后 cnpm install 重新安装一下

notes:

将webpack 命令 与 npm run build 映射起来
webpack 命令可以替换成 npm run build 见package.json,详细配置 第8行,如下
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”,
“build” : “webpack” //一旦这样配置,会默认优先使用本地webpack,本地找不到会找全局的
},

Webpack 的使用

1, 使用webpack 的loader

需要哪些 loader可以在 https://www.webpackjs.com 查到,中文文档–LOADERS–样式

css-loader 只负责将css文件进行加载
styler-loader 只负责将样式加载到DOM,文件中

css

在main.js中依赖对应css文件 require("./css/normal.css") //这个是自己的css文件
安装cssloader,styler loader,
cnpm install --save-dev css-loader
cnpm install style-loader --save-dev
在webpack.config.js文件中进行配置 (配置见最后的配置文件)

less
1, 在main.js中依赖对应css文件 require("./css/special.less")
安装依赖
cnpm install --save-dev less-loader less

图片问题
cnpm install --save-dev url-loader
为了加载更大的文件,超过limit限制的文件
需要使用fileLoader 进行加载
cnpm install --save-dev file-loader

​ ES6 转成 ES5
​ 使用babel
​ 首先安装
​ cnpm install --save-dev babel-loader@7 babel-core babel-preset-es2015

2,webpack配置vue相关的东西

cnpm install vue --save
main.js导入
import Vue from “vue”

cnpm install --save-dev vue-loader vue-template-compiler
在使用中可能还会报错,是vueloader版本的问题,14版本之后使用vue-loader需要安装插件
在package.json中 将vue-loader 改为 “vue-loader”: “^13.0.0”,

3,使用常见webpack插件

webpack 插件对webpack 现有功能进行各种的扩展,如,打包优化,文件压缩等等

3.1 为打包的文件添加版权声明的插件 BannerPlugin

1,webpack.config.js 中添加
const webpack = require(“webpack”);
2,module.exports = {
…,
plugins:[
new webpack.BannerPlugin(‘最终版权归波儿霸和霸儿波所有’)
]
}

3.2 将html文件一块打包到dist文件夹中,直接发布的插件 HtmlWebpackPlugin

自动生成一个index.html文件,将打包的js文件,自动通过script标签插入到body中去

1,cnpm install html-webpack-plugin --save-dev
2,webpack.config.js 中添加
const htmlWebpackPlugin = require(“html-webpack-plugin”);
3,module.exports = {
…,
plugins:[

new htmlWebpackPlugin({
template : “index.html” //根据index.html文件生成
})
]
}

3.3 对打包的js文件进行压缩的插件 uglifyjs-webpack-plugin

1,cnpm install uglifyjs-webpack-plugin@1.1.1 --save-dev
2,webpack.config.js 中添加
3,module.exports = {
…,
plugins:[

new UglifyjsWebpackPlugin()
]
}

4,webpack安装本地服务器

非常方便,直接修改发布前代码,浏览器会做出相应改变。改变在内存中,最后只用发布一次就行了

1,安装
cnpm install webpack-dev-server@2.9.3 --save-dev
2,配置:
webpack.config.js 中

plugins:{…},
devServer:{
contentBase: “./dist”, //服务的目录
inline:true, //页面实施刷新
port:9999 //端口号
}
3,运行
package.json 中
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”,
“build”: “webpack”,
“dev”: “webpack-dev-server”
},
npm run dev

5,对配置文件进行抽离

有些东西,开发时需要,而发布时不需要,有些则反之
开发时需要build/base.config.js 和 build/dev.config.js
生产时需要build/base.config.js 和 build/prod.config.js

使用:
1,cnpm install webpack webpack-merge --save-dev
2,将东西进行分离
3,package.json 中 修改成
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”,
“build”: “webpack --config ./build/prod.config.js”,
“dev”: “webpack-dev-server --open --config ./build/dev.config.js”
}
4,base.config.js文件中,修改path
module.exports = {

output: { //配置出口
path: path.resolve(__dirname, “…/dist”), //_dirname 表示当前目录
filename: “bundle.js”,
},

注意:

当使用npm run dev的时候使用的开发环境,

当使用npm run build的时候使用的是生产环境

配置文件

webpack.config.js

const path = require("path"); // 
const webpack = require("webpack");
const htmlWebpackPlugin = require("html-webpack-plugin");
const UglifyjsWebpackPlugin = require("uglifyjs-webpack-plugin");

module.exports = {
	entry: "./src/main.js", //配置入口
	output: { //配置出口
		path: path.resolve(__dirname, "dist"), //_dirname 表示当前目录
		filename: "bundle.js",
		// publicPath: "dist/" //设置发布路径
	},
	module: {
		rules: [{
				test: /\.(less|css)$/,
				//使用多个loader时,是从右向左
				use: [{
						loader: "style-loader" // creates style nodes from JS strings
					},
					{
						loader: "css-loader" // translates CSS into CommonJS
					}, {
						loader: "less-loader" // compiles Less to CSS
					}
				]
			}, {
				test: /\.(png|jpg|gif)$/,
				use: [{
					loader: 'url-loader',
					options: {
						limit: 1024 * 10,
						name: "img/[name].[hash:8].[ext]", //加载出来的图片所在dist中的位置
						//name代表原文件名,ext代表原扩展名
					}
				}]
			},
			{
				test: /\.js$/,
				exclude: /(node_modules|bower_components)/,
				use: {
					loader: 'babel-loader',
					options: {
						presets: ['es2015']
					}
				}
			},
			{
				test:/\.vue$/,
				use: {
					loader: 'vue-loader'
				}
			}
		]
		
	},
	resolve:{
		alias:{
			'vue$':'vue/dist/vue.esm.js'
		}
	},
	plugins:[
		new webpack.BannerPlugin('最终版权归波儿霸和霸儿波所有'),
		new htmlWebpackPlugin({
			template : "index.html"
		}),
		new UglifyjsWebpackPlugin()
	],
	devServer:{
		contentBase: "./dist",
		inline:true, //页面实施刷新
		port:9999
	} 
}

抽离后

base.config.js

const path = require("path"); // 
const webpack = require("webpack");
const htmlWebpackPlugin = require("html-webpack-plugin");
const UglifyjsWebpackPlugin = require("uglifyjs-webpack-plugin");
module.exports = {
	entry: "./src/main.js", //配置入口
	output: { //配置出口
		path: path.resolve(__dirname, "../dist"), //_dirname 表示当前目录
		filename: "bundle.js",
		// publicPath: "dist/" //设置发布路径
	},
	module: {
		rules: [{
				test: /\.(less|css)$/,
				//使用多个loader时,是从右向左
				use: [{
						loader: "style-loader" // creates style nodes from JS strings
					},
					{
						loader: "css-loader" // translates CSS into CommonJS
					}, {
						loader: "less-loader" // compiles Less to CSS
					}
				]
			}, {
				test: /\.(png|jpg|gif)$/,
				use: [{
					loader: 'url-loader',
					options: {
						limit: 1024 * 10,
						name: "img/[name].[hash:8].[ext]", //加载出来的图片所在dist中的位置
						//name代表原文件名,ext代表原扩展名
					}
				}]
			},
			{
				test: /\.js$/,
				exclude: /(node_modules|bower_components)/,
				use: {
					loader: 'babel-loader',
					options: {
						presets: ['es2015']
					}
				}
			},
			{
				test:/\.vue$/,
				use: {
					loader: 'vue-loader'
				}
			}
		]
	},
	resolve:{
		alias:{
			'vue$':'vue/dist/vue.esm.js'
		}
	},
	plugins:[
		new webpack.BannerPlugin('最终版权归波儿霸和霸儿波所有'),
		new htmlWebpackPlugin({
			template : "index.html"
		}),
	]
}

dev.config.js

//开发时依赖需要这些东西
const WebpackMerge = require("webpack-merge")
const baseConfig = require('./base.config.js')
module.exports = new WebpackMerge(baseConfig, {
	devServer:{
		contentBase: "./dist",
		inline:true, //页面实施刷新
		port:9999
	} 
})

prod.config.js

//生产时需要的
const UglifyjsWebpackPlugin = require("uglifyjs-webpack-plugin");
const WebpackMerge = require("webpack-merge")
const baseConfig = require('./base.config.js')
module.exports = new WebpackMerge(baseConfig, {
	plugins:[
		new UglifyjsWebpackPlugin()
	]
})
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值