From e3bb83a40e1c2c1960422220b3d136843482c3c0 Mon Sep 17 00:00:00 2001 From: Eugene Datsky Date: Fri, 23 Nov 2018 22:32:21 +1000 Subject: [PATCH] fix: support deduplication of string module ids (optimization.namedModules) (#789) --- lib/css-base.js | 4 ++-- test/cssBaseTest.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/css-base.js b/lib/css-base.js index 59af87df..7b9a4b4b 100644 --- a/lib/css-base.js +++ b/lib/css-base.js @@ -25,7 +25,7 @@ module.exports = function(useSourceMap) { var alreadyImportedModules = {}; for(var i = 0; i < this.length; i++) { var id = this[i][0]; - if(typeof id === "number") + if(id != null) alreadyImportedModules[id] = true; } for(i = 0; i < modules.length; i++) { @@ -34,7 +34,7 @@ module.exports = function(useSourceMap) { // this implementation is not 100% perfect for weird media query combinations // when a module is imported multiple times with different media queries. // I hope this will never occur (Hey this way we have smaller bundles) - if(typeof item[0] !== "number" || !alreadyImportedModules[item[0]]) { + if(item[0] == null || !alreadyImportedModules[item[0]]) { if(mediaQuery && !item[2]) { item[2] = mediaQuery; } else if(mediaQuery) { diff --git a/test/cssBaseTest.js b/test/cssBaseTest.js index 01b2a2ee..fa43cd96 100644 --- a/test/cssBaseTest.js +++ b/test/cssBaseTest.js @@ -52,6 +52,21 @@ describe("css-base", function() { "@media print{body { d: 4; }}" + "@media screen{body { a: 1; }}"); }); + it("should import named modules", function() { + var m = base(); + var m1 = ["./module1", "body { a: 1; }", "screen"]; + var m2 = ["./module2", "body { b: 2; }", ""]; + var m3 = ["./module3", "body { c: 3; }", ""]; + var m4 = ["./module4", "body { d: 4; }", ""]; + m.i([m2, m3], ""); + m.i([m2], ""); + m.i([m2, m4], "print"); + m.push(m1); + m.toString().should.be.eql("body { b: 2; }" + + "body { c: 3; }" + + "@media print{body { d: 4; }}" + + "@media screen{body { a: 1; }}"); + }); it("should toString with source mapping", function() { var m = base(true); m.push([1, "body { a: 1; }", "", {