简体   繁体   English

如何使用 JavaScript 源映射(.map 文件)?

[英]How can I use JavaScript source maps (.map files)?

Recently I have seen files with the .js.map extension shipped with some JavaScript libraries (like Angular ), and that just raised a few questions in my head:最近我看到一些 JavaScript 库(如Angular )附带带有.js.map扩展名的文件,这只是在我脑海中提出了几个问题:

  • What is it for?它是做什么用的? Why do the guys at Angular care to deliver a .js.map file?为什么 Angular 的人关心交付.js.map文件?
  • How can I (as a JavaScript developer) use the angular.min.js.map file?我(作为 JavaScript 开发人员)如何使用angular.min.js.map文件?
  • Should I care about creating .js.map files for my JavaScript applications?我应该关心为我的 JavaScript 应用程序创建.js.map文件吗?
  • How does it get created?它是如何创建的? I took a look at angular.min.js.map and it was filled with strange-formatted strings, so I assume it's not created manually.我看了一下angular.min.js.map ,里面充满了奇怪格式的字符串,所以我认为它不是手动创建的。

The .map files are for JavaScript and CSS (and now TypeScript too) files that have been minified. .map文件用于已缩小的 JavaScript 和 CSS(现在也是 TypeScript)文件。 They are called source maps .它们被称为源映射 When you minify a file, like the angular.js file, it takes thousands of lines of pretty code and turns it into only a few lines of ugly code.当你缩小一个文件时,比如angular.js文件,它需要数千行漂亮的代码,然后把它变成只有几行丑陋的代码。 Hopefully, when you are shipping your code to production, you are using the minified code instead of the full, unminified version.希望当您将代码交付到生产环境时,您使用的是缩小版的代码,而不是完整的未缩小版。 When your app is in production, and has an error, the source map will help take your ugly file, and will allow you to see the original version of the code.当您的应用程序在生产中出现错误时,源映射将帮助您获取丑陋的文件,并允许您查看代码的原始版本。 If you didn't have the source map, then any error would seem cryptic at best.如果您没有源映射,那么任何错误充其量看起来都是神秘的。

Same for CSS files. CSS 文件也是如此。 Once you take a Sass or Less file and compile it to CSS, it looks nothing like its original form.一旦你将一个SassLess文件编译成 CSS,它看起来就完全不像它的原始形式了。 If you enable sourcemaps, then you can see the original state of the file, instead of the modified state.如果启用 sourcemaps,那么您可以看到文件的原始状态,而不是修改后的状态。

So, to answer you questions in order:所以,按顺序回答你的问题:

  • What is it for?它是做什么用的? To de-reference uglified code取消引用丑化代码
  • How can a developer use it?开发人员如何使用它? You use it for debugging a production app.您可以使用它来调试生产应用程序。 In development mode you can use the full version of Angular.在开发模式下,您可以使用完整版的 Angular。 In production, you would use the minified version.在生产中,您将使用缩小版本。
  • Should I care about creating a js.map file?我应该关心创建一个 js.map 文件吗? If you care about being able to debug production code easier, then yes, you should do it.如果您关心能够更轻松地调试生产代码,那么是的,您应该这样做。
  • How does it get created?它是如何创建的? It is created at build time.它是在构建时创建的。 There are build tools that can build your .map file for you as it does other files.有一些构建工具可以像构建其他文件一样为您构建 .map 文件。 Sourcemaps fail if the output file is not located in the project root directory #71 如果输出文件不在项目根目录中,则源映射失败 #71

I hope this makes sense.我希望这是有道理的。

How can a developer use it?开发人员如何使用它?

  1. Don't link your js.map file in your index.html file (no need for that)不要在你的index.html文件中链接你的js.map文件(不需要)

  2. Minification tools (good ones) add a comment to your .min.js file:缩小工具(好的)向您的.min.js文件添加注释:

     //# sourceMappingURL=yourFileName.min.js.map

    which will connect your .map file.这将连接您的.map文件。

    When the min.js and js.map files are ready...min.jsjs.map文件准备好时...

  3. Chrome: Open dev-tools , navigate to Sources tab. Chrome:打开dev-tools ,导航到Sources选项卡。 You will see the sources folder, where un-minified applications files are kept.您将看到保存未缩小的应用程序文件的文件夹。

I just wanted to focus on the last part of the question;我只想关注问题的最后一部分; How are source map files created?源映射文件是如何创建的? by listing the build tools I know that can create source maps.通过列出我知道可以创建源映射的构建工具。

  1. Grunt : using plugin grunt-contrib-uglify Grunt : 使用插件grunt-contrib-uglify
  2. Gulp : using plugin gulp-uglify Gulp : 使用插件gulp-uglify
  3. Google closure : using parameter --create_source_map Google 关闭:使用参数--create_source_map

The map file maps the unminified file to the minified file.映射文件将未缩小的文件映射到缩小的文件。 If you make changes in the unminified file, the changes will be automatically reflected to the minified version of the file.如果您在未缩小的文件中进行更改,更改将自动反映到文件的缩小版本。

Just to add to how to use map files: I use Google Chrome for Ubuntu and if I go to sources and click on a file, if there is a map file a message comes up telling me that I can view the original file and how to do it.只是添加到如何使用地图文件:我使用 Google Chrome for Ubuntu,如果我转到源并单击文件,如果有地图文件,则会出现一条消息,告诉我我可以查看原始文件以及如何查看做吧。

For the Angular files that I worked with today I click Ctrl + P and a list of original files comes up in a small window.对于我今天使用的 Angular 文件,我单击Ctrl + P ,一个原始文件列表出现在一个小窗口中。

I can then browse through the list to view the file that I would like to inspect and check where the issue might be.然后我可以浏览列表以查看我想要检查的文件并检查问题可能出在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM