webpack3 服务器loader配置-解析json和es6语法

module
	loaders;新版本改名为use
	作用:例如将es6->es5 jsx->js less->css

	1、在webpack.config.js文件中配置loaders
		(1)test: 一个匹配loader要做操作的文件的一个正则表达式
		(2)loader:loader要执行的任务的名字
		(3)options:为loader提供一些外部选型配置
	
	2、json格式的数据转换成js对象(测试使用)
		当前只是为了测试webpack 3.x版本,当前版本已经集成了json-loader;不需要再安装了

		(1)安装json-loader
			cnpm install -D json-loader
			在json文件编写json数据,require引入模块时,原文件不需暴露直接使用,因为json格式文件不能写js导出代码

		(2)、在webpack.config.js文件中添加配置
			module:{
				rules:[{
					test:/\.json$/,
					use:"json-loader"
				}
			]
		}
	3、将es6->es5
		(1)安装依赖
			cnpm install -D babel-core babel-loader@7.1.5 babel-preset-es2015
			若安装babel-loader至最新版,会和babel-core不兼容
		(2)配置webpack.config.js文件
			在module的rules[]内添加
			  {
			  	test:/\.js$/,
			  	use:"babel-loader"
			  }

代码示例:
webpack.config.js文件代码示例:

module.exports={
	entry:__dirname+"/src/app.js",
	output:{
		path:__dirname+'/dist',
		filename:'boundle.js'
	},

	module:{
			rules:[{
					test:/\.json$/,
					use:"json-loader"
				},
				{
					test:/\.js$/,
					use:"babel-loader"
				}
			]
		}
}

package.json文件:

{
  "name": "code",
  "version": "1.0.0",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-es2015": "^6.24.1",
    "json-loader": "^0.5.7",
    "webpack": "^3.8.1",
    "webpack-dev-server": "^2.9.7"
  },
  "scripts": {
    "build": "webpack",
    "dev": "webpack-dev-server --content-base dist --inline"
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值