文章首发:https://github.com/giscafer/g...node
node.js在 v8.5
版本以后能够直接支持ES6模块,可是有个限制就是,须要为.mjs
后缀的文件才行,好比有个es6代码文件为test.mjs
,能够直接用node.js运行调式: node --experimental-modules test.mjs
。webpack
有限制就不灵活了,看lodash
源码文件,两百多个源码文件都是ES6语法,文件名称是.js
,想在VS Code中直接node启动调式是不行的,仍是须要用到babel
才行。git
下边介绍VS Code 如何配置 webpack+babel 调式es6代码的环境es6
npm i babel-core babel-loader webpack --save-dev
github
webpack.config.js
web
const path = require('path'); module.exports = { devtool: 'source-map', entry: { index: path.resolve('./test/index.js'), // 文件主入口 }, output: { path: path.resolve('./dist/'), // 打包文件位置 filename: '[name].js' }, module: { loaders: [{ test: /.js$/, loader: 'babel-loader' }] } };
"scripts": { "build": "webpack --progress", }
按下 F5
会自动建立launch.json
默认配置,咱们作调整修改就行npm
{ "version": "0.2.0", // 多个独立的配置项 "configurations": [ { // 语言 "type": "node", // 是调试模式,仍是附着到已运行的程序上 "request": "launch", // 该配置项的名字 "name": "Launch Program", // 程序的绝对路径 "program": "${workspaceFolder}/test/index.js", // 调试以前要作的任务名 "preLaunchTask": "build", // SourceMap "sourceMaps": true, // 是否自动中止程序 "stopOnEntry": false, // 生成的代码中,若是没法映射回源代码,则自动单步执行。 "smartStep": true, // 编译后的文件地址 "outFiles": [ "${workspaceRoot}/dist/**" ] } ] }
而后建立任务 task.json
,步骤:任务菜单——配置任务json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", // 这里是任务名称,对应launch.json中的preLaunchTask属性配置 "type": "npm", "script": "build", "problemMatcher": [] } ] }
完成上边的步骤后,就结束了,在你配置的对应webpack entry文件入口,写ES6代码后,打断点,按下F5便可看到断点调式效果。babel
好比个人test/index.js文件代码以下,引入的是lodash的add.js源码框架
import add from '../add.js'; console.log(add(1,2));
在add.js里边依赖的模块baseGetTag断点调式截图:
无论什么框架仍是库的源码,咱们阅读源码的时候,为了更容易理解代码逻辑,确定是须要源码调式的分析。使用nodejs环境调式使得效率更高,直接再IDE中阅读源码,直接调式看效果。