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.

123 lines
2.7 KiB

4 years ago
  1. import { SourceMapGenerator } from 'source-map'
  2. import {
  3. RawSourceMap,
  4. VueTemplateCompiler,
  5. VueTemplateCompilerParseOptions
  6. } from './types'
  7. const hash = require('hash-sum')
  8. const cache = require('lru-cache')(100)
  9. const splitRE = /\r?\n/g
  10. const emptyRE = /^(?:\/\/)?\s*$/
  11. export interface ParseOptions {
  12. source: string
  13. filename?: string
  14. compiler: VueTemplateCompiler
  15. compilerParseOptions?: VueTemplateCompilerParseOptions
  16. sourceRoot?: string
  17. needMap?: boolean
  18. }
  19. export interface SFCCustomBlock {
  20. type: string
  21. content: string
  22. attrs: { [key: string]: string | true }
  23. start: number
  24. end: number
  25. map?: RawSourceMap
  26. }
  27. export interface SFCBlock extends SFCCustomBlock {
  28. lang?: string
  29. src?: string
  30. scoped?: boolean
  31. module?: string | boolean
  32. }
  33. export interface SFCDescriptor {
  34. template: SFCBlock | null
  35. script: SFCBlock | null
  36. styles: SFCBlock[]
  37. customBlocks: SFCCustomBlock[]
  38. }
  39. export function parse(options: ParseOptions): SFCDescriptor {
  40. const {
  41. source,
  42. filename = '',
  43. compiler,
  44. compilerParseOptions = { pad: 'line' } as VueTemplateCompilerParseOptions,
  45. sourceRoot = '',
  46. needMap = true
  47. } = options
  48. const cacheKey = hash(filename + source)
  49. let output: SFCDescriptor = cache.get(cacheKey)
  50. if (output) return output
  51. output = compiler.parseComponent(source, compilerParseOptions)
  52. if (needMap) {
  53. if (output.script && !output.script.src) {
  54. output.script.map = generateSourceMap(
  55. filename,
  56. source,
  57. output.script.content,
  58. sourceRoot,
  59. compilerParseOptions.pad
  60. )
  61. }
  62. if (output.styles) {
  63. output.styles.forEach(style => {
  64. if (!style.src) {
  65. style.map = generateSourceMap(
  66. filename,
  67. source,
  68. style.content,
  69. sourceRoot,
  70. compilerParseOptions.pad
  71. )
  72. }
  73. })
  74. }
  75. }
  76. cache.set(cacheKey, output)
  77. return output
  78. }
  79. function generateSourceMap(
  80. filename: string,
  81. source: string,
  82. generated: string,
  83. sourceRoot: string,
  84. pad?: 'line' | 'space'
  85. ): RawSourceMap {
  86. const map = new SourceMapGenerator({
  87. file: filename.replace(/\\/g, '/'),
  88. sourceRoot: sourceRoot.replace(/\\/g, '/')
  89. })
  90. let offset = 0
  91. if (!pad) {
  92. offset =
  93. source
  94. .split(generated)
  95. .shift()!
  96. .split(splitRE).length - 1
  97. }
  98. map.setSourceContent(filename, source)
  99. generated.split(splitRE).forEach((line, index) => {
  100. if (!emptyRE.test(line)) {
  101. map.addMapping({
  102. source: filename,
  103. original: {
  104. line: index + 1 + offset,
  105. column: 0
  106. },
  107. generated: {
  108. line: index + 1,
  109. column: 0
  110. }
  111. })
  112. }
  113. })
  114. return JSON.parse(map.toString())
  115. }