Skip to content

Commit f344a2f

Browse files
committed
fix: convert to same sourcemap format as gulp-sass v5
Default to relative paths in the sources array.
1 parent 02b3bb3 commit f344a2f

File tree

2 files changed

+45
-136
lines changed

2 files changed

+45
-136
lines changed

index.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,37 @@ const filePush = (file, compileResult, callback) => {
3030

3131
// Build Source Maps!
3232
if (compileResult.sourceMap) {
33+
const proto = /^file:\/\/?/;
3334
const sassMap = compileResult.sourceMap;
35+
const base = path.resolve(file.cwd, file.base);
36+
3437
if (!sassMap.file) {
35-
sassMap.file = file.path;
38+
// Convert from absolute path to relative as in gulp-sass 5.0.0
39+
sassMap.file = file.history[0]
40+
.replace(base + path.sep, '')
41+
.replace(proto, '');
3642
}
3743

38-
// Grab the stdout and transform it into stdin
39-
const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
44+
// Transform to relative file paths as in gulp-sass 5.0.0
45+
sassMap.sources = sassMap.sources.map((src) => {
46+
// The current file and its content is included
47+
// as data:<encoded file contents> in the new Sass JS API.
48+
// Map it to the original file name (first history entry).
49+
if (src.startsWith('data:')) {
50+
return file.history[0]
51+
.replace(base + path.sep, '')
52+
.replace(proto, '');
53+
}
54+
return src
55+
.replace(proto, '')
56+
.replace(base + path.sep, '');
57+
});
58+
4059
// Grab the base filename that's being worked on
4160
const sassFileSrc = file.relative;
42-
// Grab the path portion of the file that's being worked on
43-
const sassFileSrcPath = path.dirname(sassFileSrc);
44-
45-
if (sassFileSrcPath) {
46-
const sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
47-
// Prepend the path to all files in the sources array except the file that's being worked on
48-
sassMap.sources = sassMap.sources.map((source, index) => (
49-
index === sourceFileIndex
50-
? source
51-
: path.join(sassFileSrcPath, source)
52-
));
53-
}
54-
55-
// Remove 'stdin' from souces and replace with filenames!
56-
sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src);
57-
5861
// Replace the map file with the original filename (but new extension)
5962
sassMap.file = replaceExtension(sassFileSrc, '.css');
63+
6064
// Apply the map
6165
applySourceMap(file, sassMap);
6266
}

test/main.js

Lines changed: 22 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -256,31 +256,16 @@ describe('gulp-sass -- async compile', () => {
256256
+ '}';
257257

258258
// Expected sources are relative to file.base
259-
const legacyExpectedSources = [
260-
'inheritance.scss',
261-
'includes/_cats.scss',
262-
'includes/_dogs.sass',
263-
];
264-
265-
// Going forward the source map typically uses absolute file: URLs,
266-
// although this can be controlled by custom importers
267259
const expectedSources = [
268-
'data:',
260+
'inheritance.scss',
269261
'includes/_cats.scss',
270262
'includes/_dogs.sass',
271263
];
272264

273265
const stream = sass({ silenceDeprecations: ['import'] });
274266
stream.on('data', (cssFile) => {
275267
assert.ok(cssFile.sourceMap);
276-
if (LEGACY_API) {
277-
assert.deepEqual(cssFile.sourceMap.sources.sort(), legacyExpectedSources.sort());
278-
} else {
279-
// look for partial matches since each test runner can have a different absolute path
280-
assert.ok(cssFile.sourceMap.sources.every(
281-
(source) => expectedSources.find((partial) => source.includes(partial)),
282-
));
283-
}
268+
assert.deepEqual(cssFile.sourceMap.sources.sort(), expectedSources.sort());
284269
done();
285270
});
286271
stream.write(sassFile);
@@ -460,16 +445,8 @@ describe('gulp-sass -- sync compile', () => {
460445
const sassFile = createVinyl('inheritance.scss');
461446

462447
// Expected sources are relative to file.base
463-
const legacyExpectedSources = [
464-
'inheritance.scss',
465-
'includes/_cats.scss',
466-
'includes/_dogs.sass',
467-
];
468-
469-
// Going forward the source map typically uses absolute file: URLs,
470-
// although this can be controlled by custom importers
471448
const expectedSources = [
472-
'data:',
449+
'inheritance.scss',
473450
'includes/_cats.scss',
474451
'includes/_dogs.sass',
475452
];
@@ -486,77 +463,41 @@ describe('gulp-sass -- sync compile', () => {
486463
const stream = sass.sync({ silenceDeprecations: ['import'] });
487464
stream.on('data', (cssFile) => {
488465
assert.ok(cssFile.sourceMap);
489-
if (LEGACY_API) {
490-
assert.deepEqual(cssFile.sourceMap.sources.sort(), legacyExpectedSources.sort());
491-
} else {
492-
// look for partial matches since each test runner can have a different absolute path
493-
assert.ok(cssFile.sourceMap.sources.every(
494-
(source) => expectedSources.find((partial) => source.includes(partial)),
495-
));
496-
}
466+
assert.deepEqual(cssFile.sourceMap.sources.sort(), expectedSources.sort());
497467
done();
498468
});
499469
stream.write(sassFile);
500470
});
501471

502472
it('should work with gulp-sourcemaps and autoprefixer', (done) => {
503-
const legacyExpectedSourcesBefore = [
504-
'inheritance.scss',
505-
'includes/_cats.scss',
506-
'includes/_dogs.sass',
507-
];
508-
509-
const legacyExpectedSourcesAfter = [
510-
'includes/_cats.scss',
511-
'includes/_dogs.sass',
512-
'inheritance.scss',
513-
];
514-
515473
const expectedSourcesBefore = [
516-
'data:',
474+
'inheritance.scss',
517475
'includes/_cats.scss',
518476
'includes/_dogs.sass',
519477
];
520478

521479
const expectedSourcesAfter = [
522480
'includes/_cats.scss',
523481
'includes/_dogs.sass',
524-
'data:',
482+
'inheritance.css', // added by postcss
483+
'inheritance.scss',
525484
];
526485

527-
if (MODERN_COMPILER) {
528-
const result = 'inheritance.css';
529-
legacyExpectedSourcesAfter.push(result);
530-
expectedSourcesAfter.push(result);
531-
}
532-
533486
gulp.src(path.join(__dirname, 'scss', 'inheritance.scss'))
534487
.pipe(sourcemaps.init())
535488
.pipe(sass.sync({ silenceDeprecations: ['import'] }))
536489
.pipe(tap((file) => {
537490
assert.ok(file.sourceMap);
538-
if (LEGACY_API) {
539-
assert.deepEqual(file.sourceMap.sources.sort(), legacyExpectedSourcesBefore.sort());
540-
} else {
541-
// look for partial matches since each test runner can have a different absolute path
542-
assert.ok(file.sourceMap.sources.every(
543-
(source) => expectedSourcesBefore.find((partial) => source.includes(partial)),
544-
));
545-
}
491+
assert.equal(file.sourceMap.file, 'inheritance.css');
492+
assert.deepEqual(file.sourceMap.sources.sort(), expectedSourcesBefore.sort());
546493
}))
547494
.pipe(postcss([autoprefixer()]))
548495
.pipe(sourcemaps.write())
549496
.pipe(gulp.dest(path.join(__dirname, 'results')))
550497
.pipe(tap((file) => {
551498
assert.ok(file.sourceMap);
552-
if (LEGACY_API) {
553-
assert.deepEqual(file.sourceMap.sources.sort(), legacyExpectedSourcesAfter.sort());
554-
} else {
555-
// look for partial matches since each test runner can have a different absolute path
556-
assert.ok(file.sourceMap.sources.every(
557-
(source) => expectedSourcesAfter.find((partial) => source.includes(partial)),
558-
));
559-
}
499+
assert.equal(file.sourceMap.file, 'inheritance.css');
500+
assert.deepEqual(file.sourceMap.sources.sort(), expectedSourcesAfter.sort());
560501
}));
561502
done();
562503
});
@@ -576,77 +517,41 @@ describe('gulp-sass -- sync compile', () => {
576517
.pipe(sass.sync({ silenceDeprecations: ['import'] }))
577518
.pipe(tap((file) => {
578519
assert.ok(file.sourceMap);
579-
if (LEGACY_API) {
580-
const actual = normaliseEOL(file.sourceMap.sourcesContent[0]);
581-
const expected = normaliseEOL(filesContent[path.normalize(file.sourceMap.sources[0])]);
582-
assert.deepEqual(actual, expected);
583-
} else {
584-
const sourceMap = file.sourceMap.sources[0];
585-
const source = decodeURI(sourceMap.split('data:;charset=utf-8,')[1]);
586-
const actual = normaliseEOL(source);
587-
const expected = normaliseEOL(filesContent[path.normalize(file.sourceMap.file.replace('.css', '.scss'))]);
588-
assert.deepEqual(actual, expected);
589-
}
520+
assert.ok(file.sourceMap.sourcesContent);
521+
const actual = normaliseEOL(file.sourceMap.sourcesContent[0]);
522+
const expected = normaliseEOL(filesContent[path.normalize(file.sourceMap.sources[0])]);
523+
assert.deepEqual(actual, expected);
590524
}));
591525
done();
592526
});
593527

594528
it('should work with gulp-sourcemaps and autoprefixer with different file.base', (done) => {
595-
const legacyExpectedSourcesBefore = [
596-
'scss/inheritance.scss',
597-
'scss/includes/_cats.scss',
598-
'scss/includes/_dogs.sass',
599-
];
600-
601-
const legacyExpectedSourcesAfter = [
602-
'scss/includes/_cats.scss',
603-
'scss/includes/_dogs.sass',
604-
'scss/inheritance.scss',
605-
];
606-
607529
const expectedSourcesBefore = [
608-
'scss/data:',
530+
'scss/inheritance.scss',
609531
'scss/includes/_cats.scss',
610532
'scss/includes/_dogs.sass',
611533
];
612534

613535
const expectedSourcesAfter = [
614536
'scss/includes/_cats.scss',
615537
'scss/includes/_dogs.sass',
616-
'scss/data:',
538+
'scss/inheritance.css', // added by postcss
539+
'scss/inheritance.scss',
617540
];
618541

619-
if (MODERN_COMPILER) {
620-
const result = 'scss/inheritance.css';
621-
legacyExpectedSourcesAfter.push(result);
622-
expectedSourcesAfter.push(result);
623-
}
624-
625542
gulp.src(path.join(__dirname, 'scss', 'inheritance.scss'), { base: 'test' })
626543
.pipe(sourcemaps.init())
627544
.pipe(sass.sync({ silenceDeprecations: ['import'] }))
628545
.pipe(tap((file) => {
629546
assert.ok(file.sourceMap);
630-
if (LEGACY_API) {
631-
assert.deepEqual(file.sourceMap.sources.sort(), legacyExpectedSourcesBefore.sort());
632-
} else {
633-
// look for partial matches since each test runner can have a different absolute path
634-
assert.ok(file.sourceMap.sources.every(
635-
(source) => expectedSourcesBefore.find((partial) => source.includes(partial)),
636-
));
637-
}
547+
assert.equal(file.sourceMap.file, 'scss/inheritance.css');
548+
assert.deepEqual(file.sourceMap.sources.sort(), expectedSourcesBefore.sort());
638549
}))
639550
.pipe(postcss([autoprefixer()]))
640551
.pipe(tap((file) => {
641552
assert.ok(file.sourceMap);
642-
if (LEGACY_API) {
643-
assert.deepEqual(file.sourceMap.sources.sort(), legacyExpectedSourcesAfter.sort());
644-
} else {
645-
// look for partial matches since each test runner can have a different absolute path
646-
assert.ok(file.sourceMap.sources.every(
647-
(source) => expectedSourcesAfter.find((partial) => source.includes(partial)),
648-
));
649-
}
553+
assert.equal(file.sourceMap.file, 'scss/inheritance.css');
554+
assert.deepEqual(file.sourceMap.sources.sort(), expectedSourcesAfter.sort());
650555
}));
651556
done();
652557
});

0 commit comments

Comments
 (0)