源碼映射

Chrome中的源碼映射

When your source code has gone through transformations, debugging becomes a problem. When debugging in a browser, how to tell where the original code is? Source maps solve this problem by providing a mapping between the original and the transformed source code. In addition to source compiling to JavaScript, this works for styling as well.
當你的源代碼完成轉換後,調試就成了一個問題。在瀏覽器中調試時,如何確認代碼中的原始位置呢?Source maps 經過創建源碼與轉換後的代碼之間的映射關係來解決這個問題。除了能夠用在編譯後的Javascript中,還能夠用在樣式中。javascript

One approach is to skip source maps during development and rely on browser support of language features. If you use ES2015 without any extensions and develop using a modern browser, this can work. The advantage of doing this is that you avoid all the problems related to source maps while gaining better performance.
在開發中不使用源碼映射的方法是依賴於瀏覽器對語言特性的支持。若是你只使用ES015而不使用任何擴展而且在開發中使用現代瀏覽器,這是能夠實現的。這樣作的好處就是爲了獲取更好的性能時能夠避免因爲源碼映射致使的性能降低問題。css

If you are using webpack 4 and the new mode option, the tool will generate source maps automatically for you in development mode. Production usage requires attention, though.
若是你在使用Webpack4而且使用 mode 選項,在development模式下,Webpack會自動生成源碼映射關係。可是須要注意生產環境的使用。html

T> If you want to understand the ideas behind source maps in greater detail, read Ryan Seddon's introduction to the topic.
若是想深刻了解source map的機制,能夠讀Ryan Seddon的介紹.html5

T> To see how webpack handles source maps, see source-map-visualization by the author of the tool.
T> 想了解webpack如何處理源碼映射, 查看工具做者寫的source-map-visualization .java

Inline Source Maps and Separate Source Maps(內聯源碼映射和分離源碼映射)

Webpack can generate both inline or separate source map files. The inline ones are valuable during development due to better performance while the separate ones are handy for production use as it keeps the bundle size small. In this case, loading source maps is optional.
Webpack便可以生成內聯源碼映射也能夠生成分離源碼映射。內聯源碼映射在開發時很是有用,而爲了追求生產環境中更好的性能,分離源碼映射經過保持Bundle的文件更小而更加方便。這種狀況下,加載源碼映射是選擇性的。webpack

It's possible you don't want to generate a source map for your production bundle as this makes it effortless to inspect your application. By disabling source maps, you are performing a sort of obfuscation. Whether or not you want to enable source maps for production, they are handy for staging. Skipping source maps speeds up your build as generating source maps at the best quality can be a complicated operation.
你必定不想在生產環境中生成源碼映射,由於它能用來很是容易的分析你的程序。經過禁用源碼映射,你要執行一系列混淆。不管你想不想啓用源碼映射,可是它們對於演示來講很是方便。跳過源碼映射能夠加快你的構建速度,而生成源碼映射能夠提高質量,這可能成爲一個複雜的操做。git

Hidden source maps give stack trace information only. You can connect them with a monitoring service to get traces as the application crashes allowing you to fix the problematic situations. While this isn't ideal, it's better to know about possible problems than not.
隱藏源碼映射 只能提供堆棧跟蹤信息。在程序崩潰的時候,可讓你把他們鏈接到監視服務來獲取跟蹤信息來解決有問題的狀況。雖然這樣並不理想,可是知道可能的問題總比不知道要好。github

T> It's a good idea to study the documentation of the loaders you are using to see loader specific tips. For example, with TypeScript, you have to set a particular flag to make it work as you expect.
學習你在使用的加載器的具體提示是個好主意。例如,在Typescript中,你得設置一個標誌才能讓它按照預想的方式工做。web

Enabling Source Maps(啓用源碼映射)

Webpack provides two ways to enable source maps. There's a devtool shortcut field. You can also find two plugins that give more options to tweak. The plugins are going to be discussed briefly at the end of this chapter. Beyond webpack, you also have to enable support for source maps at the browsers you are using for development.
Webpack有兩種方式來啓用源碼映射。有一個字段devtool。你也能找到兩個插件,他們能提供更多的選項作進一步配置。這些插件將在本文最後作個簡單的介紹。除了Webpack,咱們還得在開發使用的瀏覽器中啓用源碼映射支持。chrome

Enabling Source Maps in Webpack(啓用Webpack中的源碼映射)

To get started, you can wrap the core idea within a configuration part. You can convert this to use the plugins later if you want:
在開始前,你能夠把這個核心思想封裝在一個配置部分中。若是願意,你能夠稍後將其轉換成插件:

webpack.parts.js

exports.generateSourceMaps = ({ type }) => ({
  devtool: type,
});

Webpack supports a wide variety of source map types. These vary based on quality and build speed. For now, you enable source-map for production and let webpack use the default for development. Set it up as follows:
Webpack支持不少種源碼映射類型。他們的不一樣在於質量和構建速度。如今,爲生產環境啓用source-map而且讓webpack對開發環境使用默認配置。按以下方式配置:

webpack.config.js

const productionConfig = merge([
leanpub-start-insert
  parts.generateSourceMaps({ type: "source-map" }),
leanpub-end-insert
  ...
]);

source-map is the slowest and highest quality option of them all, but that's fine for a production build.
source-map是最慢可是質量卻最高的,這對生產環境構建很適合。

{pagebreak}

If you build the project now (npm run build), you should see source maps in the output:
若是你如今構建項目(npm run build),就能在輸出中看到源碼映射:

Hash: b59445cb2b9ae4cea11b
Version: webpack 4.1.1
Time: 1347ms
Built at: 3/16/2018 4:58:14 PM
       Asset       Size  Chunks             Chunk Names
     main.js  838 bytes       0  [emitted]  main
    main.css   3.49 KiB       0  [emitted]  main
 main.js.map   3.75 KiB       0  [emitted]  main
main.css.map   85 bytes       0  [emitted]  main
  index.html  220 bytes          [emitted]
Entrypoint main = main.js main.css main.js.map main.css.map
...

Take a good look at those .map files. That's where the mapping between the generated and the source happens. During development, it writes the mapping information in the bundle.
仔細查看這些 .map 文件。這就是保存源碼和編譯後的代碼的映射關係的地方。在開發的時候,它把映射信息直接寫到Bundle中。

Enabling Source Maps in Browsers(在瀏覽器中啓用源碼映射)

To use source maps within a browser, you have to enable source maps explicitly as per browser-specific instructions:
要想在瀏覽器中使用源碼映射,你得根據不一樣瀏覽器的具體介紹準確啓用源碼映射。

W> If you want to use breakpoints (i.e., a debugger; statement or ones set through the browser), the eval-based options won't work in Chrome!
若是你想用斷點(例如,一個debugger語句或者經過瀏覽器設置),在chrome中基於eval的選項不起做用。

Source Map Types Supported by Webpack(webpack支持的源碼映射類型)

Source map types supported by webpack can be split into two categories:
Webpack支持的源碼映射能夠分爲兩類:

  • Inline source maps add the mapping data directly to the generated files.
  • Inline 源碼映射直接將映射數據添加到生成文件中.
  • Separate source maps emit the mapping data to separate source map files and link the source to them using a comment. Hidden source maps omit the comment on purpose.
  • Separate 源碼映射將映射數據生成到獨立的源碼映射文件中並經過註釋將他們的源碼關聯起來. Hidden source maps故意省略註釋。

Thanks to their speed, inline source maps are ideal for development. Given they make the bundles big, separate source maps are the preferred solution for production. Separate source maps work during development as well if the performance overhead is acceptable.
內聯源碼映射因爲速度快很是適合開發使用。因爲他們使Bundle變大,分離的源碼映射更受到生產環境的青睞。若是性能開銷能夠接受,分離源碼映射也能夠用在開發中。

Inline Source Map Types(內聯源碼映射類型)

Webpack provides multiple inline source map variants. Often eval is the starting point and webpack issue #2145 recommends cheap-module-eval-source-map as it's a good compromise between speed and quality while working reliably in Chrome and Firefox browsers.
Webpack提供多種內聯源碼映射變體。eval一般做爲開始點,在webpack issue #2145中推薦使用cheap-module-eval-source-map因爲其很好的平衡了速度和質量而且在Firefox和Chrome中運行穩定。

To get a better idea of the available options, they are listed below while providing a small example for each. The source code contains only a single console.log('Hello world') and webpack.NamedModulesPlugin is used to keep the output easier to understand. In practice, you would see a lot more code to handle the mapping.
爲了更好的離解可用的選項,下面列舉並使用短小的例子進行說明。源碼中只包含一句console.log('Hello world') 而且 webpack.NamedModulesPlugin保證輸出更易於理解 。在實際中,你將會看到更多的代碼來處理映射關係。

devtool: "eval"

eval generates code in which each module is wrapped within an eval function:
eval生成代碼,每一個模塊都封裝在一個eval函數中:

webpackJsonp([1, 2], {
  "./src/index.js": function(module, exports) {
    eval("console.log('Hello world');\n\n//////////////////\n// WEBPACK FOOTER\n// ./src/index.js\n// module id = ./src/index.js\n// module chunks = 1\n\n//# sourceURL=webpack:///./src/index.js?")
  }
}, ["./src/index.js"]);

devtool: "cheap-eval-source-map"

cheap-eval-source-map goes a step further and it includes base64 encoded version of the code as a data url. The result contains only line data while losing column mappings.
cheap-eval-source-map更進一步,它包含base64編碼版本的代碼做爲數據的URL。結果中只包含行數據而沒有列的映射數據。

webpackJsonp([1, 2], {
  "./src/index.js": function(module, exports) {
    eval("console.log('Hello world');//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9hcHAvaW5kZXguanMuanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9hcHAvaW5kZXguanM/MGUwNCJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zb2xlLmxvZygnSGVsbG8gd29ybGQnKTtcblxuXG4vLy8vLy8vLy8vLy8vLy8vLy9cbi8vIFdFQlBBQ0sgRk9PVEVSXG4vLyAuL2FwcC9pbmRleC5qc1xuLy8gbW9kdWxlIGlkID0gLi9hcHAvaW5kZXguanNcbi8vIG1vZHVsZSBjaHVua3MgPSAxIl0sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIifQ==")
  }
}, ["./src/index.js"]);

{pagebreak}

If you decode that base64 string, you get output containing the mapping:
若是解碼base64的字符串,就能獲得包含映射關係的輸出:

{
  "file": "./src/index.js",
  "mappings": "AAAA",
  "sourceRoot": "",
  "sources": [
    "webpack:///./src/index.js?0e04"
  ],
  "sourcesContent": [
    "console.log('Hello world');\n\n\n//////////////////\n// WEBPACK FOOTER\n// ./src/index.js\n// module id = ./src/index.js\n// module chunks = 1"
  ],
  "version": 3
}

devtool: "cheap-module-eval-source-map"

cheap-module-eval-source-map is the same idea, except with higher quality and lower performance:
cheap-module-eval-source-map 也是相同的思路,只是質量更高,可是性能更低:

webpackJsonp([1, 2], {
  "./src/index.js": function(module, exports) {
    eval("console.log('Hello world');//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9hcHAvaW5kZXguanMuanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vYXBwL2luZGV4LmpzPzIwMTgiXSwic291cmNlc0NvbnRlbnQiOlsiY29uc29sZS5sb2coJ0hlbGxvIHdvcmxkJyk7XG5cblxuLy8gV0VCUEFDSyBGT09URVIgLy9cbi8vIGFwcC9pbmRleC5qcyJdLCJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VSb290IjoiIn0=")
  }
}, ["./src/index.js"]);

{pagebreak}

Again, decoding the data reveals more:
再一次,解碼數據揭示更多信息:

{
  "file": "./src/index.js",
  "mappings": "AAAA",
  "sourceRoot": "",
  "sources": [
    "webpack:///src/index.js?2018"
  ],
  "sourcesContent": [
    "console.log('Hello world');\n\n\n// WEBPACK FOOTER //\n// src/index.js"
  ],
  "version": 3
}

In this particular case, the difference between the options is minimal.
在這個特定的狀況中,選項之間的差別是很小的。

devtool: "eval-source-map"

eval-source-map is the highest quality option of the inline options. It's also the slowest one as it emits the most data:
eval-source-map 是內聯選項中質量最高的選項。因爲它產生最多的數據因此也是最慢的。

webpackJsonp([1, 2], {
  "./src/index.js": function(module, exports) {
    eval("console.log('Hello world');//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9hcHAvaW5kZXguanM/ZGFkYyJdLCJuYW1lcyI6WyJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiQUFBQUEsUUFBUUMsR0FBUixDQUFZLGFBQVoiLCJmaWxlIjoiLi9hcHAvaW5kZXguanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJjb25zb2xlLmxvZygnSGVsbG8gd29ybGQnKTtcblxuXG4vLyBXRUJQQUNLIEZPT1RFUiAvL1xuLy8gLi9hcHAvaW5kZXguanMiXSwic291cmNlUm9vdCI6IiJ9")
  }
}, ["./src/index.js"]);

{pagebreak}

This time around there's more mapping data available for the browser:
這回有更多的映射數據可供瀏覽器使用:

{
  "file": "./src/index.js",
  "mappings": "AAAAA,QAAQC,GAAR,CAAY,aAAZ",
  "names": [
    "console",
    "log"
  ],
  "sourceRoot": "",
  "sources": [
    "webpack:///./src/index.js?dadc"
  ],
  "sourcesContent": [
    "console.log('Hello world');\n\n\n// WEBPACK FOOTER //\n// ./src/index.js"
  ],
  "version": 3
}

Separate Source Map Types(分離式源碼映射類型)

Webpack can also generate production usage friendly source maps. These end up in separate files ending with .map extension and are loaded by the browser only when required. This way your users get good performance while it's easier for you to debug the application.
Webpack還能生成適用於生產環境的源碼映射。它們最終存入以.map爲擴展名的單獨文件中,而且只在須要的狀況下由瀏覽器加載。用戶能夠得到更好的性能,同時他也能夠Debug程序。

source-map is a reasonable default here. Even though it takes longer to generate the source maps this way, you get the best quality. If you don't care about production source maps, you can skip the setting there and get better performance in return.
source-map是這裏理所固然的默認值。儘管按這種方式生成代碼映射會耗費更長時間,可是你能得到更好的性能。若是你不介意生產環境的代碼映射,能夠跳過此步來獲取更好的性能。

{pagebreak}

devtool: "cheap-source-map"

cheap-source-map is similar to the cheap options above. The result is going to miss column mappings. Also, source maps from loaders, such as css-loader, are not going to be used.
cheap-source-map和上面介紹的簡易選項同樣。結果就是丟失列映射關係。此外,來自加載器的源碼映射(如css-loader)將不能使用。

Examining the .map file reveals the following output in this case:
在這個例子中,查看.map文件能夠獲得如下輸出:

{
  "file": "main.9aff3b1eced1f089ef18.js",
  "mappings": "AAAA",
  "sourceRoot": "",
  "sources": [
    "webpack:///main.9aff3b1eced1f089ef18.js"
  ],
  "sourcesContent": [
    "webpackJsonp([1,2],{\"./src/index.js\":function(o,n){console.log(\"Hello world\")}},[\"./src/index.js\"]);\n\n\n// WEBPACK FOOTER //\n// main.9aff3b1eced1f089ef18.js"
  ],
  "version": 3
}

The source contains //# sourceMappingURL=main.9a...18.js.map kind of comment at its end to map to this file.
源碼結尾中包含 //# sourceMappingURL=main.9a...18.js.map這樣的註釋來指向分離的映射文件。

{pagebreak}

devtool: "cheap-module-source-map"

cheap-module-source-map is the same as previous except source maps from loaders are simplified to a single mapping per line. It yields the following output in this case:
除了加載器的源映射被簡化爲每行一個映射以外,cheap-module-source-map與前面的映射是相同的。在這種狀況下,它產生以下輸出:

{
  "file": "main.9aff3b1eced1f089ef18.js",
  "mappings": "AAAA",
  "sourceRoot": "",
  "sources": [
    "webpack:///main.9aff3b1eced1f089ef18.js"
  ],
  "version": 3
}

W> cheap-module-source-map is currently broken if minification is used and this is an excellent reason to avoid the option for now.
W> 若是使用了minification cheap-module-source-map 就會遭到破壞,這也成爲不使用這個選項的絕佳理由.

devtool: "hidden-source-map"

hidden-source-map is the same as source-map except it doesn't write references to the source maps to the source files. If you don't want to expose source maps to development tools directly while you wish proper stack traces, this is handy.
hidden-source-mapsource-map同樣,只不過它不會將源碼映射的引用寫入源文件中。若是您不想直接將源映射公開給開發工具,而但願使用適當的堆棧跟蹤,那麼這是很是方便的。

devtool: "nosources-source-map"

nosources-source-map creates a source map without sourcesContent in it. You still get stack traces, though. The option is useful if you don't want to expose your source code to the client.
nosources-source-map建立的源碼映射裏不包含代碼內容。可是你仍是能獲得堆棧跟蹤信息。若是不想暴露源碼給用戶這個選項頗有用。

T> The official documentation contains more information about devtool options.
T> The official documentation 包含更多關於 devtool 選項的信息.

{pagebreak}

devtool: "source-map"

source-map provides the best quality with the complete result, but it's also the slowest option. The output reflects this:
source-map用完整的結果提供最好的質量,可是它也是最慢的選項。輸出以下所示:

{
  "file": "main.9aff3b1eced1f089ef18.js",
  "mappings": "AAAAA,cAAc,EAAE,IAEVC,iBACA,SAAUC,EAAQC,GCHxBC,QAAQC,IAAI,kBDST",
  "names": [
    "webpackJsonp",
    "./src/index.js",
    "module",
    "exports",
    "console",
    "log"
  ],
  "sourceRoot": "",
  "sources": [
    "webpack:///main.9aff3b1eced1f089ef18.js",
    "webpack:///./src/index.js"
  ],
  "sourcesContent": [
    "webpackJsonp([1,2],{\n\n/***/ \"./src/index.js\":\n/***/ (function(module, exports) {\n\nconsole.log('Hello world');\n\n/***/ })\n\n},[\"./src/index.js\"]);\n\n\n// WEBPACK FOOTER //\n// main.9aff3b1eced1f089ef18.js",
    "console.log('Hello world');\n\n\n// WEBPACK FOOTER //\n// ./src/index.js"
  ],
  "version": 3
}

{pagebreak}

Other Source Map Options(其餘源碼映射選項)

There are a couple of other options that affect source map generation:
還有其餘一些選項影響源碼映射的生成:

{
  output: {
    // Modify the name of the generated source map file.
    // You can use [file], [id], and [hash] replacements here.
    // The default option is enough for most use cases.
    sourceMapFilename: '[file].map', // Default

    // This is the source map filename template. It's default
    // format depends on the devtool option used. You don't
    // need to modify this often.
    devtoolModuleFilenameTemplate:
      'webpack:///[resource-path]?[loaders]'
  },
}

T> The official documentation digs into output specifics.
T> 官方文檔 深刻研究了output的細節 .

W> If you are using UglifyJsPlugin and still want source maps, you need to enable sourceMap: true for the plugin. Otherwise, the result isn't what you expect because UglifyJS will perform a further transformation of the code, breaking the mapping. The same has to be done with other plugins and loaders performing changes. css-loader and related loaders are a good example.
W> 若是你在使用 UglifyJsPlugin,而且想使用源碼映射,你須要在插件中啓用sourcemap:true。不然,結果與你的預期會不一致,由於UglifyJS會執行更深刻的代碼轉換從而破壞了映射。在其餘的插件和加載器中也須要作相應的修改。css-loader和相關的加載器就是很好的例子。

SourceMapDevToolPlugin and EvalSourceMapDevToolPlugin

If you want more control over source map generation, it's possible to use the SourceMapDevToolPlugin or EvalSourceMapDevToolPlugin instead. The latter is a more limited alternative, and as stated by its name, it's handy for generating eval based source maps.
若是想對源碼映射進行更多的控制,你可使用SourceMapDevToolPlugin 或者 EvalSourceMapDevToolPlugin 來代替. 後者是一種更有限的選擇,正如其名稱所述,它對於生成基於eval的源碼映射很是方便。

Both plugins can allow more granular control over which portions of the code you want to generate source maps for, while also having strict control over the result with SourceMapDevToolPlugin. Using either plugin allows you to skip the devtool option altogether.
這兩個插件都容許更細粒度地控制要爲哪部分代碼生成源碼映射, 同時還可使用「SourceMapDevToolPlugin」嚴格控制結果。使用任何一個插件均可以跳過「devtool」選項。

Given webpack matches only .js and .css files by default for source maps, you can use SourceMapDevToolPlugin to overcome this issue. This can be achieved by passing a test pattern like /\.(js|jsx|css)($|\?)/i.
考慮到webpack生成源碼匹配時默認只匹配 .js.css 文件, 你可使用 SouceMapDevToolPlugin來解決這個問題。能夠經過傳入一個 像/\.(js|jsx|css)($|\?)/i`test這樣的test`模式來解決。

EvalSourceMapDevToolPlugin accepts only module and lineToLine options as described above. Therefore it can be considered as an alias to devtool: "eval" while allowing a notch more flexibility.
如上所述,EvalSourceMapDevToolPlugin只接受modulelineToLine選項。所以能夠把它當作是devtool: "eval"的別稱,只不過是配置更富彈性。

Changing Source Map Prefix(修改源碼映射前綴)

You can prefix a source map option with a pragma character that gets injected into the source map reference. Webpack uses # by default that is supported by modern browsers, so you don't have to set it.
您能夠在源碼映射選項前面加上一個編譯指示字符,該字符被注入到源碼映射引用中。Webpack默認使用現代瀏覽器都支持的#,因此不用特意去設置它。

To override this, you have to prefix your source map option with it (e.g., @source-map). After the change, you should see //@ kind of reference to the source map over //# in your JavaScript files assuming a separate source map type was used.
要替換它的話,你須要把它添加到源碼映射選項的前面(例如:@source-map).修改以後,若是是正在使用分離映射文件,你應該能在源碼映射中看到//@這樣的引用而不是//#

Using Dependency Source Maps(使用依賴源碼映射)

Assuming you are using a package that uses inline source maps in its distribution, you can use source-map-loader to make webpack aware of them. Without setting it up against the package, you get minified debug output. Often you can skip this step as it's a special case.
假設你在使用一個包,它在使用內聯源碼映射,你可使用source-map-loader來讓Webpack注意到他們。若是沒有對這個包進行設置,你會得到較少的輸出。因爲這是一個特例,一般你能夠路過此步。

Source Maps for Styling(樣式的源碼映射)

If you want to enable source maps for styling files, you can achieve this by enabling the sourceMap option. The same idea works with style loaders such as css-loader, sass-loader, and less-loader.
若是想在樣式文件中使用源碼映射,能夠經過啓用sourceMap選項來實現。相同的想法對樣式加載器如 css-loader, sass-loaderless-loader也適用。

The css-loader is known to have issues when you are using relative paths in imports. To overcome this problem, you should set output.publicPath to resolve the server url.
在imports中使用相對路徑會致使 css-loader已知問題發生.要解決這個問題,你應該設置 output.publicPath來處理服務器路徑。

{pagebreak}

Conclusion(總結)

Source maps can be convenient during development. They provide better means to debug applications as you can still examine the original code over a generated one. They can be valuable even for production usage and allow you to debug issues while serving a client-friendly version of your application.
源碼映射給開發帶來了便利。他們提供了調試應用程序更好的方法,由於你仍然能夠經過生成的代碼檢查原始代碼。他們在生產環境中使用也是頗有意義而且讓你能夠在對用戶友好的版本中調試問題。

To recap:(概述)

  • Source maps can be helpful both during development and production. They provide more accurate information about what's going on and make it faster to debug possible problems.
  • 源碼映射對於開發環境和生產環境都很是有幫助。
  • Webpack supports a large variety of source map variants. They can be split into inline and separate source maps based on where they are generated. Inline source maps are handy during development due to their speed. Separate source maps work for production as then loading them becomes optional.
  • Webpack 不少種源碼映射變體。他們能夠根據生成位置的不一樣被分紅內聯式的和分離式的源碼映射。由於內聯源碼映射速度快很是適合開發階段。因爲源碼映射加載是可選的,分離源碼映射適合於生產環境.
  • devtool: "source-map" is the highest quality option making it valuable for production.
  • devtool: "source-map" 是最高質量的選項,所以很是適合生產環境.
  • cheap-module-eval-source-map is a good starting point for development.
  • cheap-module-eval-source-map 是開發的一個好起點。
  • If you want to get only stack traces during production, use devtool: "hidden-source-map". You can capture the output and send it to a third party service for you to examine. This way you can capture errors and fix them.
  • 若是隻想在生產環境中得到堆棧跟蹤,使用 devtool: "hidden-source-map。您能夠捕獲輸出並將其發送到第三方服務以供檢查。這樣你 能捕獲錯誤並修復他們。
  • SourceMapDevToolPlugin and EvalSourceMapDevToolPlugin provide more control over the result than the devtool shortcut.
  • SourceMapDevToolPluginEvalSourceMapDevToolPlugindevtool 對結果提供更多的控制。
  • source-map-loader can come in handy if your dependencies provide source maps.
  • 若是依賴包提供源碼映射,那麼用source-map-loader就會很方便。
  • Enabling source maps for styling requires additional effort. You have to enable sourceMap option per styling related loader you are using.
  • 爲式樣啓用源碼映射須要進行額外的配置. 須要爲每一個加載器啓用 sourceMap 選項。

In the next chapter, you'll learn to split bundles and separate the current one into application and vendor bundles.在下一節中,你將學到分離bundle並將如今的Bundle分離到應用Bundle和供應商Bundle中。

相關文章
相關標籤/搜索