| 105 | } |
| 106 | |
| 107 | export function compileTemplate( |
| 108 | options: SFCTemplateCompileOptions, |
| 109 | ): SFCTemplateCompileResults { |
| 110 | const { preprocessLang, preprocessCustomRequire } = options |
| 111 | |
| 112 | if ( |
| 113 | (__ESM_BROWSER__ || __GLOBAL__) && |
| 114 | preprocessLang && |
| 115 | !preprocessCustomRequire |
| 116 | ) { |
| 117 | throw new Error( |
| 118 | `[@vue/compiler-sfc] Template preprocessing in the browser build must ` + |
| 119 | `provide the \`preprocessCustomRequire\` option to return the in-browser ` + |
| 120 | `version of the preprocessor in the shape of { render(): string }.`, |
| 121 | ) |
| 122 | } |
| 123 | |
| 124 | const preprocessor = preprocessLang |
| 125 | ? preprocessCustomRequire |
| 126 | ? preprocessCustomRequire(preprocessLang) |
| 127 | : __ESM_BROWSER__ |
| 128 | ? undefined |
| 129 | : consolidate[preprocessLang as keyof typeof consolidate] |
| 130 | : false |
| 131 | if (preprocessor) { |
| 132 | try { |
| 133 | return doCompileTemplate({ |
| 134 | ...options, |
| 135 | source: preprocess(options, preprocessor), |
| 136 | ast: undefined, // invalidate AST if template goes through preprocessor |
| 137 | }) |
| 138 | } catch (e: any) { |
| 139 | return { |
| 140 | code: `export default function render() {}`, |
| 141 | source: options.source, |
| 142 | tips: [], |
| 143 | errors: [e], |
| 144 | } |
| 145 | } |
| 146 | } else if (preprocessLang) { |
| 147 | return { |
| 148 | code: `export default function render() {}`, |
| 149 | source: options.source, |
| 150 | tips: [ |
| 151 | `Component ${options.filename} uses lang ${preprocessLang} for template. Please install the language preprocessor.`, |
| 152 | ], |
| 153 | errors: [ |
| 154 | `Component ${options.filename} uses lang ${preprocessLang} for template, however it is not installed.`, |
| 155 | ], |
| 156 | } |
| 157 | } else { |
| 158 | return doCompileTemplate(options) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | function doCompileTemplate({ |
| 163 | filename, |