Skip to content

Commit 78dbf16

Browse files
committed
Merge pull request #68 from hgl/hash
Add failing test for same file contents
2 parents e9db26d + 0cfa806 commit 78dbf16

File tree

9 files changed

+28
-22
lines changed

9 files changed

+28
-22
lines changed

index.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var clone = require("clone")
99
var resolve = require("resolve")
1010
var postcss = require("postcss")
1111
var helpers = require("postcss-message-helpers")
12-
var hash = require("string-hash")
1312
var glob = require("glob")
1413

1514
var Promize = global.Promise || require("es6-promise").Promise
@@ -353,29 +352,27 @@ function readImportedContent(
353352
return resolvedPromise
354353
}
355354

356-
// skip files wich only contain @import rules
357-
var newFileContent = fileContent.replace(/@import (.*);/, "")
358-
if (newFileContent.trim() !== "") {
359-
var fileContentHash = hash(fileContent)
360-
361-
// skip files already imported at the same scope and same hash
362-
if (
363-
state.hashFiles[fileContentHash] &&
364-
state.hashFiles[fileContentHash][media]
365-
) {
366-
detach(atRule)
367-
return resolvedPromise
368-
}
355+
// skip previous imported files not containing @import rules
356+
if (
357+
state.hashFiles[fileContent] &&
358+
state.hashFiles[fileContent][media]
359+
) {
360+
detach(atRule)
361+
return resolvedPromise
362+
}
369363

364+
var newStyles = postcss.parse(fileContent, options)
365+
var hasImport = newStyles.some(function(child) {
366+
return child.type === "atrule" && child.name.toLowerCase() === "import"
367+
})
368+
if (!hasImport) {
370369
// save hash files to skip them next time
371-
if (!state.hashFiles[fileContentHash]) {
372-
state.hashFiles[fileContentHash] = {}
370+
if (!state.hashFiles[fileContent]) {
371+
state.hashFiles[fileContent] = {}
373372
}
374-
state.hashFiles[fileContentHash][media] = true
373+
state.hashFiles[fileContent][media] = true
375374
}
376375

377-
var newStyles = postcss.parse(fileContent, options)
378-
379376
// recursion: import @import from imported file
380377
var parsedResult = parseStyles(
381378
result,

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
"object-assign": "^3.0.0",
2929
"postcss": "^4.1.4",
3030
"postcss-message-helpers": "^2.0.0",
31-
"resolve": "^1.0.0",
32-
"string-hash": "^1.1.0"
31+
"resolve": "^1.0.0"
3332
},
3433
"devDependencies": {
3534
"css-whitespace": "^1.1.0",

test/fixtures/imports/bar/baz.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "baz.css"

test/fixtures/imports/foo/baz.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "baz.css"

test/fixtures/same.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import "foo/index.css";
2+
@import "bar/index.css";

test/fixtures/same.expected.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo {}
2+
bar {}

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ function trimResultCss(result) {
3232
}
3333

3434
test("@import", function(t) {
35-
t.plan(15)
35+
t.plan(16)
3636

3737
compareFixtures(t, "simple", "should import stylsheets")
3838

3939
compareFixtures(t, "no-duplicate", "should not import a stylsheet twice")
4040

41+
compareFixtures(t, "same", "should import stylsheets with same content")
42+
4143
compareFixtures(t, "ignore", "should ignore & adjust external import")
4244

4345
compareFixtures(t, "glob", "should handle a glob pattern", {

0 commit comments

Comments
 (0)