Skip to content

Commit

Permalink
Handle ignoredGlobals and no longer use ModuleInfo for the raw infos.
Browse files Browse the repository at this point in the history
  • Loading branch information
codeworrior authored and tobiasso85 committed Aug 28, 2020
1 parent 8d1df1d commit 7f64f15
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/lbt/bundle/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,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};`);
});
}
Expand Down
34 changes: 21 additions & 13 deletions lib/lbt/resources/LibraryFileAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) &&
Expand All @@ -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;
}
});
Expand Down
18 changes: 12 additions & 6 deletions lib/lbt/resources/ModuleInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -237,11 +248,6 @@ public class ModuleInfo {
private boolean excludeFromAllInOne;
public void removeIgnoredGlobalNames(Collection<String> ignoredNames) {
if ( !exposedGlobals.isEmpty() ) {
exposedGlobals.removeAll(ignoredNames);
}
}
} */

Expand Down
13 changes: 9 additions & 4 deletions lib/lbt/resources/ResourcePool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) ) {
Expand Down Expand Up @@ -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]);
}
Expand Down

0 comments on commit 7f64f15

Please sign in to comment.