diff --git a/package-lock.json b/package-lock.json index b65195f0..d4aded54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14466,18 +14466,29 @@ } }, "sass-loader": { - "version": "10.0.1", - "resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-10.0.1.tgz", - "integrity": "sha512-b2PSldKVTS3JcFPHSrEXh3BeAfR7XknGiGCAO5aHruR3Pf3kqLP3Gb2ypXLglRrAzgZkloNxLZ7GXEGDX0hBUQ==", + "version": "10.0.2", + "resolved": "https://registry.npmjs.org/sass-loader/-/sass-loader-10.0.2.tgz", + "integrity": "sha512-wV6NDUVB8/iEYMalV/+139+vl2LaRFlZGEd5/xmdcdzQcgmis+npyco6NsDTVOlNA3y2NV9Gcz+vHyFMIT+ffg==", "dev": true, "requires": { "klona": "^2.0.3", "loader-utils": "^2.0.0", "neo-async": "^2.6.2", - "schema-utils": "^2.7.0", + "schema-utils": "^2.7.1", "semver": "^7.3.2" }, "dependencies": { + "schema-utils": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-2.7.1.tgz", + "integrity": "sha512-SHiNtMOUGWBQJwzISiVYKu82GiV4QYGePp3odlY1tuKO7gPtphAT5R/py0fA6xtbgLL/RvtJZnU9b8s0F1q0Xg==", + "dev": true, + "requires": { + "@types/json-schema": "^7.0.5", + "ajv": "^6.12.4", + "ajv-keywords": "^3.5.2" + } + }, "semver": { "version": "7.3.2", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz", diff --git a/package.json b/package.json index f1f52ea5..bcc21c77 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 5db49521..162095f0 100644 --- a/src/index.js +++ b/src/index.js @@ -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; diff --git a/test/options/__snapshots__/sourceMap.test.js.snap b/test/options/__snapshots__/sourceMap.test.js.snap index 73563aee..0d100268 100644 --- a/test/options/__snapshots__/sourceMap.test.js.snap +++ b/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 [], @@ -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 [], @@ -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; } @@ -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", @@ -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 { diff --git a/test/options/sourceMap.test.js b/test/options/sourceMap.test.js index 231e6971..8b5c3030 100644 --- a/test/options/sourceMap.test.js +++ b/test/options/sourceMap.test.js @@ -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', { @@ -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', {}, @@ -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', {