Skip to content

Commit

Permalink
fix: respect the map option and source maps (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 5, 2020
1 parent ba88040 commit 98441ff
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 20 deletions.
19 changes: 15 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -79,7 +79,7 @@
"postcss-short": "^5.0.0",
"prettier": "^2.0.5",
"sass": "^1.26.10",
"sass-loader": "^10.0.1",
"sass-loader": "^10.0.2",
"standard": "^14.3.4",
"standard-version": "^8.0.2",
"strip-ansi": "^6.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Expand Up @@ -107,6 +107,12 @@ export default async function loader(content, sourceMap) {
this.resourcePath
);
}
} else if (sourceMap && typeof processOptions.map !== 'undefined') {
if (typeof processOptions.map === 'boolean') {
processOptions.map = { inline: true };
}

processOptions.map.prev = sourceMap;
}

let result;
Expand Down
63 changes: 51 additions & 12 deletions test/options/__snapshots__/sourceMap.test.js.snap
@@ -1,15 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): css 1`] = `
exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader"): css 1`] = `
"a {
color: coral;
}
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLnNjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7RUFBSSxZQUFBO0FBRUoiLCJmaWxlIjoic3R5bGUuc2NzcyIsInNvdXJjZXNDb250ZW50IjpbImEgeyBjb2xvcjogY29yYWwgfVxuIl19 */"
`;

exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader"): errors 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader"): warnings 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): css 1`] = `
"a {
color: coral;
}
/*# sourceMappingURL=style.scss.map */"
`;

exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): errors 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): source map 1`] = `
Object {
"file": "style.scss",
"mappings": "AAAA;EAAI,YAAA;AAEJ",
"names": Array [],
"sourceRoot": "",
"sources": Array [
"../style.scss",
],
"sourcesContent": Array [
"a { color: coral }
",
],
"version": 3,
}
`;

exports[`"sourceMap" option should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader"): warnings 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): css 1`] = `
"a {
color: coral;
}
"
`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): errors 1`] = `Array []`;
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): errors 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): source map 1`] = `
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): source map 1`] = `
Object {
"mappings": "AAAA;EAAI,YAAA;AAEJ",
"names": Array [],
Expand All @@ -25,17 +64,17 @@ Object {
}
`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("less-loader"): warnings 1`] = `Array []`;
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("less-loader"): warnings 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): css 1`] = `
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): css 1`] = `
"a {
color: coral;
}"
`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): errors 1`] = `Array []`;
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): errors 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): source map 1`] = `
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): source map 1`] = `
Object {
"mappings": "AAAA;EAAI,YAAA;AAEJ",
"names": Array [],
Expand All @@ -51,9 +90,9 @@ Object {
}
`;

exports[`"sourceMap" option should generate source maps when previous loader return source maps ("sass-loader"): warnings 1`] = `Array []`;
exports[`"sourceMap" option should generate source maps when previous loader returns source maps ("sass-loader"): warnings 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: css 1`] = `
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: css 1`] = `
"a {
color: black;
}
Expand Down Expand Up @@ -100,9 +139,9 @@ a {
"
`;

exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: errors 1`] = `Array []`;
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: errors 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: source map 1`] = `
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: source map 1`] = `
Object {
"file": "style.css",
"mappings": "AAAA;EACE,YAAY;AACd;;AAEA;EACE,UAAU;AACZ;;AAEA;EACE,YAAY;AACd;;AAEA;EACE,WAAW;AACb;;AAEA;EACE,4BAA4B;EAC5B,mBAAmB;AACrB;;AAEA;EACE,4BAA4B;EAC5B,mBAAmB;AACrB;;AAEA;EACE;IACE,YAAY;;IAEZ;MACE,WAAW;IACb;;IAEA;MACE,YAAY;IACd;EACF;;EAEA;IACE,cAAc;EAChB;AACF",
Expand Down Expand Up @@ -161,7 +200,7 @@ a {
}
`;

exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value: warnings 1`] = `Array []`;
exports[`"sourceMap" option should generate source maps when value has "false" value, but the "postcssOptions.map" has values: warnings 1`] = `Array []`;

exports[`"sourceMap" option should generate source maps when value has "true" value and the "devtool" option has "false" value: css 1`] = `
"a {
Expand Down
132 changes: 129 additions & 3 deletions test/options/sourceMap.test.js
Expand Up @@ -108,7 +108,7 @@ describe('"sourceMap" option', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should generate source maps when value has "false" value, but the "postcssOptions.map" has the "true" value', async () => {
it('should generate source maps when value has "false" value, but the "postcssOptions.map" has values', async () => {
const compiler = getCompiler(
'./css/index.js',
{
Expand Down Expand Up @@ -147,7 +147,133 @@ describe('"sourceMap" option', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should generate source maps when previous loader return source maps ("sass-loader")', async () => {
it('should generate source maps the "postcssOptions.map" has the "true" values and previous loader returns source maps ("sass-loader")', async () => {
const compiler = getCompiler(
'./scss/index.js',
{},
{
devtool: false,
module: {
rules: [
{
test: /\.scss$/i,
use: [
{
loader: require.resolve('../helpers/testLoader'),
options: {},
},
{
loader: path.resolve(__dirname, '../../src'),
options: {
postcssOptions: {
map: true,
},
},
},
{
loader: 'sass-loader',
options: {
// eslint-disable-next-line global-require
implementation: require('sass'),
sassOptions: {
sourceMap: true,
outFile: path.join(
__dirname,
'../fixtures/scss/style.css.map'
),
sourceMapContents: true,
omitSourceMapUrl: true,
sourceMapEmbed: false,
},
},
},
],
},
],
},
}
);
const stats = await compile(compiler);
const { css, sourceMap } = getCodeFromBundle('style.scss', stats);

expect(css).toMatchSnapshot('css');
expect(sourceMap).toBeUndefined();
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should generate source maps the "postcssOptions.map" has values and previous loader returns source maps ("sass-loader")', async () => {
const compiler = getCompiler(
'./scss/index.js',
{},
{
devtool: false,
module: {
rules: [
{
test: /\.scss$/i,
use: [
{
loader: require.resolve('../helpers/testLoader'),
options: {},
},
{
loader: path.resolve(__dirname, '../../src'),
options: {
postcssOptions: {
map: {
inline: false,
sourcesContent: true,
annotation: true,
},
},
},
},
{
loader: 'sass-loader',
options: {
// eslint-disable-next-line global-require
implementation: require('sass'),
sassOptions: {
sourceMap: true,
outFile: path.join(
__dirname,
'../fixtures/scss/style.css.map'
),
sourceMapContents: true,
omitSourceMapUrl: true,
sourceMapEmbed: false,
},
},
},
],
},
],
},
}
);
const stats = await compile(compiler);
const { css, sourceMap } = getCodeFromBundle('style.scss', stats);

sourceMap.sourceRoot = '';
sourceMap.sources = sourceMap.sources.map((source) => {
expect(path.isAbsolute(source)).toBe(false);
expect(
fs.existsSync(path.resolve(__dirname, '../fixtures/scss', source))
).toBe(true);

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

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

it('should generate source maps when previous loader returns source maps ("sass-loader")', async () => {
const compiler = getCompiler(
'./scss/index.js',
{},
Expand Down Expand Up @@ -201,7 +327,7 @@ describe('"sourceMap" option', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should generate source maps when previous loader return source maps ("less-loader")', async () => {
it('should generate source maps when previous loader returns source maps ("less-loader")', async () => {
const compiler = getCompiler(
'./less/index.js',
{
Expand Down

0 comments on commit 98441ff

Please sign in to comment.