Skip to content

Commit

Permalink
wip: strip with
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 10, 2022
1 parent ed4bbed commit 22c457f
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 20 deletions.
33 changes: 15 additions & 18 deletions packages/compiler-sfc/src/compileTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import assetUrlsModule, {
import srcsetModule from './templateCompilerModules/srcset'
import consolidate from '@vue/consolidate'
import * as _compiler from 'web/entry-compiler'
import transpile from 'vue-template-es2015-compiler'
import { stripWith } from './stripWith'

export interface TemplateCompileOptions {
source: string
Expand All @@ -26,6 +26,7 @@ export interface TemplateCompileOptions {
isFunctional?: boolean
optimizeSSR?: boolean
prettify?: boolean
isTS?: boolean
}

export interface TemplateCompileResult {
Expand Down Expand Up @@ -108,7 +109,8 @@ function actuallyCompile(
isProduction = process.env.NODE_ENV === 'production',
isFunctional = false,
optimizeSSR = false,
prettify = true
prettify = true,
isTS = false
} = options

const compile =
Expand Down Expand Up @@ -142,25 +144,20 @@ function actuallyCompile(
errors
}
} else {
// TODO better transpile
const finalTranspileOptions = Object.assign({}, transpileOptions, {
transforms: Object.assign({}, transpileOptions.transforms, {
stripWithFunctional: isFunctional
})
})

const toFunction = (code: string): string => {
return `function (${isFunctional ? `_h,_vm` : ``}) {${code}}`
}

// transpile code with vue-template-es2015-compiler, which is a forked
// version of Buble that applies ES2015 transforms + stripping `with` usage
let code =
transpile(
`var __render__ = ${toFunction(render)}\n` +
`var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`,
finalTranspileOptions
) + `\n`
`var __render__ = ${stripWith(
render,
`render`,
isFunctional,
isTS,
transpileOptions
)}\n` +
`var __staticRenderFns__ = [${staticRenderFns.map(code =>
stripWith(code, ``, isFunctional, isTS, transpileOptions)
)}]` +
`\n`

// #23 we use __render__ to avoid `render` not being prefixed by the
// transpiler when stripping with, but revert it back to `render` to
Expand Down

0 comments on commit 22c457f

Please sign in to comment.