feat(compiler-sfc): properly pass on options
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import {
|
||||
CompilerOptions,
|
||||
CodegenResult,
|
||||
CompilerError
|
||||
CompilerError,
|
||||
NodeTransform
|
||||
} from '@vue/compiler-core'
|
||||
import { RawSourceMap } from 'source-map'
|
||||
import { transformAssetUrl } from './templateTransformAssetUrl'
|
||||
import {
|
||||
transformAssetUrl,
|
||||
AssetURLOptions,
|
||||
createAssetUrlTransformWithOptions
|
||||
} from './templateTransformAssetUrl'
|
||||
import { transformSrcset } from './templateTransformSrcset'
|
||||
|
||||
const consolidate = require('consolidate')
|
||||
import { isObject } from '@vue/shared'
|
||||
import consolidate from 'consolidate'
|
||||
|
||||
export interface TemplateCompileResults {
|
||||
code: string
|
||||
@@ -28,6 +33,7 @@ export interface TemplateCompileOptions {
|
||||
compilerOptions?: CompilerOptions
|
||||
preprocessLang?: string
|
||||
preprocessOptions?: any
|
||||
transformAssetUrls?: AssetURLOptions | boolean
|
||||
}
|
||||
|
||||
function preprocess(
|
||||
@@ -56,7 +62,8 @@ export function compileTemplate(
|
||||
options: TemplateCompileOptions
|
||||
): TemplateCompileResults {
|
||||
const { preprocessLang } = options
|
||||
const preprocessor = preprocessLang && consolidate[preprocessLang]
|
||||
const preprocessor =
|
||||
preprocessLang && consolidate[preprocessLang as keyof typeof consolidate]
|
||||
if (preprocessor) {
|
||||
return doCompileTemplate({
|
||||
...options,
|
||||
@@ -83,18 +90,29 @@ export function compileTemplate(
|
||||
}
|
||||
|
||||
function doCompileTemplate({
|
||||
filename,
|
||||
source,
|
||||
compiler,
|
||||
compilerOptions = {},
|
||||
filename
|
||||
transformAssetUrls
|
||||
}: TemplateCompileOptions): TemplateCompileResults {
|
||||
const errors: CompilerError[] = []
|
||||
|
||||
const nodeTransforms: NodeTransform[] = [transformSrcset]
|
||||
if (isObject(transformAssetUrls)) {
|
||||
nodeTransforms.push(createAssetUrlTransformWithOptions(transformAssetUrls))
|
||||
} else if (transformAssetUrls !== false) {
|
||||
nodeTransforms.push(transformAssetUrl)
|
||||
}
|
||||
|
||||
const { code, map } = compiler.compile(source, {
|
||||
...compilerOptions,
|
||||
filename,
|
||||
mode: 'module',
|
||||
mode: 'module', // implies prefixIdentifiers: true
|
||||
hoistStatic: true,
|
||||
cacheHandlers: true,
|
||||
sourceMap: true,
|
||||
nodeTransforms: [transformAssetUrl, transformSrcset],
|
||||
nodeTransforms,
|
||||
onError: e => errors.push(e)
|
||||
})
|
||||
return { code, source, errors, tips: [], map }
|
||||
|
||||
@@ -13,7 +13,7 @@ export interface AssetURLOptions {
|
||||
[name: string]: string[]
|
||||
}
|
||||
|
||||
const assetURLOptions: AssetURLOptions = {
|
||||
const defaultOptions: AssetURLOptions = {
|
||||
video: ['src', 'poster'],
|
||||
source: ['src'],
|
||||
img: ['src'],
|
||||
@@ -21,11 +21,26 @@ const assetURLOptions: AssetURLOptions = {
|
||||
use: ['xlink:href', 'href']
|
||||
}
|
||||
|
||||
export const transformAssetUrl: NodeTransform = (node, context) => {
|
||||
export const createAssetUrlTransformWithOptions = (
|
||||
options: AssetURLOptions
|
||||
): NodeTransform => {
|
||||
const mergedOptions = {
|
||||
...defaultOptions,
|
||||
...options
|
||||
}
|
||||
return (node, context) =>
|
||||
(transformAssetUrl as Function)(node, context, mergedOptions)
|
||||
}
|
||||
|
||||
export const transformAssetUrl: NodeTransform = (
|
||||
node,
|
||||
context,
|
||||
options: AssetURLOptions = defaultOptions
|
||||
) => {
|
||||
if (node.type === NodeTypes.ELEMENT) {
|
||||
for (const tag in assetURLOptions) {
|
||||
for (const tag in options) {
|
||||
if ((tag === '*' || node.tag === tag) && node.props.length) {
|
||||
const attributes = assetURLOptions[tag]
|
||||
const attributes = options[tag]
|
||||
attributes.forEach(item => {
|
||||
node.props.forEach((attr: AttributeNode, index) => {
|
||||
if (attr.type !== NodeTypes.ATTRIBUTE) return
|
||||
|
||||
Reference in New Issue
Block a user