|
@@ -1,176 +0,0 @@
|
|
|
-import { VueTemplateCompiler, VueTemplateCompilerOptions } from './types'
|
|
|
-
|
|
|
-import assetUrlsModule, {
|
|
|
- AssetURLOptions
|
|
|
-} from './templateCompilerModules/assetUrl'
|
|
|
-import srcsetModule from './templateCompilerModules/srcset'
|
|
|
-
|
|
|
-const prettier = require('prettier')
|
|
|
-const consolidate = require('consolidate')
|
|
|
-const transpile = require('vue-template-es2015-compiler')
|
|
|
-
|
|
|
-export interface TemplateCompileOptions {
|
|
|
- source: string
|
|
|
- filename: string
|
|
|
- compiler: VueTemplateCompiler
|
|
|
- compilerOptions?: VueTemplateCompilerOptions
|
|
|
- transformAssetUrls?: AssetURLOptions | boolean
|
|
|
- preprocessLang?: string
|
|
|
- preprocessOptions?: any
|
|
|
- transpileOptions?: any
|
|
|
- isProduction?: boolean
|
|
|
- isFunctional?: boolean
|
|
|
- optimizeSSR?: boolean
|
|
|
-}
|
|
|
-
|
|
|
-export interface TemplateCompileResult {
|
|
|
- code: string
|
|
|
- source: string
|
|
|
- tips: string[]
|
|
|
- errors: string[]
|
|
|
-}
|
|
|
-
|
|
|
-export function compileTemplate(
|
|
|
- options: TemplateCompileOptions
|
|
|
-): TemplateCompileResult {
|
|
|
- const { preprocessLang } = options
|
|
|
- const preprocessor = preprocessLang && consolidate[preprocessLang]
|
|
|
- if (preprocessor) {
|
|
|
- return actuallyCompile(
|
|
|
- Object.assign({}, options, {
|
|
|
- source: preprocess(options, preprocessor)
|
|
|
- })
|
|
|
- )
|
|
|
- } else if (preprocessLang) {
|
|
|
- return {
|
|
|
- code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
|
|
|
- source: options.source,
|
|
|
- tips: [
|
|
|
- `Component ${
|
|
|
- options.filename
|
|
|
- } uses lang ${preprocessLang} for template. Please install the language preprocessor.`
|
|
|
- ],
|
|
|
- errors: [
|
|
|
- `Component ${
|
|
|
- options.filename
|
|
|
- } uses lang ${preprocessLang} for template, however it is not installed.`
|
|
|
- ]
|
|
|
- }
|
|
|
- } else {
|
|
|
- return actuallyCompile(options)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function preprocess(
|
|
|
- options: TemplateCompileOptions,
|
|
|
- preprocessor: any
|
|
|
-): string {
|
|
|
- const { source, filename, preprocessOptions } = options
|
|
|
-
|
|
|
- const finalPreprocessOptions = Object.assign(
|
|
|
- {
|
|
|
- filename
|
|
|
- },
|
|
|
- preprocessOptions
|
|
|
- )
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- let res: any, err
|
|
|
- preprocessor.render(
|
|
|
- source,
|
|
|
- finalPreprocessOptions,
|
|
|
- (_err: Error | null, _res: string) => {
|
|
|
- if (_err) err = _err
|
|
|
- res = _res
|
|
|
- }
|
|
|
- )
|
|
|
-
|
|
|
- if (err) throw err
|
|
|
- return res
|
|
|
-}
|
|
|
-
|
|
|
-function actuallyCompile(
|
|
|
- options: TemplateCompileOptions
|
|
|
-): TemplateCompileResult {
|
|
|
- const {
|
|
|
- source,
|
|
|
- compiler,
|
|
|
- compilerOptions = {},
|
|
|
- transpileOptions = {},
|
|
|
- transformAssetUrls,
|
|
|
- isProduction = process.env.NODE_ENV === 'production',
|
|
|
- isFunctional = false,
|
|
|
- optimizeSSR = false
|
|
|
- } = options
|
|
|
-
|
|
|
- const compile =
|
|
|
- optimizeSSR && compiler.ssrCompile ? compiler.ssrCompile : compiler.compile
|
|
|
-
|
|
|
- let finalCompilerOptions = compilerOptions
|
|
|
- if (transformAssetUrls) {
|
|
|
- const builtInModules = [
|
|
|
- transformAssetUrls === true
|
|
|
- ? assetUrlsModule()
|
|
|
- : assetUrlsModule(transformAssetUrls),
|
|
|
- srcsetModule()
|
|
|
- ]
|
|
|
- finalCompilerOptions = Object.assign({}, compilerOptions, {
|
|
|
- modules: [...builtInModules, ...(compilerOptions.modules || [])]
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- const { render, staticRenderFns, tips, errors } = compile(
|
|
|
- source,
|
|
|
- finalCompilerOptions
|
|
|
- )
|
|
|
-
|
|
|
- if (errors && errors.length) {
|
|
|
- return {
|
|
|
- code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
|
|
|
- source,
|
|
|
- tips,
|
|
|
- errors
|
|
|
- }
|
|
|
- } else {
|
|
|
- const finalTranspileOptions = Object.assign({}, transpileOptions, {
|
|
|
- transforms: Object.assign({}, transpileOptions.transforms, {
|
|
|
- stripWithFunctional: isFunctional
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
- const toFunction = (code: string): string => {
|
|
|
- return `function (${isFunctional ? `_h,_vm` : ``}) {${code}}`
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- let code =
|
|
|
- transpile(
|
|
|
- `var __render__ = ${toFunction(render)}\n` +
|
|
|
- `var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`,
|
|
|
- finalTranspileOptions
|
|
|
- ) + `\n`
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- code = code.replace(/\s__(render|staticRenderFns)__\s/g, ' $1 ')
|
|
|
-
|
|
|
- if (!isProduction) {
|
|
|
-
|
|
|
-
|
|
|
- code += `render._withStripped = true`
|
|
|
- code = prettier.format(code, { semi: false, parser: 'babylon' })
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- code,
|
|
|
- source,
|
|
|
- tips,
|
|
|
- errors
|
|
|
- }
|
|
|
- }
|
|
|
-}
|