Skip to content

Lazy load for components, filters and directives (fix #8106) #8807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/instance/render-helpers/resolve-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { identity, resolveAsset } from 'core/util/index'
* Runtime helper for resolving filters
*/
export function resolveFilter (id: string): Function {
return resolveAsset(this.$options, 'filters', id, true) || identity
return resolveAsset(this, 'filters', id, true) || identity
}
14 changes: 11 additions & 3 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
import {
extend,
hasOwn,
camelize,
toRawType,
camelize,
capitalize,
identifierSpellings,
isBuiltInTag,
isPlainObject
} from 'shared/util'
Expand Down Expand Up @@ -410,7 +411,7 @@ export function mergeOptions (
* to assets defined in its ancestor chain.
*/
export function resolveAsset (
options: Object,
context: Component,
type: string,
id: string,
warnMissing?: boolean
Expand All @@ -419,6 +420,7 @@ export function resolveAsset (
if (typeof id !== 'string') {
return
}
const options = context.$options
const assets = options[type]
// check local registration variations first
if (hasOwn(assets, id)) return assets[id]
Expand All @@ -427,7 +429,13 @@ export function resolveAsset (
const PascalCaseId = capitalize(camelizedId)
if (hasOwn(assets, PascalCaseId)) return assets[PascalCaseId]
// fallback to prototype chain
const res = assets[id] || assets[camelizedId] || assets[PascalCaseId]
let res = assets[id] || assets[camelizedId] || assets[PascalCaseId]
if (!res) {
const getAsset = options['get' + capitalize(type.slice(0, -1))]
if (getAsset instanceof Function) {
res = getAsset.call(context, id, identifierSpellings(id))
}
}
if (process.env.NODE_ENV !== 'production' && warnMissing && !res) {
warn(
'Failed to resolve ' + type.slice(0, -1) + ': ' + id,
Expand Down
2 changes: 1 addition & 1 deletion src/core/vdom/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function _createElement (
config.parsePlatformTagName(tag), data, children,
undefined, undefined, context
)
} else if (isDef(Ctor = resolveAsset(context.$options, 'components', tag))) {
} else if (isDef(Ctor = resolveAsset(context, 'components', tag))) {
// component
vnode = createComponent(Ctor, data, context, children, tag)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/core/vdom/modules/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function normalizeDirectives (
dir.modifiers = emptyModifiers
}
res[getRawDirName(dir)] = dir
dir.def = resolveAsset(vm.$options, 'directives', dir.name, true)
dir.def = resolveAsset(vm, 'directives', dir.name, true)
}
// $flow-disable-line
return res
Expand Down
14 changes: 14 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ export const hyphenate = cached((str: string): string => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
})

/**
* Get all variations of a identifier spelling.
*/
export const identifierSpellings = cached((str: string): Object => {
const camelized = camelize(str)
return {
raw: str,
hyphenated: hyphenate(str),
camelized: camelized,
PascalCase: capitalize(camelized),
toString: () => str
}
})

/**
* Simple bind polyfill for environments that do not support it... e.g.
* PhantomJS 1.x. Technically we don't need this anymore since native bind is
Expand Down