From 21e6df58de5593eca8157dbf4889d2fe99f18739 Mon Sep 17 00:00:00 2001 From: Frank Weigel Date: Fri, 28 Aug 2020 09:23:56 +0200 Subject: [PATCH] Handle ignoredGlobals and no longer use ModuleInfo for the raw infos. --- lib/lbt/bundle/Builder.js | 1 + lib/lbt/resources/LibraryFileAnalyzer.js | 34 +++++++++++++++--------- lib/lbt/resources/ModuleInfo.js | 18 ++++++++----- lib/lbt/resources/ResourcePool.js | 13 ++++++--- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/lib/lbt/bundle/Builder.js b/lib/lbt/bundle/Builder.js index aa8dcfa79..ea059f307 100644 --- a/lib/lbt/bundle/Builder.js +++ b/lib/lbt/bundle/Builder.js @@ -458,6 +458,7 @@ class BundleBuilder { } this.outW.ensureNewLine(); info.exposedGlobals.forEach( (globalName) => { + // Note: globalName can be assumed to be a valid identifier as it is used as variable name anyhow this.outW.writeln(`this.${globalName}=${globalName};`); }); } diff --git a/lib/lbt/resources/LibraryFileAnalyzer.js b/lib/lbt/resources/LibraryFileAnalyzer.js index 66750f46f..35fe07190 100644 --- a/lib/lbt/resources/LibraryFileAnalyzer.js +++ b/lib/lbt/resources/LibraryFileAnalyzer.js @@ -5,7 +5,7 @@ "use strict"; const xml2js = require("xml2js"); -const ModuleInfo = require("./ModuleInfo"); +const log = require("@ui5/logger").getLogger("lbt:resources:LibraryFileAnalyzer"); const parser = new xml2js.Parser({ // explicitChildren: true, @@ -20,29 +20,36 @@ function getAttribute(node, attr) { return (node.$ && node.$[attr] && node.$[attr].value) || null; } -function makeModuleInfo(rawModule) { +/* + * Analyzes the given XML2JS object `rawModule` and creates a rawInfo object from it. + * @param {object} rawModule XML2JS object, representing a <raw-module> node from a .library file + * @returns {{name:string,dependencies?:string[],requiresTopLevelScope?:boolean,ignoredGlobals?:string[]} + */ +function createRawInfo(rawModule) { const name = getAttribute(rawModule, "name"); - const deps = getAttribute(rawModule, "depends"); if ( name ) { - const info = new ModuleInfo(name); + const rawInfo = { + name + }; + const deps = getAttribute(rawModule, "depends"); if ( deps != null ) { - deps.trim().split(/\s*,\s*/).forEach( (dep) => info.addDependency(dep) ); + rawInfo.dependencies = deps.trim().split(/\s*,\s*/); + } + const requiresTopLevelScope = getAttribute(rawModule, "requiresTopLevelScope"); + if ( requiresTopLevelScope ) { + rawInfo.requiresTopLevelScope = requiresTopLevelScope === "true"; } - info.rawModule = true; - info.requiresTopLevelScope = getAttribute(rawModule, "requiresTopLevelScope") === "true"; const ignoredGlobals = getAttribute(rawModule, "ignoredGlobals"); if ( ignoredGlobals ) { - info.ignoredGlobals = ignoredGlobals.trim().split(/\s*,\s*/); + rawInfo.ignoredGlobals = ignoredGlobals.trim().split(/\s*,\s*/); } - // console.log(info); - return info; + return rawInfo; } } -function getDependencyInfos( content ) { +function getDependencyInfos( name, content ) { const infos = {}; parser.parseString(content, (err, result) => { - // console.log(JSON.stringify(result, null, '\t')); if ( result && result.library && Array.isArray(result.library.appData) && @@ -54,8 +61,9 @@ function getDependencyInfos( content ) { Array.isArray(packaging["module-infos"]) ) { packaging["module-infos"].forEach( function(moduleInfos) { moduleInfos["raw-module"] && moduleInfos["raw-module"].forEach( (rawModule) => { - const info = makeModuleInfo(rawModule); + const info = createRawInfo(rawModule); if ( info ) { + log.verbose(" rawInfo:", JSON.stringify(rawInfo)); infos[info.name] = info; } }); diff --git a/lib/lbt/resources/ModuleInfo.js b/lib/lbt/resources/ModuleInfo.js index 7fa60a9ab..a669089b5 100644 --- a/lib/lbt/resources/ModuleInfo.js +++ b/lib/lbt/resources/ModuleInfo.js @@ -95,7 +95,7 @@ class ModuleInfo { * * @returns Whether the module requires top level scope. */ - this.requiresTopLevelScope = undefined; + this.requiresTopLevelScope = false; /** * Global names that the module exposes intentionally and that should be exported @@ -202,6 +202,17 @@ class ModuleInfo { return Object.keys(this._dependencies); } + /** + * Removes the given set of `ignoredGlobals` from the set of exposed global names. + * @param {string[]} ignoredGlobals Names to ignore (determined from shims in .library) + */ + removeIgnoredGlobalNames(ignoredGlobals) { + if ( this.exposedGlobals ) { + const remaining = this.exposedGlobals.filter((global) => !ignoredGlobals.includes(global)); + this.exposedGlobals = remaining.length > 0 ? remaining : null; + } + } + toString() { return "ModuleInfo(" + this.name + @@ -237,11 +248,6 @@ public class ModuleInfo { private boolean excludeFromAllInOne; - public void removeIgnoredGlobalNames(Collection ignoredNames) { - if ( !exposedGlobals.isEmpty() ) { - exposedGlobals.removeAll(ignoredNames); - } - } } */ diff --git a/lib/lbt/resources/ResourcePool.js b/lib/lbt/resources/ResourcePool.js index 5b18fc702..eed73541e 100644 --- a/lib/lbt/resources/ResourcePool.js +++ b/lib/lbt/resources/ResourcePool.js @@ -75,12 +75,17 @@ async function determineDependencyInfo(resource, rawInfo, pool) { if ( rawInfo ) { info.rawModule = true; // console.log("adding preconfigured dependencies for %s:", resource.name, rawInfo.dependencies); - rawInfo.dependencies.forEach( (dep) => info.addDependency(dep) ); - if ( rawInfo.requiresTopLevelScope !== undefined) { + if ( rawInfo.dependencies ) { + rawInfo.dependencies.forEach( (dep) => info.addDependency(dep) ); + } + + if ( rawInfo.requiresTopLevelScope != null ) { + // an explicitly defined value for requiresTopLevelScope from .library overrides analysis result info.requiresTopLevelScope = rawInfo.requiresTopLevelScope; } + if ( rawInfo.ignoredGlobals ) { - info.ignoredGlobals = rawInfo.ignoredGlobals; + info.removeIgnoredGlobalNames(rawInfo.ignoredGlobals); } } if ( /(?:^|\/)Component\.js/.test(resource.name) ) { @@ -153,7 +158,7 @@ class ResourcePool { if ( /\.library$/.test(resource.name) ) { // read raw-module info from .library files return resource.buffer().then( (buffer) => { - const infos = LibraryFileAnalyzer.getDependencyInfos( buffer ); + const infos = LibraryFileAnalyzer.getDependencyInfos( resource.name, buffer ); for ( const name of Object.keys(infos) ) { this._rawModuleInfos.set(name, infos[name]); }