From 44584f19832f148251b56527833dfb509d101c16 Mon Sep 17 00:00:00 2001 From: Dan Bissonnet Date: Tue, 6 Sep 2022 16:49:21 +0100 Subject: [PATCH 1/4] Fix sourcemap implementation SourceMapGenerator returns a Promise which we need to wait for before getting each mapping --- index.js | 59 ++++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index e89988d..07f1eeb 100644 --- a/index.js +++ b/index.js @@ -14,10 +14,10 @@ module.exports = function (params) { var SourceMapConsumer = require('source-map').SourceMapConsumer; var extensions = null, // The extension to be searched after - globalIncludedFiles = [], // To track what files have been included over all files + globalIncludedFiles = [], // For track of what files have been included over all files includePaths = false, // The paths to be searched hardFail = false, // Throw error when no match - separateInputs = false; // Process each input file separately when using `require` directive + separateInputs = false; // Process each input file separately when using `require` logic. // Check for includepaths in the params if (params.includePaths) { @@ -155,7 +155,7 @@ module.exports = function (params) { var fileMatches = []; var includePath = ""; - if (includePaths != false && !isExplicitRelativePath(split[1])) { + if (includePaths != false) { // If includepaths are set, search in those folders for (var y = 0; y < includePaths.length; y++) { includePath = includePaths[y] + "/" + split[1]; @@ -167,7 +167,7 @@ module.exports = function (params) { } } else { // Otherwise search relatively - includePath = relativeBasePath + "/" + removeRelativePathPrefix(split[1]); + includePath = relativeBasePath + "/" + split[1]; var globResults = glob.sync(includePath, { mark: true }); @@ -203,28 +203,30 @@ module.exports = function (params) { if (result.map.mappings && result.map.mappings.length > 0) { var resultMap = new SourceMapConsumer(result.map); - resultMap.eachMapping(function (mapping) { - if (!mapping.source) return; - - map.addMapping({ - generated: { - line: mapping.generatedLine + currentLine - 1, - column: mapping.generatedColumn + (leadingWhitespace ? leadingWhitespace.length : 0) - }, - original: { - line: mapping.originalLine, - column: mapping.originalColumn - }, - source: mapping.source, - name: mapping.name + resultMap.then(map => { + map.eachMapping(function (mapping) { + if (!mapping.source) return; + + map.addMapping({ + generated: { + line: mapping.generatedLine + currentLine - 1, + column: mapping.generatedColumn + (leadingWhitespace ? leadingWhitespace.length : 0) + }, + original: { + line: mapping.originalLine, + column: mapping.originalColumn + }, + source: mapping.source, + name: mapping.name + }); + + if (map.sourcesContent) { + map.sourcesContent.forEach(function (sourceContent, i) { + map.setSourceContent(map.sources[i], sourceContent); + }); + } }); }); - - if (result.map.sourcesContent) { - result.map.sourcesContent.forEach(function (sourceContent, i) { - map.setSourceContent(result.map.sources[i], sourceContent); - }); - } } } else { // result was a simple file, map whole file to new location for (var q = 0; q < lines; q++) { @@ -302,13 +304,6 @@ module.exports = function (params) { }).join("\n"); } - function isExplicitRelativePath(filePath) { - return filePath.indexOf('./') === 0; - } - - function removeRelativePathPrefix(filePath) { - return filePath.replace(/^\.\//, ''); - } function fileNotFoundError(includePath) { if (hardFail) { throw new PluginError('gulp-include', 'No files found matching ' + includePath); @@ -331,4 +326,4 @@ module.exports = function (params) { } return es.map(include) -}; \ No newline at end of file +}; From aef8aa1ebaf65b55b03a2f4cd05c5d38c1ae76b5 Mon Sep 17 00:00:00 2001 From: Dan Bissonnet Date: Tue, 6 Sep 2022 16:53:58 +0100 Subject: [PATCH 2/4] Revert some accidental changes --- index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 07f1eeb..43f8086 100644 --- a/index.js +++ b/index.js @@ -14,10 +14,10 @@ module.exports = function (params) { var SourceMapConsumer = require('source-map').SourceMapConsumer; var extensions = null, // The extension to be searched after - globalIncludedFiles = [], // For track of what files have been included over all files + globalIncludedFiles = [], // To track what files have been included over all files includePaths = false, // The paths to be searched hardFail = false, // Throw error when no match - separateInputs = false; // Process each input file separately when using `require` logic. + separateInputs = false; // Process each input file separately when using `require` directive // Check for includepaths in the params if (params.includePaths) { @@ -155,7 +155,7 @@ module.exports = function (params) { var fileMatches = []; var includePath = ""; - if (includePaths != false) { + if (includePaths != false && !isExplicitRelativePath(split[1])) { // If includepaths are set, search in those folders for (var y = 0; y < includePaths.length; y++) { includePath = includePaths[y] + "/" + split[1]; @@ -167,7 +167,7 @@ module.exports = function (params) { } } else { // Otherwise search relatively - includePath = relativeBasePath + "/" + split[1]; + includePath = relativeBasePath + "/" + removeRelativePathPrefix(split[1]); var globResults = glob.sync(includePath, { mark: true }); @@ -304,6 +304,13 @@ module.exports = function (params) { }).join("\n"); } + function isExplicitRelativePath(filePath) { + return filePath.indexOf('./') === 0; + } + + function removeRelativePathPrefix(filePath) { + return filePath.replace(/^\.\//, ''); + } function fileNotFoundError(includePath) { if (hardFail) { throw new PluginError('gulp-include', 'No files found matching ' + includePath); From e7a11cfd74a624e96870459a0364ec35565fd048 Mon Sep 17 00:00:00 2001 From: Dan Bissonnet Date: Wed, 7 Sep 2022 08:14:28 +0100 Subject: [PATCH 3/4] Avoid variable collision --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 43f8086..1774af5 100644 --- a/index.js +++ b/index.js @@ -203,8 +203,8 @@ module.exports = function (params) { if (result.map.mappings && result.map.mappings.length > 0) { var resultMap = new SourceMapConsumer(result.map); - resultMap.then(map => { - map.eachMapping(function (mapping) { + resultMap.then(consumer => { + consumer.eachMapping(function (mapping) { if (!mapping.source) return; map.addMapping({ From 06330987ab755aa81c7b30ea01ff8963daa4a23d Mon Sep 17 00:00:00 2001 From: Dan Bissonnet Date: Fri, 9 Sep 2022 08:32:08 +0100 Subject: [PATCH 4/4] Set sources content only once --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 1774af5..ab81d82 100644 --- a/index.js +++ b/index.js @@ -219,13 +219,13 @@ module.exports = function (params) { source: mapping.source, name: mapping.name }); - - if (map.sourcesContent) { - map.sourcesContent.forEach(function (sourceContent, i) { - map.setSourceContent(map.sources[i], sourceContent); - }); - } }); + + if (map.sourcesContent) { + map.sourcesContent.forEach(function (sourceContent, i) { + map.setSourceContent(map.sources[i], sourceContent); + }); + } }); } } else { // result was a simple file, map whole file to new location