MCPcopy Create free account
hub / github.com/SAP/ui5-builder / getSourceMapForModule

Method getSourceMapForModule

lib/lbt/bundle/Builder.js:667–751  ·  view source on GitHub ↗
({moduleName, moduleContent, resourcePath})

Source from the content-addressed store, hash-verified

665 }
666
667 async getSourceMapForModule({moduleName, moduleContent, resourcePath}) {
668 let moduleSourceMap = null;
669 let newModuleContent = moduleContent;
670
671 const sourceMapUrlMatch = moduleContent.match(sourceMappingUrlPattern);
672 if (sourceMapUrlMatch) {
673 const sourceMapUrl = sourceMapUrlMatch[1];
674 log.silly(`Found source map reference in content of module ${moduleName}: ${sourceMapUrl}`);
675
676 // Strip sourceMappingURL from module code to be bundled
677 // It has no effect and might be cause for confusion
678 newModuleContent = moduleContent.replace(sourceMappingUrlPattern, "");
679
680 if (sourceMapUrl) {
681 if (sourceMapUrl.startsWith("data:")) {
682 // Data-URI indicates an inline source map
683 const expectedTypeAndEncoding = "data:application/json;charset=utf-8;base64,";
684 if (sourceMapUrl.startsWith(expectedTypeAndEncoding)) {
685 const base64Content = sourceMapUrl.slice(expectedTypeAndEncoding.length);
686 moduleSourceMap = Buffer.from(base64Content, "base64").toString();
687 } else {
688 log.warn(
689 `Source map reference in module ${moduleName} is a data URI but has an unexpected` +
690 `encoding: ${sourceMapUrl}. Expected it to start with ` +
691 `"data:application/json;charset=utf-8;base64,"`);
692 }
693 } else if (httpPattern.test(sourceMapUrl)) {
694 log.warn(`Source map reference in module ${moduleName} is an absolute URL. ` +
695 `Currently, only relative URLs are supported.`);
696 } else if (path.posix.isAbsolute(sourceMapUrl)) {
697 log.warn(`Source map reference in module ${moduleName} is an absolute path. ` +
698 `Currently, only relative paths are supported.`);
699 } else {
700 const sourceMapPath = path.posix.join(path.posix.dirname(moduleName), sourceMapUrl);
701
702 try {
703 const sourceMapResource = await this.pool.findResource(sourceMapPath);
704 moduleSourceMap = (await sourceMapResource.buffer()).toString();
705 } catch (e) {
706 // No input source map
707 log.warn(`Unable to read source map for module ${moduleName}: ${e.message}`);
708 }
709 }
710 }
711 } else {
712 const sourceMapFileCandidate = resourcePath.slice("/resources/".length) + ".map";
713 log.silly(`Could not find a sourceMappingURL reference in content of module ${moduleName}. ` +
714 `Attempting to find a source map resource based on the module's path: ${sourceMapFileCandidate}`);
715 try {
716 const sourceMapResource = await this.pool.findResource(sourceMapFileCandidate);
717 moduleSourceMap = (await sourceMapResource.buffer()).toString();
718 } catch (e) {
719 // No input source map
720 log.silly(`Could not find a source map for module ${moduleName}: ${e.message}`);
721 }
722 }
723
724

Callers 4

writeRawModuleMethod · 0.95
rewriteAMDModulesMethod · 0.95
writePreloadModuleMethod · 0.95
Builder.jsFile · 0.80

Calls 5

createTransientSourceMapFunction · 0.85
fromMethod · 0.80
toStringMethod · 0.45
findResourceMethod · 0.45
bufferMethod · 0.45

Tested by

no test coverage detected