From b60e62a655719cc1779fae7d577af6ad6cf42135 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Fri, 3 Jan 2020 18:18:20 +0300 Subject: [PATCH 1/3] fix: don't output invalid es5 code when locals do not exists (#1035) --- src/utils.js | 6 +-- test/__snapshots__/loader.test.js.snap | 37 +++++++++++++++++++ .../modules/issue-1033/issue-1033.css | 0 .../fixtures/modules/issue-1033/issue-1033.js | 5 +++ test/loader.test.js | 33 +++++++++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/modules/issue-1033/issue-1033.css create mode 100644 test/fixtures/modules/issue-1033/issue-1033.js diff --git a/src/utils.js b/src/utils.js index c5b00d10..bdbf7910 100644 --- a/src/utils.js +++ b/src/utils.js @@ -480,9 +480,9 @@ function getExportCode( if (exportType === 'locals') { exportItems.push( - `${ - esModule ? 'export default' : 'module.exports =' - } {\n${exportLocalsCode}\n};` + `${esModule ? 'export default' : 'module.exports ='} ${ + exportLocalsCode ? `{\n${exportLocalsCode}\n}` : '{}' + };` ); } else { if (exportLocalsCode) { diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index ef5ed1b2..0f296761 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -1,5 +1,42 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`loader issue #1033 (2): errors 1`] = `Array []`; + +exports[`loader issue #1033 (2): module 1`] = ` +"// Imports +var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\"); +exports = ___CSS_LOADER_API_IMPORT___(false); +// Module +exports.push([module.id, \\"\\", \\"\\"]); +// Exports +module.exports = exports; +" +`; + +exports[`loader issue #1033 (2): result 1`] = ` +Array [ + Array [ + "./modules/issue-1033/issue-1033.css", + "", + "", + ], +] +`; + +exports[`loader issue #1033 (2): warnings 1`] = `Array []`; + +exports[`loader issue #1033: errors 1`] = `Array []`; + +exports[`loader issue #1033: module 1`] = ` +"// Exports +module.exports = {}; +" +`; + +exports[`loader issue #1033: result 1`] = `Object {}`; + +exports[`loader issue #1033: warnings 1`] = `Array []`; + exports[`loader should reuse \`ast\` from "postcss-loader": errors 1`] = `Array []`; exports[`loader should reuse \`ast\` from "postcss-loader": module 1`] = ` diff --git a/test/fixtures/modules/issue-1033/issue-1033.css b/test/fixtures/modules/issue-1033/issue-1033.css new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/modules/issue-1033/issue-1033.js b/test/fixtures/modules/issue-1033/issue-1033.js new file mode 100644 index 00000000..fcb67b1f --- /dev/null +++ b/test/fixtures/modules/issue-1033/issue-1033.js @@ -0,0 +1,5 @@ +import css from './issue-1033.css'; + +__export__ = css; + +export default css; diff --git a/test/loader.test.js b/test/loader.test.js index 68741413..8763bd34 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -308,4 +308,37 @@ describe('loader', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); expect(getErrors(stats)).toMatchSnapshot('errors'); }); + + it('issue #1033', async () => { + const compiler = getCompiler('./modules/issue-1033/issue-1033.js', { + modules: { mode: 'local', localIdentName: '_[local]' }, + onlyLocals: true, + }); + const stats = await compile(compiler); + + expect( + getModuleSource('./modules/issue-1033/issue-1033.css', stats) + ).toMatchSnapshot('module'); + expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot( + 'result' + ); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); + + it('issue #1033 (2)', async () => { + const compiler = getCompiler('./modules/issue-1033/issue-1033.js', { + modules: { mode: 'local', localIdentName: '_[local]' }, + }); + const stats = await compile(compiler); + + expect( + getModuleSource('./modules/issue-1033/issue-1033.css', stats) + ).toMatchSnapshot('module'); + expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot( + 'result' + ); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); }); From ded2a797271f2adf864bf92300621c024974bc79 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Fri, 3 Jan 2020 18:26:39 +0300 Subject: [PATCH 2/3] fix: do not output `undefined` when sourceRoot is unavailable (#1036) --- src/runtime/api.js | 2 +- test/runtime/__snapshots__/api.test.js.snap | 8 +++++++- test/runtime/api.test.js | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/runtime/api.js b/src/runtime/api.js index b2acad8c..bf8a1cbc 100644 --- a/src/runtime/api.js +++ b/src/runtime/api.js @@ -58,7 +58,7 @@ function cssWithMappingToString(item, useSourceMap) { if (useSourceMap && typeof btoa === 'function') { const sourceMapping = toComment(cssMapping); const sourceURLs = cssMapping.sources.map( - (source) => `/*# sourceURL=${cssMapping.sourceRoot}${source} */` + (source) => `/*# sourceURL=${cssMapping.sourceRoot || ''}${source} */` ); return [content] diff --git a/test/runtime/__snapshots__/api.test.js.snap b/test/runtime/__snapshots__/api.test.js.snap index 59695e48..23ba2486 100644 --- a/test/runtime/__snapshots__/api.test.js.snap +++ b/test/runtime/__snapshots__/api.test.js.snap @@ -8,6 +8,12 @@ exports[`api should toString a single module 1`] = `"body { a: 1; }"`; exports[`api should toString multiple modules 1`] = `"body { b: 2; }body { a: 1; }"`; +exports[`api should toString with a source map without "sourceRoot" 1`] = ` +"body { a: 1; } +/*# sourceURL=./path/to/test.scss */ +/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsifQ== */" +`; + exports[`api should toString with media query 1`] = `"body { b: 2; }body { c: 3; }body { b: 2; }@media print {body { b: 2; }}@media print {body { d: 4; }}@media screen {body { a: 1; }}"`; exports[`api should toString with source mapping 1`] = ` @@ -16,4 +22,4 @@ exports[`api should toString with source mapping 1`] = ` /*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsiLCJzb3VyY2VSb290Ijoid2VicGFjazovLyJ9 */" `; -exports[`api should toString without source mapping if btoa not avalibale 1`] = `"body { a: 1; }"`; +exports[`api should toString without source mapping if btoa not available 1`] = `"body { a: 1; }"`; diff --git a/test/runtime/api.test.js b/test/runtime/api.test.js index 508139a5..8ff7a5d5 100644 --- a/test/runtime/api.test.js +++ b/test/runtime/api.test.js @@ -101,7 +101,7 @@ describe('api', () => { expect(m.toString()).toMatchSnapshot(); }); - it('should toString without source mapping if btoa not avalibale', () => { + it('should toString without source mapping if btoa not available', () => { global.btoa = null; const m = api(true); @@ -120,4 +120,21 @@ describe('api', () => { expect(m.toString()).toMatchSnapshot(); }); + + it.only('should toString with a source map without "sourceRoot"', () => { + const m = api(true); + + m.push([ + 1, + 'body { a: 1; }', + '', + { + file: 'test.scss', + sources: ['./path/to/test.scss'], + mappings: 'AAAA;', + }, + ]); + + expect(m.toString()).toMatchSnapshot(); + }); }); From 7c9f47bee36579db366d69dae3af3c0bfbef82ce Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 3 Jan 2020 18:37:09 +0300 Subject: [PATCH 3/3] chore(release): 3.4.1 --- CHANGELOG.md | 8 ++++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c3b7b73..a9420b6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [3.4.1](https://github.com/webpack-contrib/css-loader/compare/v3.4.0...v3.4.1) (2020-01-03) + + +### Bug Fixes + +* do not output `undefined` when sourceRoot is unavailable ([#1036](https://github.com/webpack-contrib/css-loader/issues/1036)) ([ded2a79](https://github.com/webpack-contrib/css-loader/commit/ded2a797271f2adf864bf92300621c024974bc79)) +* don't output invalid es5 code when locals do not exists ([#1035](https://github.com/webpack-contrib/css-loader/issues/1035)) ([b60e62a](https://github.com/webpack-contrib/css-loader/commit/b60e62a655719cc1779fae7d577af6ad6cf42135)) + ## [3.4.0](https://github.com/webpack-contrib/css-loader/compare/v3.3.1...v3.4.0) (2019-12-17) diff --git a/package-lock.json b/package-lock.json index ffd83df0..befea976 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "css-loader", - "version": "3.4.0", + "version": "3.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b92fbf1c..6cb1a318 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "css-loader", - "version": "3.4.0", + "version": "3.4.1", "description": "css loader module for webpack", "license": "MIT", "repository": "webpack-contrib/css-loader",