From d83af0134838ae52f0428eade989f85f093c058c Mon Sep 17 00:00:00 2001 From: sergioperezcheco Date: Mon, 19 Oct 2026 12:11:22 +0800 Subject: [PATCH] fix: resolve source map URLs relative to CLI output Assisted-by: Hermes AI Signed-off-by: sergioperezcheco --- lib/precompiler.js | 19 ++++++-- spec/precompiler.js | 112 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/lib/precompiler.js b/lib/precompiler.js index a8e69d6c..a237f311 100644 --- a/lib/precompiler.js +++ b/lib/precompiler.js @@ -2,7 +2,8 @@ import Async from 'neo-async'; import fs from 'fs'; import Handlebars from './handlebars.js'; -import { basename } from 'path'; +import { basename, dirname, parse, relative, resolve, sep } from 'path'; +import { pathToFileURL } from 'url'; import { SourceMapConsumer, SourceNode } from 'source-map'; export function loadTemplates(opts, callback) { @@ -254,15 +255,27 @@ export async function cli(opts) { output.add('})();'); } + let sourceMapUrl = opts.map; + if (opts.map && opts.output) { + const mapPath = resolve(opts.map); + const outputDirectory = dirname(resolve(opts.output)); + const relativePath = relative(outputDirectory, mapPath); + // Different Windows drives or UNC shares need an absolute file URL. + sourceMapUrl = + parse(outputDirectory).root.toLowerCase() !== + parse(mapPath).root.toLowerCase() + ? pathToFileURL(mapPath).href + : relativePath.split(sep).map(encodeURIComponent).join('/'); + } if (opts.map) { - output.add('\n//# sourceMappingURL=' + opts.map + '\n'); + output.add('\n//# sourceMappingURL=' + sourceMapUrl + '\n'); } output = output.toStringWithSourceMap(); output.map = output.map + ''; if (opts.min) { - output = await minify(output, opts.map); + output = await minify(output, sourceMapUrl); } if (opts.map) { diff --git a/spec/precompiler.js b/spec/precompiler.js index 3c4d3b8f..1a54e295 100644 --- a/spec/precompiler.js +++ b/spec/precompiler.js @@ -197,6 +197,118 @@ describe('precompiler', function () { expect(log.match(/sourceMappingURL=/g).length).toBe(1); }); + it.each([false, true])( + 'should resolve the map relative to the output file (min: %s)', + async function (min) { + var writes = {}; + fs.writeFileSync = function (name, value) { + writes[name] = value; + }; + await Precompiler.cli({ + templates: [emptyTemplate], + output: 'dist/templates.js', + map: 'maps/templates.js.map', + min: min, + }); + expect(writes['maps/templates.js.map']).toBeDefined(); + expect( + writes['dist/templates.js'].match(/sourceMappingURL=(.*)/)[1] + ).toBe('../maps/templates.js.map'); + } + ); + + it.each([false, true])( + 'should resolve an absolute map path relative to the output (min: %s)', + async function (min) { + var writes = {}; + fs.writeFileSync = function (name, value) { + writes[name] = value; + }; + var output = path.resolve('dist/templates.js'); + var map = path.resolve('dist/templates.js.map'); + await Precompiler.cli({ + templates: [emptyTemplate], + output: output, + map: map, + min: min, + }); + expect(writes[map]).toBeDefined(); + expect(writes[output].match(/sourceMappingURL=(.*)/)[1]).toBe( + 'templates.js.map' + ); + } + ); + + it.each([false, true])( + 'should URL-encode map filename components (min: %s)', + async function (min) { + var writes = {}; + fs.writeFileSync = function (name, value) { + writes[name] = value; + }; + await Precompiler.cli({ + templates: [emptyTemplate], + output: 'dist/templates.js', + map: 'dist/template #1%.js.map', + min: min, + }); + expect(writes['dist/template #1%.js.map']).toBeDefined(); + expect( + writes['dist/templates.js'].match(/sourceMappingURL=(.*)/)[1] + ).toBe('template%20%231%25.js.map'); + } + ); + + describe.runIf(process.platform === 'win32')('Windows map URLs', function () { + it.each([ + [ + 'C:\\dist\\templates.js', + 'E:\\maps\\template #1%.map', + 'file:///E:/maps/template%20%231%25.map', + ], + [ + 'C:\\dist\\templates.js', + 'c:\\maps\\templates.map', + '../maps/templates.map', + ], + [ + '\\\\server\\share\\dist\\templates.js', + '\\\\server\\share\\maps\\templates.map', + '../maps/templates.map', + ], + [ + '\\\\server\\share\\dist\\templates.js', + '\\\\server\\other\\maps\\templates.map', + 'file://server/other/maps/templates.map', + ], + [ + '\\\\server\\share\\dist\\templates.js', + '\\\\other\\share\\maps\\templates.map', + 'file://other/share/maps/templates.map', + ], + [ + 'C:\\dist\\templates.js', + '\\\\server\\share\\maps\\templates.map', + 'file://server/share/maps/templates.map', + ], + [ + '\\\\server\\share\\dist\\templates.js', + 'E:\\maps\\templates.map', + 'file:///E:/maps/templates.map', + ], + ])('should link %s to %s', async function (output, map, expected) { + for (const min of [false, true]) { + const writes = {}; + fs.writeFileSync = function (name, value) { + writes[name] = value; + }; + await Precompiler.cli({ templates: [emptyTemplate], output, map, min }); + expect(writes[map]).toBeDefined(); + expect(writes[output].match(/sourceMappingURL=(.*)/)[1]).toBe(expected); + } + }); + }); + describe('#loadTemplates', function () { function loadTemplatesAsync(inputOpts) { return new Promise(function (resolve, reject) {