demo:https://github.com/chenchunyong/source-map-demo.git
背景
前端开发中,开发环境与生产环境的代码往往不一致,生产环境的代码通过构建工具进行合并,混淆,压缩等操作。不过通过构建工具生成的js代码可读性差,不利于维护、排查问题。
解决方案
首先,通过构建工具生成两个生产js文件,如下:
- 所有用户可见js,比如bundle.js
- 特定用户js,排查问题用,此js文件会加入sourcemap引用,比如说bundle.debug.js
然后,根据用户输入的url是否包含sourcemap参数,来判断是否启动sourcemap调试功能。比如如果包含sourcemap参数,则引入bundle.debug.js,否则引入bundle.js。
使用
克隆
进入source-map-demo目录,安装
打包js文件
启动
输入http://localhost:3000 或者http://localhost:3000?sourcemap=true 查看效果。