Skip to content

Commit 6850a5b

Browse files
nenadvicenticNenad Vićentić
andauthored
fix: mapSourceRoot not calculated correctly on Windows, when outputDir is outside of project's root folder (#1213)
closes #1212 --------- Co-authored-by: Nenad Vićentić <vicentic@immounited.com>
1 parent 254508c commit 6850a5b

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

lib/build/bundle.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,7 @@ exports.Bundle = class {
364364
}
365365

366366
let mapFileName = bundleFileName + '.map';
367-
let mapSourceRoot = path.posix.relative(
368-
path.posix.join(process.cwd(), platform.output),
369-
process.cwd()
370-
);
367+
let mapSourceRoot = calculateRelativeSourceMapsRoot(process.cwd(), platform.output);
371368

372369
logger.info(`Writing ${bundleFileName}...`);
373370

@@ -531,3 +528,20 @@ function uniqueBy(collection, key) {
531528
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
532529
});
533530
}
531+
532+
/**
533+
* Returns a POSIX-style relative path from `outputDir` back to `projectRoot`.
534+
* Works on Windows, macOS and Linux.
535+
*
536+
* @param {string} projectRootDir - The root directory of the project.
537+
* @param {string} outputDir - The output directory where the files are generated.
538+
* @returns {string} A POSIX-style relative path from `outputDir` to `projectRoot`.
539+
*/
540+
function calculateRelativeSourceMapsRoot(projectRootDir, outputDir){
541+
const absoluteProjectRootDir = path.resolve(projectRootDir.split('\\').join('/'));
542+
const absoluteOutputDir = path.resolve(absoluteProjectRootDir, outputDir.split('\\').join('/'));
543+
544+
return path.relative(absoluteOutputDir, absoluteProjectRootDir).split('\\').join('/');
545+
}
546+
547+
exports._calculateRelativeSourceMapsRoot = calculateRelativeSourceMapsRoot;

spec/lib/build/bundle.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const BundlerMock = require('../../mocks/bundler');
22
const Bundle = require('../../../lib/build/bundle').Bundle;
3+
const _calculateRelativeSourceMapsRoot = require('../../../lib/build/bundle')._calculateRelativeSourceMapsRoot;
34
const CLIOptionsMock = require('../../mocks/cli-options');
45
const DependencyDescription = require('../../../lib/build/dependency-description').DependencyDescription;
56
const SourceInclusion = require('../../../lib/build/source-inclusion').SourceInclusion;
@@ -459,3 +460,45 @@ describe('the Bundle module', () => {
459460
});
460461
});
461462
});
463+
464+
describe('function _calculateRelativeSourceMapsRoot', () => {
465+
const testCases = [
466+
// Basic UNIX cases
467+
{ projectDir: '/usr/home/my-app', outputDir: './dist', expected: '..' },
468+
{ projectDir: '/usr/home/my-app/', outputDir: './dist/', expected: '..' },
469+
{ projectDir: '/usr/home/my-app', outputDir: 'dist', expected: '..' },
470+
{ projectDir: '/usr/home/my-app/', outputDir: 'dist/', expected: '..' },
471+
// Basic Windows cases
472+
{ projectDir: 'C:/My Documents/MyApp', outputDir: './dist', expected: '..' },
473+
{ projectDir: 'C:/My Documents/MyApp/', outputDir: './dist/', expected: '..' },
474+
{ projectDir: 'C:/My Documents/MyApp', outputDir: 'dist', expected: '..' },
475+
{ projectDir: 'C:/My Documents/MyApp/', outputDir: 'dist/', expected: '..' },
476+
// Basic Windows cases with backslashes
477+
{ projectDir: 'C:\\My Documents\\MyApp', outputDir: '.\\dist', expected: '..' },
478+
{ projectDir: 'C:\\My Documents\\MyApp\\', outputDir: '.\\dist\\', expected: '..' },
479+
{ projectDir: 'C:\\My Documents\\MyApp', outputDir: 'dist', expected: '..' },
480+
{ projectDir: 'C:\\My Documents\\MyApp\\', outputDir: 'dist\\', expected: '..' },
481+
// Windows mixed slashes
482+
{ projectDir: 'C:\\My Documents\\MyApp', outputDir: './dist', expected: '..' },
483+
{ projectDir: 'C:\\My Documents\\MyApp\\', outputDir: './dist/', expected: '..' },
484+
{ projectDir: 'C:/My Documents/MyApp/', outputDir: 'dist\\', expected: '..' },
485+
// Output directory outside of project root
486+
{ projectDir: '/usr/home/my-app', outputDir: '../wwwroot/scripts', expected: '../../my-app' },
487+
{ projectDir: 'C:\\My Documents\\MyApp', outputDir: '../wwwroot/scripts', expected: '../../MyApp' },
488+
// Relative project root paths, basic cases
489+
{ projectDir: './my-app', outputDir: './dist', expected: '..' },
490+
{ projectDir: 'my-app', outputDir: 'dist', expected: '..' },
491+
{ projectDir: '.\\MyApp', outputDir: '.\\dist', expected: '..' },
492+
{ projectDir: 'MyApp\\', outputDir: 'dist\\', expected: '..' },
493+
// Relative project root paths, output directory outside of project root
494+
{ projectDir: './my-app', outputDir: '../wwwroot/scripts', expected: '../../my-app' },
495+
{ projectDir: '.\\MyApp\\', outputDir: '..\\wwwroot\\scripts\\', expected: '../../MyApp' }
496+
];
497+
498+
testCases.forEach(({ projectDir, outputDir, expected }) => {
499+
it(`returns "${expected}" for projectDir "${projectDir}" and outputDir "${outputDir}"`, () => {
500+
const result = _calculateRelativeSourceMapsRoot(projectDir, outputDir);
501+
expect(result).toBe(expected);
502+
});
503+
});
504+
});

0 commit comments

Comments
 (0)