Skip to content

Commit

Permalink
Added: skipDuplicates option now allows you to **not** skip duplica…
Browse files Browse the repository at this point in the history
…ted files (6.2.0)

Close #67
  • Loading branch information
MoOx committed Jul 21, 2015
1 parent 78dbf16 commit 2466a53
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 6.2.0 - 2015-07-21

- Added: `skipDuplicates` option now allows you to **not** skip duplicated files
([#67](https://github.com/postcss/postcss-import/issues/67))

# 6.1.1 - 2015-07-07

- Fixed: Prevent mutability issue, round 2
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ You can also provide manually multiples paths where to look at.

- This plugin works great with [postcss-url](https://github.com/postcss/postcss-url) plugin,
which will allow you to adjust assets `url()` (or even inline them) after inlining imported files.
- In order to optimize output, this plugin will only import a file once on a given scope (root, media query...). Tests are made from the path & the content of imported files (using a hash table).
- In order to optimize output, **this plugin will only import a file once** on a given scope (root, media query...).
Tests are made from the path & the content of imported files (using a hash table).
If this behavior is not what you want, look at `skipDuplicates` option

## Installation

Expand Down Expand Up @@ -148,6 +150,16 @@ Default: `null`

You can overwrite the default path resolving way by setting this option, using the `resolve.sync(id, opts)` signature that [resolve.sync](https://github.com/substack/node-resolve#resolvesyncid-opts) has.

#### `skipDuplicates`

Type: `Boolean`
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.

#### Example with some options

```js
Expand Down
48 changes: 27 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var moduleDirectories = [

var warnNodesMessage =
"It looks like you didn't end correctly your @import statement. " +
"Some children nodes are attached to it"
"Some children nodes are attached to it."

/**
* Inline `@import`ed files
Expand All @@ -39,6 +39,7 @@ function AtImport(options) {
root: process.cwd(),
async: false,
path: [],
skipDuplicates: true,
}, options || {})

// convert string to an array of a single element
Expand Down Expand Up @@ -178,6 +179,7 @@ function parseGlob(atRule, options, imports) {
var dir = options.source && options.source.input && options.source.input.file
? path.dirname(path.resolve(options.root, options.source.input.file))
: options.root

paths.forEach(function(p) {
p = path.resolve(dir, p)
var globbed = glob.sync(path.join(p, globPattern))
Expand Down Expand Up @@ -284,20 +286,22 @@ function readAtImport(
options.resolve
)

// skip files already imported at the same scope
if (
state.importedFiles[resolvedFilename] &&
state.importedFiles[resolvedFilename][media]
) {
detach(atRule)
return resolvedPromise
}
if (options.skipDuplicates) {
// skip files already imported at the same scope
if (
state.importedFiles[resolvedFilename] &&
state.importedFiles[resolvedFilename][media]
) {
detach(atRule)
return resolvedPromise
}

// save imported files to skip them next time
if (!state.importedFiles[resolvedFilename]) {
state.importedFiles[resolvedFilename] = {}
// save imported files to skip them next time
if (!state.importedFiles[resolvedFilename]) {
state.importedFiles[resolvedFilename] = {}
}
state.importedFiles[resolvedFilename][media] = true
}
state.importedFiles[resolvedFilename][media] = true

return readImportedContent(
result,
Expand Down Expand Up @@ -362,15 +366,17 @@ function readImportedContent(
}

var newStyles = postcss.parse(fileContent, options)
var hasImport = newStyles.some(function(child) {
return child.type === "atrule" && child.name.toLowerCase() === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[fileContent]) {
state.hashFiles[fileContent] = {}
if (options.skipDuplicates) {
var hasImport = newStyles.some(function(child) {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[fileContent]) {
state.hashFiles[fileContent] = {}
}
state.hashFiles[fileContent][media] = true
}
state.hashFiles[fileContent][media] = true
}

// recursion: import @import from imported file
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "postcss-import",
"version": "6.1.1",
"version": "6.2.0",
"description": "PostCSS plugin to import CSS files",
"keywords": [
"css",
"postcss",
"postcss-plugins",
"postcss-plugin",
"import",
"node modules",
"npm"
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/duplicates.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import "foo.css";
@import "foo.css";
@import "foo-duplicate.css";

@import "foo.css" screen;
@import "foo-duplicate2" screen;

@import "proxy-file/index.css";
@import "proxy-file/sub-directory/index.css";

content{}
15 changes: 15 additions & 0 deletions test/fixtures/duplicates.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
foo{}
foo{}
foo{}

@media screen{
foo{}
}
@media screen{
foo{}
}

proxy {}
import {}

content{}
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ function trimResultCss(result) {
}

test("@import", function(t) {
t.plan(16)
t.plan(17)

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

compareFixtures(t, "no-duplicate", "should not import a stylsheet twice")
compareFixtures(
t,
"duplicates",
"should be able to import a stylsheet twice",
{
skipDuplicates: false,
}
)

compareFixtures(t, "same", "should import stylsheets with same content")

Expand Down

0 comments on commit 2466a53

Please sign in to comment.