Skip to content

Commit

Permalink
fix: source maps (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Oct 2, 2020
1 parent 7d65853 commit 1be8169
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -62,7 +62,7 @@ export default async function stylusLoader(source) {
useSourceMap
? {
comment: false,
sourceRoot: this.rootContext,
sourceRoot: stylusOptions.dest,
basePath: this.rootContext,
}
: stylusOptions.sourcemap
Expand Down Expand Up @@ -154,7 +154,7 @@ export default async function stylusLoader(source) {
let map = styl.sourcemap;

if (map && useSourceMap) {
map = normalizeSourceMap(map, this.rootContext);
map = normalizeSourceMap(map, stylusOptions.dest);

try {
map.sourcesContent = await Promise.all(
Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/sourceMap-options.test.js.snap
@@ -1,5 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"sourceMap" options should generate nested source maps when value is "true": css 1`] = `
".other {
font-family: serif;
}
body {
font: 12px Helvetica, Arial, sans-serif;
}
a.button {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.some-class {
margin: 10px;
}
"
`;

exports[`"sourceMap" options should generate nested source maps when value is "true": errors 1`] = `Array []`;

exports[`"sourceMap" options should generate nested source maps when value is "true": source map 1`] = `
Object {
"mappings": "AAAA;EACC,aAAY,MAAZ;;ACID;EACE,MAAmB,kCAAnB;;AAEF;EAPE,uBAAsB,IAAtB;EACA,oBAAmB,IAAnB;EACA,eAAc,IAAd;;ACAF;EACE,QAAQ,KAAR",
"names": Array [],
"sourceRoot": "",
"sources": Array [
"test/fixtures/paths/in-paths.styl",
"test/fixtures/basic.styl",
"test/fixtures/source-map/index.styl",
],
"sourcesContent": Array [
".other
font-family serif",
"border-radius()
-webkit-border-radius arguments
-moz-border-radius arguments
border-radius arguments
body
font 12px Helvetica, Arial, sans-serif
a.button
border-radius 5px",
"@import 'in-paths'
@import '../basic'
.some-class
margin: 10px
",
],
"version": 3,
}
`;
exports[`"sourceMap" options should generate nested source maps when value is "true": warnings 1`] = `Array []`;
exports[`"sourceMap" options should generate source maps when the "devtool" value is "source-map": css 1`] = `
".other {
font-family: serif;
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/source-map/index.styl
@@ -0,0 +1,5 @@
@import 'in-paths'
@import '../basic'

.some-class
margin: 10px
36 changes: 36 additions & 0 deletions test/sourceMap-options.test.js
Expand Up @@ -211,4 +211,40 @@ describe('"sourceMap" options', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should generate nested source maps when value is "true"', async () => {
const testId = './source-map/index.styl';
const compiler = getCompiler(testId, {
sourceMap: true,
stylusOptions: {
paths: ['test/fixtures/paths'],
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const { css, map } = codeFromBundle;

map.sourceRoot = '';
map.sources = map.sources.map((source) => {
expect(path.isAbsolute(source)).toBe(true);
expect(source).toBe(path.normalize(source));
expect(fs.existsSync(path.resolve(map.sourceRoot, source))).toBe(true);

return path
.relative(path.resolve(__dirname, '..'), source)
.replace(/\\/g, '/');
});

const codeFromStylus = await getCodeFromStylus(testId, {
stylusOptions: {
paths: ['test/fixtures/paths'],
},
});

expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(css).toMatchSnapshot('css');
expect(map).toMatchSnapshot('source map');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit 1be8169

Please sign in to comment.