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. mvn命令

    打包:mvn package 编译:mvn compile 编译测试程序:mvn test-compile 清空:mvn clean 运行测试:mvn test 生成站点目录: mvn site 生成 ...

  2. UVa 101 (模拟) The Blocks Problem

    题意: 有n个木块及n个木块堆,初始状态是第i个木块在第i个木块堆上.对应有四种操作,然后输出最终状态. 分析: 用一个vector<int>模拟一个木块堆,进行相应操作即可. #incl ...

  3. BZOJ2055: 80人环游世界

    题解: 总算A掉了,各种蛋疼... int main() { freopen("input.txt","r",stdin); freopen("out ...

  4. Codeforces Round #306 (Div. 2)

    A. Two Substrings You are given string s. Your task is to determine if the given string s contains t ...

  5. jQuery Mobile 入门教程

    你每天都会对着它讲话,和它玩游戏,用它看新闻——没错,它就是你裤兜里的智能手机.android,黑莓还是iphone?为了让你清楚意识到究竟哪些才算是智能手机,我在下面总结了一个智能手机系统/设备的列 ...

  6. VirtualBox故障一例

    早上的测试环境,估计是任务太重了吧,在点击VirtualBox的快速休眠后,就没有响应了,查看日志,内容都是: aComponent={Console} aText={The virtual mach ...

  7. LeetCode题解——Longest Substring Without Repeating Characters

    题目: 给定一个字符串,返回其中不包含重复字符的最长子串长度. 解法: 维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值:然后第一个指针 ...

  8. python学习之optparse

    Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...

  9. 你真的知道HTML吗?

    经过几次面试当中,被问及到最基础的东西,没想到回答不上来,有点蛋痛,今天特地的复习了一下!! 内容: 1.Doctype(文档类型)的作用是什么?有多少文档类型? 2.浏览器标准模式和怪异模式之间的区 ...

  10. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...