[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等经压 ...
随机推荐
- lnmp 安装环境之后discuz论坛排版乱的问题
服务器系统类型:centos 6.5 环境:使用lnmp官方安装shell安装 (http://lnmp.org/install.html) 在部署dz之后,访问页面 出现排版乱,资源不能加载的问题: ...
- 【转】iOS 宏(define)与常量(const)的正确使用-- 不错
原文网址:http://www.jianshu.com/p/f83335e036b5 在iOS开发中,经常用到宏定义,或用const修饰一些数据类型,经常有开发者不知怎么正确使用,导致项目中乱用宏与c ...
- 【转】蓝牙ble app开发(三) -- 抓包
原文网址:http://blog.csdn.net/lckj686/article/details/43156617 关于android 蓝牙app开发抓包的重要性在 android 蓝牙ble ap ...
- MS-SQL索引类型
一.索引的概念 索引就是加快检索表中数据的方法.数据库的索引类似于书籍的索引.在书籍中,索引允许用户不必翻阅完整个书就能迅速地找到所需要的信息.在数据库中,索引也允许数据库程序迅速地找到表中的 ...
- 使用Busybox-1.2.0制作根文件系统
使用Busybox-1.2.0制作根文件系统 cross-3.3.2 make-3.8.1 STEP 1: 创建根文件系统目录,主要包括以下目录/bin,/etc,/dev,/mnt,/sbin,/u ...
- [转] python程序的调试方法
qi09 原文 python程序的调试方法 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 源码例子 例如,有模拟税收计算的程序: #!/usr/bin/python de ...
- maven的pom报plugins却是的解决方法2
Failure to transfer org.apache.maven.plugins:maven-plugins:pom:25 from https://repo.maven.apache.org ...
- htmlcss笔记--a
a标签 1.下载:href里面放一个文件或者压缩包时,会下载: 2.锚点:跳转到锚点: href="#id" 跳转到的模块添加一个id,点击id就会跳转到该模块. html标签: ...
- js学习记录
1.js语法 2.数据类型(基本类型与对象类型.类型的转换) 2.1 数字 2.2 文本 2.3 布尔值 2.4 null和undefined 2.5 对象 2.6 类型检测 3.操作符和表达式 4. ...
- 50道经典的JAVA编程题(41-45)
50道经典的JAVA编程题(41-45),苦逼的程序猿,晚上睡不着了编程吧~今天坚持做10道题!发现编程能是我快乐...O(∩_∩)O哈哈~能平静我烦乱的心,剩下5道题留到考试完了再做吧!该睡觉了.. ...