Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

map-generator.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. 'use strict'
  2. let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
  3. let { dirname, relative, resolve, sep } = require('path')
  4. let { pathToFileURL } = require('url')
  5. let Input = require('./input')
  6. let sourceMapAvailable = Boolean(SourceMapConsumer && SourceMapGenerator)
  7. let pathAvailable = Boolean(dirname && resolve && relative && sep)
  8. class MapGenerator {
  9. constructor(stringify, root, opts, cssString) {
  10. this.stringify = stringify
  11. this.mapOpts = opts.map || {}
  12. this.root = root
  13. this.opts = opts
  14. this.css = cssString
  15. this.originalCSS = cssString
  16. this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute
  17. this.memoizedFileURLs = new Map()
  18. this.memoizedPaths = new Map()
  19. this.memoizedURLs = new Map()
  20. }
  21. addAnnotation() {
  22. let content
  23. if (this.isInline()) {
  24. content =
  25. 'data:application/json;base64,' + this.toBase64(this.map.toString())
  26. } else if (typeof this.mapOpts.annotation === 'string') {
  27. content = this.mapOpts.annotation
  28. } else if (typeof this.mapOpts.annotation === 'function') {
  29. content = this.mapOpts.annotation(this.opts.to, this.root)
  30. } else {
  31. content = this.outputFile() + '.map'
  32. }
  33. let eol = '\n'
  34. if (this.css.includes('\r\n')) eol = '\r\n'
  35. this.css += eol + '/*# sourceMappingURL=' + content + ' */'
  36. }
  37. applyPrevMaps() {
  38. for (let prev of this.previous()) {
  39. let from = this.toUrl(this.path(prev.file))
  40. let root = prev.root || dirname(prev.file)
  41. let map
  42. if (this.mapOpts.sourcesContent === false) {
  43. map = new SourceMapConsumer(prev.text)
  44. if (map.sourcesContent) {
  45. map.sourcesContent = null
  46. }
  47. } else {
  48. map = prev.consumer()
  49. }
  50. this.map.applySourceMap(map, from, this.toUrl(this.path(root)))
  51. }
  52. }
  53. clearAnnotation() {
  54. if (this.mapOpts.annotation === false) return
  55. if (this.root) {
  56. let node
  57. for (let i = this.root.nodes.length - 1; i >= 0; i--) {
  58. node = this.root.nodes[i]
  59. if (node.type !== 'comment') continue
  60. if (node.text.indexOf('# sourceMappingURL=') === 0) {
  61. this.root.removeChild(i)
  62. }
  63. }
  64. } else if (this.css) {
  65. this.css = this.css.replace(/\n*\/\*#[\S\s]*?\*\/$/gm, '')
  66. }
  67. }
  68. generate() {
  69. this.clearAnnotation()
  70. if (pathAvailable && sourceMapAvailable && this.isMap()) {
  71. return this.generateMap()
  72. } else {
  73. let result = ''
  74. this.stringify(this.root, i => {
  75. result += i
  76. })
  77. return [result]
  78. }
  79. }
  80. generateMap() {
  81. if (this.root) {
  82. this.generateString()
  83. } else if (this.previous().length === 1) {
  84. let prev = this.previous()[0].consumer()
  85. prev.file = this.outputFile()
  86. this.map = SourceMapGenerator.fromSourceMap(prev, {
  87. ignoreInvalidMapping: true
  88. })
  89. } else {
  90. this.map = new SourceMapGenerator({
  91. file: this.outputFile(),
  92. ignoreInvalidMapping: true
  93. })
  94. this.map.addMapping({
  95. generated: { column: 0, line: 1 },
  96. original: { column: 0, line: 1 },
  97. source: this.opts.from
  98. ? this.toUrl(this.path(this.opts.from))
  99. : '<no source>'
  100. })
  101. }
  102. if (this.isSourcesContent()) this.setSourcesContent()
  103. if (this.root && this.previous().length > 0) this.applyPrevMaps()
  104. if (this.isAnnotation()) this.addAnnotation()
  105. if (this.isInline()) {
  106. return [this.css]
  107. } else {
  108. return [this.css, this.map]
  109. }
  110. }
  111. generateString() {
  112. this.css = ''
  113. this.map = new SourceMapGenerator({
  114. file: this.outputFile(),
  115. ignoreInvalidMapping: true
  116. })
  117. let line = 1
  118. let column = 1
  119. let noSource = '<no source>'
  120. let mapping = {
  121. generated: { column: 0, line: 0 },
  122. original: { column: 0, line: 0 },
  123. source: ''
  124. }
  125. let lines, last
  126. this.stringify(this.root, (str, node, type) => {
  127. this.css += str
  128. if (node && type !== 'end') {
  129. mapping.generated.line = line
  130. mapping.generated.column = column - 1
  131. if (node.source && node.source.start) {
  132. mapping.source = this.sourcePath(node)
  133. mapping.original.line = node.source.start.line
  134. mapping.original.column = node.source.start.column - 1
  135. this.map.addMapping(mapping)
  136. } else {
  137. mapping.source = noSource
  138. mapping.original.line = 1
  139. mapping.original.column = 0
  140. this.map.addMapping(mapping)
  141. }
  142. }
  143. lines = str.match(/\n/g)
  144. if (lines) {
  145. line += lines.length
  146. last = str.lastIndexOf('\n')
  147. column = str.length - last
  148. } else {
  149. column += str.length
  150. }
  151. if (node && type !== 'start') {
  152. let p = node.parent || { raws: {} }
  153. let childless =
  154. node.type === 'decl' || (node.type === 'atrule' && !node.nodes)
  155. if (!childless || node !== p.last || p.raws.semicolon) {
  156. if (node.source && node.source.end) {
  157. mapping.source = this.sourcePath(node)
  158. mapping.original.line = node.source.end.line
  159. mapping.original.column = node.source.end.column - 1
  160. mapping.generated.line = line
  161. mapping.generated.column = column - 2
  162. this.map.addMapping(mapping)
  163. } else {
  164. mapping.source = noSource
  165. mapping.original.line = 1
  166. mapping.original.column = 0
  167. mapping.generated.line = line
  168. mapping.generated.column = column - 1
  169. this.map.addMapping(mapping)
  170. }
  171. }
  172. }
  173. })
  174. }
  175. isAnnotation() {
  176. if (this.isInline()) {
  177. return true
  178. }
  179. if (typeof this.mapOpts.annotation !== 'undefined') {
  180. return this.mapOpts.annotation
  181. }
  182. if (this.previous().length) {
  183. return this.previous().some(i => i.annotation)
  184. }
  185. return true
  186. }
  187. isInline() {
  188. if (typeof this.mapOpts.inline !== 'undefined') {
  189. return this.mapOpts.inline
  190. }
  191. let annotation = this.mapOpts.annotation
  192. if (typeof annotation !== 'undefined' && annotation !== true) {
  193. return false
  194. }
  195. if (this.previous().length) {
  196. return this.previous().some(i => i.inline)
  197. }
  198. return true
  199. }
  200. isMap() {
  201. if (typeof this.opts.map !== 'undefined') {
  202. return !!this.opts.map
  203. }
  204. return this.previous().length > 0
  205. }
  206. isSourcesContent() {
  207. if (typeof this.mapOpts.sourcesContent !== 'undefined') {
  208. return this.mapOpts.sourcesContent
  209. }
  210. if (this.previous().length) {
  211. return this.previous().some(i => i.withContent())
  212. }
  213. return true
  214. }
  215. outputFile() {
  216. if (this.opts.to) {
  217. return this.path(this.opts.to)
  218. } else if (this.opts.from) {
  219. return this.path(this.opts.from)
  220. } else {
  221. return 'to.css'
  222. }
  223. }
  224. path(file) {
  225. if (this.mapOpts.absolute) return file
  226. if (file.charCodeAt(0) === 60 /* `<` */) return file
  227. if (/^\w+:\/\//.test(file)) return file
  228. let cached = this.memoizedPaths.get(file)
  229. if (cached) return cached
  230. let from = this.opts.to ? dirname(this.opts.to) : '.'
  231. if (typeof this.mapOpts.annotation === 'string') {
  232. from = dirname(resolve(from, this.mapOpts.annotation))
  233. }
  234. let path = relative(from, file)
  235. this.memoizedPaths.set(file, path)
  236. return path
  237. }
  238. previous() {
  239. if (!this.previousMaps) {
  240. this.previousMaps = []
  241. if (this.root) {
  242. this.root.walk(node => {
  243. if (node.source && node.source.input.map) {
  244. let map = node.source.input.map
  245. if (!this.previousMaps.includes(map)) {
  246. this.previousMaps.push(map)
  247. }
  248. }
  249. })
  250. } else {
  251. let input = new Input(this.originalCSS, this.opts)
  252. if (input.map) this.previousMaps.push(input.map)
  253. }
  254. }
  255. return this.previousMaps
  256. }
  257. setSourcesContent() {
  258. let already = {}
  259. if (this.root) {
  260. this.root.walk(node => {
  261. if (node.source) {
  262. let from = node.source.input.from
  263. if (from && !already[from]) {
  264. already[from] = true
  265. let fromUrl = this.usesFileUrls
  266. ? this.toFileUrl(from)
  267. : this.toUrl(this.path(from))
  268. this.map.setSourceContent(fromUrl, node.source.input.css)
  269. }
  270. }
  271. })
  272. } else if (this.css) {
  273. let from = this.opts.from
  274. ? this.toUrl(this.path(this.opts.from))
  275. : '<no source>'
  276. this.map.setSourceContent(from, this.css)
  277. }
  278. }
  279. sourcePath(node) {
  280. if (this.mapOpts.from) {
  281. return this.toUrl(this.mapOpts.from)
  282. } else if (this.usesFileUrls) {
  283. return this.toFileUrl(node.source.input.from)
  284. } else {
  285. return this.toUrl(this.path(node.source.input.from))
  286. }
  287. }
  288. toBase64(str) {
  289. if (Buffer) {
  290. return Buffer.from(str).toString('base64')
  291. } else {
  292. return window.btoa(unescape(encodeURIComponent(str)))
  293. }
  294. }
  295. toFileUrl(path) {
  296. let cached = this.memoizedFileURLs.get(path)
  297. if (cached) return cached
  298. if (pathToFileURL) {
  299. let fileURL = pathToFileURL(path).toString()
  300. this.memoizedFileURLs.set(path, fileURL)
  301. return fileURL
  302. } else {
  303. throw new Error(
  304. '`map.absolute` option is not available in this PostCSS build'
  305. )
  306. }
  307. }
  308. toUrl(path) {
  309. let cached = this.memoizedURLs.get(path)
  310. if (cached) return cached
  311. if (sep === '\\') {
  312. path = path.replace(/\\/g, '/')
  313. }
  314. let url = encodeURI(path).replace(/[#?]/g, encodeURIComponent)
  315. this.memoizedURLs.set(path, url)
  316. return url
  317. }
  318. }
  319. module.exports = MapGenerator