MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / loadCustomComponent

Function loadCustomComponent

packages/core/ui/builder/index.ts:208–266  ·  view source on GitHub ↗
(componentNamespace: string, componentName?: string, attributes?: Object, context?: Object, parentPage?: View, isRootComponent = true, moduleNamePath?: string)

Source from the content-addressed store, hash-verified

206}
207
208export function loadCustomComponent(componentNamespace: string, componentName?: string, attributes?: Object, context?: Object, parentPage?: View, isRootComponent = true, moduleNamePath?: string): ComponentModule {
209 if (!parentPage && context) {
210 // Read the parent page that was passed down below
211 // https://github.com/NativeScript/NativeScript/issues/1639
212 parentPage = context['_parentPage'];
213 delete context['_parentPage'];
214 }
215
216 let result: ComponentModule;
217
218 componentNamespace = sanitizeModuleName(componentNamespace);
219 const moduleName = `${componentNamespace}/${componentName}`;
220 const resolvedCodeModuleName = resolveModuleName(moduleName, '');
221 const resolvedXmlModuleName = resolveModuleName(moduleName, 'xml');
222 let resolvedCssModuleName = resolveModuleName(moduleName, 'css');
223
224 if (resolvedXmlModuleName) {
225 // Custom components with XML
226 let subExports = context;
227 if (resolvedCodeModuleName) {
228 // Component has registered code module.
229 subExports = global.loadModule(resolvedCodeModuleName);
230 }
231
232 // Pass the parent page down the chain in case of custom components nested on many levels. Use the context for piggybacking.
233 // https://github.com/NativeScript/NativeScript/issues/1639
234 if (!subExports) {
235 subExports = {};
236 }
237
238 subExports['_parentPage'] = parentPage;
239
240 result = loadInternal(moduleName, subExports);
241
242 // Attributes will be transferred to the custom component
243 if (isDefined(result) && isDefined(result.component) && isDefined(attributes)) {
244 for (const attr in attributes) {
245 setPropertyValue(result.component, subExports, context, attr, attributes[attr]);
246 }
247 }
248 } else {
249 // Custom components without XML
250 result = getComponentModule(componentName, componentNamespace, attributes, context, moduleNamePath, isRootComponent);
251
252 // The namespace is the JS module and the (componentName is the name of the class in the module)
253 // So if there is no componentNamespace/componentName.{qualifiers}.css we should also look for
254 // componentNamespace.{qualifiers}.css
255 if (!resolvedCssModuleName) {
256 resolvedCssModuleName = resolveModuleName(componentNamespace, 'css');
257 }
258 }
259
260 // Add CSS from webpack module if exists.
261 if (parentPage && resolvedCssModuleName) {
262 parentPage.addCssFile(resolvedCssModuleName);
263 }
264
265 return result;

Callers 2

loadMethod · 0.85
buildComponentMethod · 0.85

Calls 7

sanitizeModuleNameFunction · 0.90
resolveModuleNameFunction · 0.90
isDefinedFunction · 0.90
setPropertyValueFunction · 0.90
getComponentModuleFunction · 0.90
loadInternalFunction · 0.85
addCssFileMethod · 0.80

Tested by

no test coverage detected