[Javascript] Webpack Loaders, Source Maps, and ES6
Using ES6
To use ES6, we need loader.
- Modify webpack.config.js file:
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: __dirname
},
module: {
loaders: [
{test: /\.js$/, loader: 'babel', exclude: '/node_modules/'}
]
}
};
We add module property, which is an object. We define the loaders property here which is an array of objects.
- test: It is a regex, will include all the files which match the regex. In the exmaple, we say that "include all the js files".
- loader: You need to define the ES6 loader for this, which is 'babel'. For that, you need to install it by:
npm install babel-loader
- exclude: Tell which files you don't want to include.
2. Change file using ES6 style:
//lam-dom-binding.js
export default {
bindEls(el1, el2){
el1.addEventListener('keyup', () => el2.value = el1.value)
el2.addEventListener('keyup', () => el1.value = el2.value)
}
}
3. run: webpack --watch
Source map
One useful thing is see the source map (means see lam-dom-binding.js map to which part in the bundle.js). The reason it is useful is because when we open the devtool, we just saw a big bundle.js file:

which is not useful for debugging.
To enable the source map, we need to add one property in webpack.config.js:
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: __dirname
},
devtool: 'eval', //init compile fast, recompile also very fast
module: {
loaders: [
{test: /\.js$/, loader: 'babel', exclude: '/node_modules/'}
]
}
};
After that, run 'webpack --watch' again. We can see from the devtool, it show the 'webpack://'

But you can see that code is still ES5 style, if you want what you coded, instead of using 'eval', you can use 'eval-source-map'.
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: __dirname
},
devtool: 'eval-source-map',
module: {
loaders: [
{test: /\.js$/, loader: 'babel', exclude: '/node_modules/'}
]
}
};
This can a little bit longer time to compile, then you can see the code you just worte:

Both 'eval' and 'eval-source-map' are recommended using in dev time.
If you want to use in production code:
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: __dirname
},
devtool: 'source-map',
module: {
loaders: [
{test: /\.js$/, loader: 'babel', exclude: '/node_modules/'}
]
}
};
It will add a .map file for you. That file is not actually loaded into the browser until the DevTools are brought up.
[Javascript] Webpack Loaders, Source Maps, and ES6的更多相关文章
- [AngualrJS + Webpack] Production Source Maps
When you uglify your Angular code with Webpack's uglify plugin, debugging your application can be a ...
- JavaScript Source Maps浅析
阅读目录 有用的链接 Link: 原文链接 译文开始: 对网站进行性能优化对一个最容易的方法就是把JS和CSS进行打包压缩.但是当你需要调试这些压缩文件中的代码的时候,会发生什么?可能会是一场噩梦.但 ...
- Source Maps简介
提高网站性能最简单的方式之一是合并压缩JavaScript和CSS文件.但是当你需要调试这些压缩文件中的代码时,那将会是一场噩梦.不过也不用担心,souce maps将会帮你解决这一问题. Sourc ...
- 【译】Source Maps浅析
Time:2019/10/27~2019/10/29 Link: 原文链接 译文开始: 对网站进行性能优化对一个最容易的方法就是把JS和CSS进行打包压缩.但是当你需要调试这些压缩文件中的代码的时候, ...
- Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT Source Maps 详解
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
- Introduction to JavaScript Source Maps
下载jquery时候发现:jquery.min.map 这什么鬼呀? https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/core.js http ...
- [转] Webpack的devtool和source maps
source maps Webpack打包生成的.map后缀文件,使得我们的开发调试更加方便,它能帮助我们链接到断点对应的源代码的位置进行调试(//# souceURL),而devtool就是用来指定 ...
- WebStorm 9 自动编译 LESS 产出 CSS 和 source maps
1.双击桌面Chrome图标,打开Chrome,按键盘“F12”键,打开开发工具界面,点击其右上角的“设置”按钮,勾选“Enable JavaScript source maps” 及“Enable ...
- 前端构建:Source Maps详解
一.前言 当使用CoffeeScript.ClojureScript编写前端脚本时,当使用Less.Sacc编写样式规则时,是否觉得调试时无法准确找到源码位置呢?当使用jquery.min.js等经压 ...
随机推荐
- freemarker得到数组的长度
取得list的长度:${fields?size}.用?size不是用?length,代码如下所示: <#list properties as item> <#assign layer ...
- 第一个MVC模式的程序
数据库 是一个SQL sever数据库,结构很简单,创建名为firstMVC的数据库,只包含一个数据表(名称为Persons),其中共有三列,分别用于保存人员(persons)的ID.姓名以及创建日期 ...
- java jvm学习笔记九(策略文件)
欢迎装载请说明出处:http://blog.csdn.net/yfqnihao/article/details/8271407 课程源码:http://download.csdn.net/detail ...
- 2014 多校联合训练赛6 Fighting the Landlords
本场比赛的三个水题之一,题意是两个玩家每人都持有一手牌,问第一个玩家是否有一种出牌方法使得在第一回和对方无牌可出.直接模拟即可,注意一次出完的情况,一开始没主意,wa了一发. #include< ...
- 设计模式_C++源码+总结
C++源码下载: http://yunpan.cn/Q7fadjGgEJxib 提取码 63cb 总结:
- flash player 版本对照
- ubuntu修改主机名和出现问题
修改主机名方法,修改/etc/hostname即可,但是修改完成后,每次sudo都出现警告,警告解决方法如下: Linux 环境, 假设这台机器名字叫dev(机器的hostname), 每次执行sud ...
- 派遣例程与IRP结构
提到派遣例程,必须理解IRP(I/O Request Package),即"输入/输出请求包"这个重要数据结构的概念.Ring3通过DeviceIoControl等函数向驱动发出I ...
- uva 11991 Easy Problem from Rujia Liu? vector+map
水题 学习一下数据的存储方法. #include<iostream> #include<cstdio> #include<cstdlib> #include< ...
- JSFのAjaxタグのoneventでbegin/complete/successを使う
PrimeFacesに慣れてしまって.通常のHTMLタグでの記述方法がわからなかったりする点があった…ので.メモ. Ajaxでリクエスト送信のタイミングやレスポンスが戻るタイミングに何らか(JavaS ...