| 105 | | LRUCache<string, SFCParseResult> = createCache<SFCParseResult>() |
| 106 | |
| 107 | export function parse( |
| 108 | source: string, |
| 109 | options: SFCParseOptions = {}, |
| 110 | ): SFCParseResult { |
| 111 | const sourceKey = genCacheKey(source, { |
| 112 | ...options, |
| 113 | compiler: { parse: options.compiler?.parse }, |
| 114 | }) |
| 115 | const cache = parseCache.get(sourceKey) |
| 116 | if (cache) { |
| 117 | return cache |
| 118 | } |
| 119 | |
| 120 | const { |
| 121 | sourceMap = true, |
| 122 | filename = DEFAULT_FILENAME, |
| 123 | sourceRoot = '', |
| 124 | pad = false, |
| 125 | ignoreEmpty = true, |
| 126 | compiler = CompilerDOM, |
| 127 | templateParseOptions = {}, |
| 128 | } = options |
| 129 | |
| 130 | const descriptor: SFCDescriptor = { |
| 131 | filename, |
| 132 | source, |
| 133 | template: null, |
| 134 | script: null, |
| 135 | scriptSetup: null, |
| 136 | styles: [], |
| 137 | customBlocks: [], |
| 138 | cssVars: [], |
| 139 | slotted: false, |
| 140 | shouldForceReload: prevImports => hmrShouldReload(prevImports, descriptor), |
| 141 | } |
| 142 | |
| 143 | const errors: (CompilerError | SyntaxError)[] = [] |
| 144 | const ast = compiler.parse(source, { |
| 145 | parseMode: 'sfc', |
| 146 | prefixIdentifiers: true, |
| 147 | ...templateParseOptions, |
| 148 | onError: e => { |
| 149 | errors.push(e) |
| 150 | }, |
| 151 | }) |
| 152 | ast.children.forEach(node => { |
| 153 | if (node.type !== NodeTypes.ELEMENT) { |
| 154 | return |
| 155 | } |
| 156 | // we only want to keep the nodes that are not empty |
| 157 | // (when the tag is not a template) |
| 158 | if ( |
| 159 | ignoreEmpty && |
| 160 | node.tag !== 'template' && |
| 161 | isEmpty(node) && |
| 162 | !hasSrc(node) |
| 163 | ) { |
| 164 | return |