Using ES6


To use ES6, we need loader.

  1. 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的更多相关文章

  1. [AngualrJS + Webpack] Production Source Maps

    When you uglify your Angular code with Webpack's uglify plugin, debugging your application can be a ...

  2. JavaScript Source Maps浅析

    阅读目录 有用的链接 Link: 原文链接 译文开始: 对网站进行性能优化对一个最容易的方法就是把JS和CSS进行打包压缩.但是当你需要调试这些压缩文件中的代码的时候,会发生什么?可能会是一场噩梦.但 ...

  3. Source Maps简介

    提高网站性能最简单的方式之一是合并压缩JavaScript和CSS文件.但是当你需要调试这些压缩文件中的代码时,那将会是一场噩梦.不过也不用担心,souce maps将会帮你解决这一问题. Sourc ...

  4. 【译】Source Maps浅析

    Time:2019/10/27~2019/10/29 Link: 原文链接 译文开始: 对网站进行性能优化对一个最容易的方法就是把JS和CSS进行打包压缩.但是当你需要调试这些压缩文件中的代码的时候, ...

  5. Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT Source Maps 详解

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  6. Introduction to JavaScript Source Maps

    下载jquery时候发现:jquery.min.map  这什么鬼呀? https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/core.js http ...

  7. [转] Webpack的devtool和source maps

    source maps Webpack打包生成的.map后缀文件,使得我们的开发调试更加方便,它能帮助我们链接到断点对应的源代码的位置进行调试(//# souceURL),而devtool就是用来指定 ...

  8. WebStorm 9 自动编译 LESS 产出 CSS 和 source maps

    1.双击桌面Chrome图标,打开Chrome,按键盘“F12”键,打开开发工具界面,点击其右上角的“设置”按钮,勾选“Enable JavaScript source maps”  及“Enable ...

  9. 前端构建:Source Maps详解

    一.前言 当使用CoffeeScript.ClojureScript编写前端脚本时,当使用Less.Sacc编写样式规则时,是否觉得调试时无法准确找到源码位置呢?当使用jquery.min.js等经压 ...

随机推荐

  1. poj3468(线段树 边覆盖)

    #include<cstdio> int lb,rb,data; long long sum[5000000],extra[5000000]; void add(int l,int r,i ...

  2. 大四实习准备2_java异常处理_android控件练习

    2015-4-24 Java 异常处理 可以有多个catch;ArrayIndexOutOfBoundsException类是Exception类的子类RuntimeException类的一个间接子类 ...

  3. 12月上旬poj其他题

    poj3170 1,4两遍bfs: poj3171 改一改poj2376即可 poj3172 dfs+剪枝 其实增长速度很快,n<=40,题目吓你的: poj3661 比较经典的dp:设f[i, ...

  4. bzoj1934 bzoj2768

    最小割的经典模型,体现出最小割的基本定义,把两个集合划分的最小代价 把一开始同意的人连源点,不同意的连汇点,有关系的人之间连边,流量都为1 不难发现,割两点(人)间的边就相当于朋友之间发生冲突 割到连 ...

  5. poj2828

    很容易想到一种动态的做法:平衡树…… 或者是二分+树状数组 但,前者编程复杂度较大,而且据说会被卡(没试过):后者理论上超时(据说可以擦边过?): 所以要尝试新的算法: 倒着考虑,显然最后一个对象的位 ...

  6. [原]Unity3D深入浅出 - 物理材质(Physics Materials)

    在Unity3d中已经配置好了5种常用的物理材质,Bouncy.Ice.Metal.Rubber.Wood,在菜单中依次选择Assets - Import Package - Physics Mate ...

  7. JS 获取Button控件的提交类型

    <script type="text/javascript"> <!--获取button控件的类型---> function isAuditOrCancel ...

  8. js匿名函数

    <script type="text/javascript"> $(function () { $("#btn").mousedown(functi ...

  9. POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)

    [题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...

  10. NOIP2012 同余方程

    1同余方程 题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输出只有一行 ...