|
| 1 | +import { |
| 2 | + VueTemplateCompiler, |
| 3 | + VueTemplateCompilerOptions |
| 4 | +} from './types' |
| 5 | + |
| 6 | +import assetUrlsModule, { AssetURLOptions } from './templateCompilerModules/assetUrl' |
| 7 | +import srcsetModule from './templateCompilerModules/srcset' |
| 8 | + |
| 9 | +const prettier = require('prettier') |
| 10 | +const consolidate = require('consolidate') |
| 11 | +const transpile = require('vue-template-es2015-compiler') |
| 12 | + |
| 13 | +export interface TemplateCompileOptions { |
| 14 | + source: string |
| 15 | + filename: string |
| 16 | + compiler: VueTemplateCompiler |
| 17 | + compilerOptions?: VueTemplateCompilerOptions |
| 18 | + transformAssetUrls?: AssetURLOptions | boolean |
| 19 | + preprocessLang?: string |
| 20 | + preprocessOptions?: any |
| 21 | + transpileOptions?: any |
| 22 | + isProduction?: boolean |
| 23 | + isFunctional?: boolean |
| 24 | + optimizeSSR?: boolean |
| 25 | +} |
| 26 | + |
| 27 | +export interface TemplateCompileResult { |
| 28 | + code: string |
| 29 | + source: string |
| 30 | + tips: string[] |
| 31 | + errors: string[] |
| 32 | +} |
| 33 | + |
| 34 | +export function compileTemplate ( |
| 35 | + options: TemplateCompileOptions |
| 36 | +): TemplateCompileResult { |
| 37 | + const { preprocessLang } = options |
| 38 | + const preprocessor = preprocessLang && consolidate[preprocessLang] |
| 39 | + if (preprocessor) { |
| 40 | + return actuallyCompile(Object.assign({}, options, { |
| 41 | + source: preprocess(options, preprocessor) |
| 42 | + })) |
| 43 | + } else { |
| 44 | + return actuallyCompile(options) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function preprocess ( |
| 49 | + options: TemplateCompileOptions, |
| 50 | + preprocessor: any |
| 51 | +): string { |
| 52 | + const { |
| 53 | + source, |
| 54 | + filename, |
| 55 | + preprocessOptions |
| 56 | + } = options |
| 57 | + |
| 58 | + const finalPreprocessOptions = Object.assign({ |
| 59 | + filename |
| 60 | + }, preprocessOptions) |
| 61 | + |
| 62 | + // Consolidate exposes a callback based API, but the callback is in fact |
| 63 | + // called synchronously for most templating engines. In our case, we have to |
| 64 | + // expose a synchronous API so that it is usable in Jest transforms (which |
| 65 | + // have to be sync because they are applied via Node.js require hooks) |
| 66 | + let res: any, err |
| 67 | + preprocessor.render( |
| 68 | + source, |
| 69 | + finalPreprocessOptions, |
| 70 | + (_err: Error | null, _res: string) => { |
| 71 | + if (_err) err = _err |
| 72 | + res = _res |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + if (err) throw err |
| 77 | + return res |
| 78 | +} |
| 79 | + |
| 80 | +function actuallyCompile ( |
| 81 | + options: TemplateCompileOptions |
| 82 | +): TemplateCompileResult { |
| 83 | + const { |
| 84 | + source, |
| 85 | + compiler, |
| 86 | + compilerOptions = {}, |
| 87 | + transpileOptions = {}, |
| 88 | + transformAssetUrls, |
| 89 | + isProduction = process.env.NODE_ENV === 'production', |
| 90 | + isFunctional = false, |
| 91 | + optimizeSSR = false |
| 92 | + } = options |
| 93 | + |
| 94 | + const compile = optimizeSSR && compiler.ssrCompile |
| 95 | + ? compiler.ssrCompile |
| 96 | + : compiler.compile |
| 97 | + |
| 98 | + let finalCompilerOptions = compilerOptions |
| 99 | + if (transformAssetUrls) { |
| 100 | + const builtInModules = [ |
| 101 | + transformAssetUrls === true |
| 102 | + ? assetUrlsModule() |
| 103 | + : assetUrlsModule(transformAssetUrls), |
| 104 | + srcsetModule() |
| 105 | + ] |
| 106 | + finalCompilerOptions = Object.assign({}, compilerOptions, { |
| 107 | + modules: [...builtInModules, ...compilerOptions.modules || []] |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + const { |
| 112 | + render, |
| 113 | + staticRenderFns, |
| 114 | + tips, |
| 115 | + errors |
| 116 | + } = compile(source, finalCompilerOptions) |
| 117 | + |
| 118 | + if (errors && errors.length) { |
| 119 | + return { |
| 120 | + code: ( |
| 121 | + `var render = function () {}\n` + |
| 122 | + `var staticRenderFns = []\n` |
| 123 | + ), |
| 124 | + source, |
| 125 | + tips, |
| 126 | + errors |
| 127 | + } |
| 128 | + } else { |
| 129 | + const finalTranspileOptions = Object.assign({}, transpileOptions, { |
| 130 | + transforms: Object.assign({}, transpileOptions.transforms, { |
| 131 | + stripWithFunctional: isFunctional |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + const toFunction = (code: string): string => { |
| 136 | + return `function (${isFunctional ? `_h,_vm` : ``}) {${code}}` |
| 137 | + } |
| 138 | + |
| 139 | + // transpile code with vue-template-es2015-compiler, which is a forked |
| 140 | + // version of Buble that applies ES2015 transforms + stripping `with` usage |
| 141 | + let code = transpile( |
| 142 | + `var render = ${toFunction(render)}\n` + |
| 143 | + `var staticRenderFns = [${staticRenderFns.map(toFunction)}]`, |
| 144 | + finalTranspileOptions |
| 145 | + ) + `\n` |
| 146 | + |
| 147 | + if (!isProduction) { |
| 148 | + // mark with stripped (this enables Vue to use correct runtime proxy |
| 149 | + // detection) |
| 150 | + code += `render._withStripped = true` |
| 151 | + code = prettier.format(code, { semi: false }) |
| 152 | + } |
| 153 | + |
| 154 | + return { |
| 155 | + code, |
| 156 | + source, |
| 157 | + tips, |
| 158 | + errors |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments