From 301329a977218d0e049caafaf147e91f46ec6857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nenad=20Vi=C4=87enti=C4=87?= Date: Sun, 20 Jul 2025 12:03:59 +0200 Subject: [PATCH 1/2] fix #1212: part 1/2 extract `mapSourceRoot` calculation to a function `calculateRelativeSourceMapsRoot(...)`, exported with prefix `_`, write unit tests that break. --- lib/build/bundle.js | 22 ++++++++++++++---- spec/lib/build/bundle.spec.js | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/build/bundle.js b/lib/build/bundle.js index b8ef64c54..264deef8f 100644 --- a/lib/build/bundle.js +++ b/lib/build/bundle.js @@ -364,10 +364,7 @@ exports.Bundle = class { } let mapFileName = bundleFileName + '.map'; - let mapSourceRoot = path.posix.relative( - path.posix.join(process.cwd(), platform.output), - process.cwd() - ); + let mapSourceRoot = calculateRelativeSourceMapsRoot(process.cwd(), platform.output); logger.info(`Writing ${bundleFileName}...`); @@ -531,3 +528,20 @@ function uniqueBy(collection, key) { return seen.hasOwnProperty(k) ? false : (seen[k] = true); }); } + +/** + * Returns a POSIX-style relative path from `outputDir` back to `projectRoot`. + * Works on Windows, macOS and Linux. + * + * @param {string} projectRootDir - The root directory of the project. + * @param {string} outputDir - The output directory where the files are generated. + * @returns {string} A POSIX-style relative path from `outputDir` to `projectRoot`. + */ +function calculateRelativeSourceMapsRoot(projectRootDir, outputDir){ + return path.posix.relative( + path.posix.join(projectRootDir, outputDir), + projectRootDir + ); +} + +exports._calculateRelativeSourceMapsRoot = calculateRelativeSourceMapsRoot; diff --git a/spec/lib/build/bundle.spec.js b/spec/lib/build/bundle.spec.js index df972f420..9ea4288ad 100644 --- a/spec/lib/build/bundle.spec.js +++ b/spec/lib/build/bundle.spec.js @@ -1,5 +1,6 @@ const BundlerMock = require('../../mocks/bundler'); const Bundle = require('../../../lib/build/bundle').Bundle; +const _calculateRelativeSourceMapsRoot = require('../../../lib/build/bundle')._calculateRelativeSourceMapsRoot; const CLIOptionsMock = require('../../mocks/cli-options'); const DependencyDescription = require('../../../lib/build/dependency-description').DependencyDescription; const SourceInclusion = require('../../../lib/build/source-inclusion').SourceInclusion; @@ -459,3 +460,45 @@ describe('the Bundle module', () => { }); }); }); + +describe('function _calculateRelativeSourceMapsRoot', () => { + const testCases = [ + // Basic UNIX cases + { projectDir: '/usr/home/my-app', outputDir: './dist', expected: '..' }, + { projectDir: '/usr/home/my-app/', outputDir: './dist/', expected: '..' }, + { projectDir: '/usr/home/my-app', outputDir: 'dist', expected: '..' }, + { projectDir: '/usr/home/my-app/', outputDir: 'dist/', expected: '..' }, + // Basic Windows cases + { projectDir: 'C:/My Documents/MyApp', outputDir: './dist', expected: '..' }, + { projectDir: 'C:/My Documents/MyApp/', outputDir: './dist/', expected: '..' }, + { projectDir: 'C:/My Documents/MyApp', outputDir: 'dist', expected: '..' }, + { projectDir: 'C:/My Documents/MyApp/', outputDir: 'dist/', expected: '..' }, + // Basic Windows cases with backslashes + { projectDir: 'C:\\My Documents\\MyApp', outputDir: '.\\dist', expected: '..' }, + { projectDir: 'C:\\My Documents\\MyApp\\', outputDir: '.\\dist\\', expected: '..' }, + { projectDir: 'C:\\My Documents\\MyApp', outputDir: 'dist', expected: '..' }, + { projectDir: 'C:\\My Documents\\MyApp\\', outputDir: 'dist\\', expected: '..' }, + // Windows mixed slashes + { projectDir: 'C:\\My Documents\\MyApp', outputDir: './dist', expected: '..' }, + { projectDir: 'C:\\My Documents\\MyApp\\', outputDir: './dist/', expected: '..' }, + { projectDir: 'C:/My Documents/MyApp/', outputDir: 'dist\\', expected: '..' }, + // Output directory outside of project root + { projectDir: '/usr/home/my-app', outputDir: '../wwwroot/scripts', expected: '../../my-app' }, + { projectDir: 'C:\\My Documents\\MyApp', outputDir: '../wwwroot/scripts', expected: '../../MyApp' }, + // Relative project root paths, basic cases + { projectDir: './my-app', outputDir: './dist', expected: '..' }, + { projectDir: 'my-app', outputDir: 'dist', expected: '..' }, + { projectDir: '.\\MyApp', outputDir: '.\\dist', expected: '..' }, + { projectDir: 'MyApp\\', outputDir: 'dist\\', expected: '..' }, + // Relative project root paths, output directory outside of project root + { projectDir: './my-app', outputDir: '../wwwroot/scripts', expected: '../../my-app' }, + { projectDir: '.\\MyApp\\', outputDir: '..\\wwwroot\\scripts\\', expected: '../../MyApp' } + ]; + + testCases.forEach(({ projectDir, outputDir, expected }) => { + it(`returns "${expected}" for projectDir "${projectDir}" and outputDir "${outputDir}"`, () => { + const result = _calculateRelativeSourceMapsRoot(projectDir, outputDir); + expect(result).toBe(expected); + }); + }); +}); From b10cfe7dc68c7da167f85f618192638630e2536a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nenad=20Vi=C4=87enti=C4=87?= Date: Sun, 20 Jul 2025 12:55:26 +0200 Subject: [PATCH 2/2] fix #1212: part 2/2 fix logic in `calculateRelativeSourceMapsRoot` function. --- lib/build/bundle.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/build/bundle.js b/lib/build/bundle.js index 264deef8f..a08086a3c 100644 --- a/lib/build/bundle.js +++ b/lib/build/bundle.js @@ -538,10 +538,10 @@ function uniqueBy(collection, key) { * @returns {string} A POSIX-style relative path from `outputDir` to `projectRoot`. */ function calculateRelativeSourceMapsRoot(projectRootDir, outputDir){ - return path.posix.relative( - path.posix.join(projectRootDir, outputDir), - projectRootDir - ); + const absoluteProjectRootDir = path.resolve(projectRootDir.split('\\').join('/')); + const absoluteOutputDir = path.resolve(absoluteProjectRootDir, outputDir.split('\\').join('/')); + + return path.relative(absoluteOutputDir, absoluteProjectRootDir).split('\\').join('/'); } exports._calculateRelativeSourceMapsRoot = calculateRelativeSourceMapsRoot;