( template: string, instance: ComponentInternalInstance, )
| 20 | const compileCache: Record<string, SSRRenderFunction> = Object.create(null) |
| 21 | |
| 22 | export function ssrCompile( |
| 23 | template: string, |
| 24 | instance: ComponentInternalInstance, |
| 25 | ): SSRRenderFunction { |
| 26 | // TODO: this branch should now work in ESM builds, enable it in a minor |
| 27 | if (!__CJS__) { |
| 28 | throw new Error( |
| 29 | `On-the-fly template compilation is not supported in the ESM build of ` + |
| 30 | `@vue/server-renderer. All templates must be pre-compiled into ` + |
| 31 | `render functions.`, |
| 32 | ) |
| 33 | } |
| 34 | |
| 35 | // TODO: This is copied from runtime-core/src/component.ts and should probably be refactored |
| 36 | const Component = instance.type as ComponentOptions |
| 37 | const { isCustomElement, compilerOptions } = instance.appContext.config |
| 38 | const { delimiters, compilerOptions: componentCompilerOptions } = Component |
| 39 | |
| 40 | const finalCompilerOptions: CompilerOptions = extend( |
| 41 | extend( |
| 42 | { |
| 43 | isCustomElement, |
| 44 | delimiters, |
| 45 | }, |
| 46 | compilerOptions, |
| 47 | ), |
| 48 | componentCompilerOptions, |
| 49 | ) |
| 50 | |
| 51 | finalCompilerOptions.isCustomElement = |
| 52 | finalCompilerOptions.isCustomElement || NO |
| 53 | finalCompilerOptions.isNativeTag = finalCompilerOptions.isNativeTag || NO |
| 54 | |
| 55 | const cacheKey = JSON.stringify( |
| 56 | { |
| 57 | template, |
| 58 | compilerOptions: finalCompilerOptions, |
| 59 | }, |
| 60 | (key, value) => { |
| 61 | return isFunction(value) ? value.toString() : value |
| 62 | }, |
| 63 | ) |
| 64 | |
| 65 | const cached = compileCache[cacheKey] |
| 66 | if (cached) { |
| 67 | return cached |
| 68 | } |
| 69 | |
| 70 | finalCompilerOptions.onError = (err: CompilerError) => { |
| 71 | if (__DEV__) { |
| 72 | const message = `[@vue/server-renderer] Template compilation error: ${err.message}` |
| 73 | const codeFrame = |
| 74 | err.loc && |
| 75 | generateCodeFrame( |
| 76 | template as string, |
| 77 | err.loc.start.offset, |
| 78 | err.loc.end.offset, |
| 79 | ) |
no test coverage detected