diff --git a/tools/isobuild/import-scanner.js b/tools/isobuild/import-scanner.js index dca37871a0..daa06dec37 100644 --- a/tools/isobuild/import-scanner.js +++ b/tools/isobuild/import-scanner.js @@ -249,19 +249,6 @@ export default class ImportScanner { // something plausible. #6411 #6383 const absPath = pathJoin(this.sourceRoot, file.sourcePath); - const dotExt = "." + file.type; - const dataString = file.data.toString("utf8"); - file.dataString = defaultExtensionHandlers[dotExt].call( - file, - dataString, - file.hash, - ); - - if (! (file.data instanceof Buffer) || - file.dataString !== dataString) { - file.data = Buffer.from(file.dataString, "utf8"); - } - // This property can have values false, true, "dynamic" (which // indicates that the file has been imported, but only dynamically). file.imported = false; @@ -302,9 +289,10 @@ export default class ImportScanner { const files = this.realPathToFiles[realPath]; if (files && files.length > 0) { const firstFile = files[0]; + const dataString = this._getDataString(firstFile); return { data: firstFile.data, - dataString: firstFile.dataString, + dataString: dataString, hash: firstFile.hash, }; } @@ -417,7 +405,7 @@ export default class ImportScanner { // plugin calling inputFile.addJavaScript multiple times for the // same source file (see discussion in #9176), with different target // paths, code, laziness, etc. - sourceFile.dataString += [ + sourceFile.dataString = this._getDataString(sourceFile) + [ "module.watch(require(" + JSON.stringify(relativeId) + "), {", ' "*": module.makeNsSetter(true)', "});", @@ -437,6 +425,8 @@ export default class ImportScanner { // maps and updating all other properties appropriately. Once this // combination is done, oldFile should be kept and newFile discarded. _combineFiles(oldFile, newFile) { + const scanner = this; + function checkProperty(name) { if (has(oldFile, name)) { if (! has(newFile, name)) { @@ -469,8 +459,11 @@ export default class ImportScanner { const consumer = file.sourceMap && new SourceMapConsumer(file.sourceMap); const node = consumer && - SourceNode.fromStringWithSourceMap(file.dataString, consumer); - return node || file.dataString; + SourceNode.fromStringWithSourceMap( + scanner._getDataString(file), + consumer + ); + return node || scanner._getDataString(file); } const { @@ -728,7 +721,7 @@ export default class ImportScanner { } const result = findImportedModuleIdentifiers( - file.dataString, + this._getDataString(file), file.hash, ); @@ -913,6 +906,27 @@ export default class ImportScanner { return archMatches(this.bundleArch, "web.browser"); } + _getDataString(file) { + if (typeof file.dataString === "string") { + return file.dataString; + } + + const dotExt = "." + file.type; + const dataString = file.data.toString("utf8"); + file.dataString = defaultExtensionHandlers[dotExt].call( + file, + dataString, + file.hash, + ); + + if (! (file.data instanceof Buffer) || + file.dataString !== dataString) { + file.data = Buffer.from(file.dataString, "utf8"); + } + + return file.dataString; + } + _readFile(absPath) { const contents = optimisticReadFile(absPath); const hash = optimisticHashOrNull(absPath);