選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

source-map-resolve.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright 2014, 2015, 2016, 2017 Simon Lydell
  2. // X11 (“MIT”) Licensed. (See LICENSE.)
  3. // Note: source-map-resolve.js is generated from source-map-resolve-node.js and
  4. // source-map-resolve-template.js. Only edit the two latter files, _not_
  5. // source-map-resolve.js!
  6. void (function(root, factory) {
  7. if (typeof define === "function" && define.amd) {
  8. define(["source-map-url", "resolve-url"], factory)
  9. } else if (typeof exports === "object") {
  10. var sourceMappingURL = require("source-map-url")
  11. var resolveUrl = require("resolve-url")
  12. module.exports = factory(sourceMappingURL, resolveUrl)
  13. } else {
  14. root.sourceMapResolve = factory(root.sourceMappingURL, root.resolveUrl)
  15. }
  16. }(this, function(sourceMappingURL, resolveUrl) {
  17. function callbackAsync(callback, error, result) {
  18. setImmediate(function() { callback(error, result) })
  19. }
  20. function parseMapToJSON(string, data) {
  21. try {
  22. return JSON.parse(string.replace(/^\)\]\}'/, ""))
  23. } catch (error) {
  24. error.sourceMapData = data
  25. throw error
  26. }
  27. }
  28. function readSync(read, url, data) {
  29. var readUrl = url
  30. try {
  31. return String(read(readUrl))
  32. } catch (error) {
  33. error.sourceMapData = data
  34. throw error
  35. }
  36. }
  37. function resolveSourceMap(code, codeUrl, read, callback) {
  38. var mapData
  39. try {
  40. mapData = resolveSourceMapHelper(code, codeUrl)
  41. } catch (error) {
  42. return callbackAsync(callback, error)
  43. }
  44. if (!mapData || mapData.map) {
  45. return callbackAsync(callback, null, mapData)
  46. }
  47. var readUrl = mapData.url
  48. read(readUrl, function(error, result) {
  49. if (error) {
  50. error.sourceMapData = mapData
  51. return callback(error)
  52. }
  53. mapData.map = String(result)
  54. try {
  55. mapData.map = parseMapToJSON(mapData.map, mapData)
  56. } catch (error) {
  57. return callback(error)
  58. }
  59. callback(null, mapData)
  60. })
  61. }
  62. function resolveSourceMapSync(code, codeUrl, read) {
  63. var mapData = resolveSourceMapHelper(code, codeUrl)
  64. if (!mapData || mapData.map) {
  65. return mapData
  66. }
  67. mapData.map = readSync(read, mapData.url, mapData)
  68. mapData.map = parseMapToJSON(mapData.map, mapData)
  69. return mapData
  70. }
  71. var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/
  72. var jsonMimeTypeRegex = /^(?:application|text)\/json$/
  73. function resolveSourceMapHelper(code, codeUrl) {
  74. var url = sourceMappingURL.getFrom(code)
  75. if (!url) {
  76. return null
  77. }
  78. var dataUri = url.match(dataUriRegex)
  79. if (dataUri) {
  80. var mimeType = dataUri[1]
  81. var lastParameter = dataUri[2] || ""
  82. var encoded = dataUri[3] || ""
  83. var data = {
  84. sourceMappingURL: url,
  85. url: null,
  86. sourcesRelativeTo: codeUrl,
  87. map: encoded
  88. }
  89. if (!jsonMimeTypeRegex.test(mimeType)) {
  90. var error = new Error("Unuseful data uri mime type: " + (mimeType || "text/plain"))
  91. error.sourceMapData = data
  92. throw error
  93. }
  94. data.map = parseMapToJSON(
  95. lastParameter === ";base64" ? atob(encoded) : decodeURIComponent(encoded),
  96. data
  97. )
  98. return data
  99. }
  100. var mapUrl = resolveUrl(codeUrl, url)
  101. return {
  102. sourceMappingURL: url,
  103. url: mapUrl,
  104. sourcesRelativeTo: mapUrl,
  105. map: null
  106. }
  107. }
  108. function resolveSources(map, mapUrl, read, options, callback) {
  109. if (typeof options === "function") {
  110. callback = options
  111. options = {}
  112. }
  113. var pending = map.sources ? map.sources.length : 0
  114. var result = {
  115. sourcesResolved: [],
  116. sourcesContent: []
  117. }
  118. if (pending === 0) {
  119. callbackAsync(callback, null, result)
  120. return
  121. }
  122. var done = function() {
  123. pending--
  124. if (pending === 0) {
  125. callback(null, result)
  126. }
  127. }
  128. resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) {
  129. result.sourcesResolved[index] = fullUrl
  130. if (typeof sourceContent === "string") {
  131. result.sourcesContent[index] = sourceContent
  132. callbackAsync(done, null)
  133. } else {
  134. var readUrl = fullUrl
  135. read(readUrl, function(error, source) {
  136. result.sourcesContent[index] = error ? error : String(source)
  137. done()
  138. })
  139. }
  140. })
  141. }
  142. function resolveSourcesSync(map, mapUrl, read, options) {
  143. var result = {
  144. sourcesResolved: [],
  145. sourcesContent: []
  146. }
  147. if (!map.sources || map.sources.length === 0) {
  148. return result
  149. }
  150. resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) {
  151. result.sourcesResolved[index] = fullUrl
  152. if (read !== null) {
  153. if (typeof sourceContent === "string") {
  154. result.sourcesContent[index] = sourceContent
  155. } else {
  156. var readUrl = fullUrl
  157. try {
  158. result.sourcesContent[index] = String(read(readUrl))
  159. } catch (error) {
  160. result.sourcesContent[index] = error
  161. }
  162. }
  163. }
  164. })
  165. return result
  166. }
  167. var endingSlash = /\/?$/
  168. function resolveSourcesHelper(map, mapUrl, options, fn) {
  169. options = options || {}
  170. var fullUrl
  171. var sourceContent
  172. var sourceRoot
  173. for (var index = 0, len = map.sources.length; index < len; index++) {
  174. sourceRoot = null
  175. if (typeof options.sourceRoot === "string") {
  176. sourceRoot = options.sourceRoot
  177. } else if (typeof map.sourceRoot === "string" && options.sourceRoot !== false) {
  178. sourceRoot = map.sourceRoot
  179. }
  180. // If the sourceRoot is the empty string, it is equivalent to not setting
  181. // the property at all.
  182. if (sourceRoot === null || sourceRoot === '') {
  183. fullUrl = resolveUrl(mapUrl, map.sources[index])
  184. } else {
  185. // Make sure that the sourceRoot ends with a slash, so that `/scripts/subdir` becomes
  186. // `/scripts/subdir/<source>`, not `/scripts/<source>`. Pointing to a file as source root
  187. // does not make sense.
  188. fullUrl = resolveUrl(mapUrl, sourceRoot.replace(endingSlash, "/"), map.sources[index])
  189. }
  190. sourceContent = (map.sourcesContent || [])[index]
  191. fn(fullUrl, sourceContent, index)
  192. }
  193. }
  194. function resolve(code, codeUrl, read, options, callback) {
  195. if (typeof options === "function") {
  196. callback = options
  197. options = {}
  198. }
  199. if (code === null) {
  200. var mapUrl = codeUrl
  201. var data = {
  202. sourceMappingURL: null,
  203. url: mapUrl,
  204. sourcesRelativeTo: mapUrl,
  205. map: null
  206. }
  207. var readUrl = mapUrl
  208. read(readUrl, function(error, result) {
  209. if (error) {
  210. error.sourceMapData = data
  211. return callback(error)
  212. }
  213. data.map = String(result)
  214. try {
  215. data.map = parseMapToJSON(data.map, data)
  216. } catch (error) {
  217. return callback(error)
  218. }
  219. _resolveSources(data)
  220. })
  221. } else {
  222. resolveSourceMap(code, codeUrl, read, function(error, mapData) {
  223. if (error) {
  224. return callback(error)
  225. }
  226. if (!mapData) {
  227. return callback(null, null)
  228. }
  229. _resolveSources(mapData)
  230. })
  231. }
  232. function _resolveSources(mapData) {
  233. resolveSources(mapData.map, mapData.sourcesRelativeTo, read, options, function(error, result) {
  234. if (error) {
  235. return callback(error)
  236. }
  237. mapData.sourcesResolved = result.sourcesResolved
  238. mapData.sourcesContent = result.sourcesContent
  239. callback(null, mapData)
  240. })
  241. }
  242. }
  243. function resolveSync(code, codeUrl, read, options) {
  244. var mapData
  245. if (code === null) {
  246. var mapUrl = codeUrl
  247. mapData = {
  248. sourceMappingURL: null,
  249. url: mapUrl,
  250. sourcesRelativeTo: mapUrl,
  251. map: null
  252. }
  253. mapData.map = readSync(read, mapUrl, mapData)
  254. mapData.map = parseMapToJSON(mapData.map, mapData)
  255. } else {
  256. mapData = resolveSourceMapSync(code, codeUrl, read)
  257. if (!mapData) {
  258. return null
  259. }
  260. }
  261. var result = resolveSourcesSync(mapData.map, mapData.sourcesRelativeTo, read, options)
  262. mapData.sourcesResolved = result.sourcesResolved
  263. mapData.sourcesContent = result.sourcesContent
  264. return mapData
  265. }
  266. return {
  267. resolveSourceMap: resolveSourceMap,
  268. resolveSourceMapSync: resolveSourceMapSync,
  269. resolveSources: resolveSources,
  270. resolveSourcesSync: resolveSourcesSync,
  271. resolve: resolve,
  272. resolveSync: resolveSync,
  273. parseMapToJSON: parseMapToJSON
  274. }
  275. }));