Skip to content

Commit e3c1247

Browse files
committed
[PoC] ResourcePool: Add option to prefer analyzing a resource's source content
Possible follow up of #551 to improve dependency analysis. JSModuleAnalyzer has trouble detecting some dependencies in minified code if they have been "mangled". E.g. in case jQuery is not named jQuery anymore. This change solves that by trying to retrieve the debug ("source") content of a resource to analyze that instead of the minified version. However, integrating this on the ResourcePool level is not ideal and a more general solution to this problem should be preferred. Therefore this PR should only act as an illustration of the discussions and thoughts. Currently it should not be merged.
1 parent 40a9032 commit e3c1247

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Diff for: lib/lbt/resources/ResourceCollector.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,14 @@ class ResourceCollector {
108108
}
109109
}
110110

111-
async enrichWithDependencyInfo(resourceInfo) {
112-
return this._pool.getModuleInfo(resourceInfo.name).then((moduleInfo) => {
111+
async enrichWithDependencyInfo({resourceInfo, debugBundleFilter}) {
112+
return this._pool.getModuleInfo(resourceInfo.name, {
113+
// While analyzing non-debug resources, try to analyze the content of the corresponding
114+
// debug resource instead
115+
preferDebugResources: true,
116+
// Provide the debugBundleFilter to prevent that debug bundles are "preferred"
117+
debugBundleFilter
118+
}).then((moduleInfo) => {
113119
if ( moduleInfo.name ) {
114120
resourceInfo.module = moduleInfo.name;
115121
}
@@ -192,7 +198,10 @@ class ResourceCollector {
192198
if ( (!info.isDebug || debugBundleFilter.matches(name)) ) {
193199
// Only analyze non-debug files which are not special debug bundles (like sap-ui-core-dbg.js)
194200
promises.push(
195-
this.enrichWithDependencyInfo(info)
201+
this.enrichWithDependencyInfo({
202+
resourceInfo: info,
203+
debugBundleFilter
204+
})
196205
);
197206
} else {
198207
nonBundledDebugResources.push(info);

Diff for: lib/lbt/resources/ResourcePool.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const XMLTemplateAnalyzer = require("../analyzer/XMLTemplateAnalyzer");
1515
const LibraryFileAnalyzer = require("./LibraryFileAnalyzer");
1616
const ModuleInfo = require("./ModuleInfo");
1717
const ResourceFilterList = require("./ResourceFilterList");
18+
const ResourceInfoList = require("./ResourceInfoList");
1819
/*
1920
const Resource = require("./Resource");
2021
*/
@@ -57,12 +58,15 @@ function scanFileOrDir(fileOrDir, name, pool) {
5758
}
5859
*/
5960

60-
async function determineDependencyInfo(resource, rawInfo, pool) {
61+
async function determineDependencyInfo(resource, dbgResource, rawInfo, pool) {
6162
const info = new ModuleInfo(resource.name);
6263
info.size = resource.fileSize;
6364
if ( /\.js$/.test(resource.name) ) {
64-
// console.log("analyzing %s", resource.file);
65-
const code = await resource.buffer();
65+
// console.log("analyzing %s", resource.name);
66+
67+
// Retrieve the content from the "debug resource".
68+
// Note that in many cases "resource" and "dbgResource" are the same object
69+
const code = await dbgResource.buffer();
6670
info.size = code.length;
6771
const promises = [];
6872
let ast;
@@ -177,14 +181,30 @@ class ResourcePool {
177181
* Retrieves the module info
178182
*
179183
* @param {string} name module name
184+
* @param {object} options
185+
* @param {boolean} options.preferDebugResources
186+
* @param {ResourceFilterList} options.debugBundleFilter
180187
* @returns {Promise<ModuleInfo>}
181188
*/
182-
async getModuleInfo(name) {
189+
async getModuleInfo(name, options) {
183190
let info = this._dependencyInfos.get(name);
184191
if ( info == null ) {
185192
// console.log("analyzing ", name);
193+
let dbgResource;
194+
if (options && options.preferDebugResources) {
195+
// If requested, try to retrieve the corresponding debug-resource (xyz-dbg.js) and pass it to
196+
// determineDependencyInfo as well. Its dependency analysis will perform better with the
197+
// not-minified content of a resource.
198+
const dbgName = ResourceInfoList.getDebugName(name);
199+
if (dbgName && (!options.debugBundleFilter || !options.debugBundleFilter.matches(dbgName))) {
200+
dbgResource = this._resourcesByName.get(dbgName);
201+
}
202+
}
186203
const resource = await this.findResource(name);
187-
info = await determineDependencyInfo( resource, this._rawModuleInfos.get(name), this );
204+
if (!dbgResource) {
205+
dbgResource = resource;
206+
}
207+
info = await determineDependencyInfo(resource, dbgResource, this._rawModuleInfos.get(name), this);
188208
// console.log("finished analyzing ", name);
189209
this._dependencyInfos.set(name, info);
190210
}

0 commit comments

Comments
 (0)