diff --git a/README.md b/README.md index 9c96cf2..92f91c1 100644 --- a/README.md +++ b/README.md @@ -180,13 +180,18 @@ promised content. #### `skipDuplicates` -Type: `Boolean` +Type: `Boolean` or `"content"` Default: `true` -By default, similar files (based on the same content) are being skipped. -It's to optimize output and skip similar files like `normalize.css` for example. -If this behavior is not what you want, just set this option to `false` to -disable it. +By default, repeated imports of the same file are being skipped. + +Set this option to `"content"` to skip similar files based on their content +instead, so that copies of the same stylesheet (`normalize.css` for example) +imported from different paths are also skipped. Files can contain at-rules +whose paths resolve relative to the file's own location, which is why this +is not the default. + +If you don't want any duplicates to be skipped, set this option to `false`. #### `addModulesDirectories` diff --git a/lib/parse-styles.js b/lib/parse-styles.js index 766de9c..d1896dc 100644 --- a/lib/parse-styles.js +++ b/lib/parse-styles.js @@ -179,7 +179,7 @@ async function loadImportContent( // skip previous imported files not containing @import rules if ( - options.skipDuplicates && + options.skipDuplicates === "content" && state.hashFiles[content]?.[stmtDuplicateCheckKey] ) { return @@ -196,7 +196,7 @@ async function loadImportContent( const styles = importedResult.root result.messages = result.messages.concat(importedResult.messages) - if (options.skipDuplicates) { + if (options.skipDuplicates === "content") { const hasImport = styles.some(child => { return child.type === "atrule" && child.name === "import" }) diff --git a/test/fixtures/content-dedup.css b/test/fixtures/content-dedup.css new file mode 100644 index 0000000..ef02919 --- /dev/null +++ b/test/fixtures/content-dedup.css @@ -0,0 +1,3 @@ +@import "same-file-dedup/a/shared.css"; +@import "same-file-dedup/b/shared.css"; +@import "same-file-dedup/a/shared.css"; diff --git a/test/fixtures/content-dedup.expected.css b/test/fixtures/content-dedup.expected.css new file mode 100644 index 0000000..6abac48 --- /dev/null +++ b/test/fixtures/content-dedup.expected.css @@ -0,0 +1 @@ +shared {} diff --git a/test/fixtures/imports/same-file-dedup/a/shared.css b/test/fixtures/imports/same-file-dedup/a/shared.css new file mode 100644 index 0000000..6abac48 --- /dev/null +++ b/test/fixtures/imports/same-file-dedup/a/shared.css @@ -0,0 +1 @@ +shared {} diff --git a/test/fixtures/imports/same-file-dedup/b/shared.css b/test/fixtures/imports/same-file-dedup/b/shared.css new file mode 100644 index 0000000..6abac48 --- /dev/null +++ b/test/fixtures/imports/same-file-dedup/b/shared.css @@ -0,0 +1 @@ +shared {} diff --git a/test/fixtures/same-file-dedup.css b/test/fixtures/same-file-dedup.css new file mode 100644 index 0000000..ef02919 --- /dev/null +++ b/test/fixtures/same-file-dedup.css @@ -0,0 +1,3 @@ +@import "same-file-dedup/a/shared.css"; +@import "same-file-dedup/b/shared.css"; +@import "same-file-dedup/a/shared.css"; diff --git a/test/fixtures/same-file-dedup.expected.css b/test/fixtures/same-file-dedup.expected.css new file mode 100644 index 0000000..572363e --- /dev/null +++ b/test/fixtures/same-file-dedup.expected.css @@ -0,0 +1,2 @@ +shared {} +shared {} diff --git a/test/import.js b/test/import.js index e2a2bd1..a099db7 100644 --- a/test/import.js +++ b/test/import.js @@ -46,6 +46,21 @@ test( test("should import stylesheets with same content", checkFixture, "same") +test( + "should not skip different files with the same content by default", + checkFixture, + "same-file-dedup", +) + +test( + "should skip files with the same content with skipDuplicates: content", + checkFixture, + "content-dedup", + { + skipDuplicates: "content", + }, +) + test("should ignore & adjust external import", checkFixture, "ignore") test("should not fail with only one absolute import", t => {