Skip to content

Commit 2466a53

Browse files
committed
Added: skipDuplicates option now allows you to **not** skip duplicated files (6.2.0)
Close #67
1 parent 78dbf16 commit 2466a53

File tree

7 files changed

+82
-25
lines changed

7 files changed

+82
-25
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 6.2.0 - 2015-07-21
2+
3+
- Added: `skipDuplicates` option now allows you to **not** skip duplicated files
4+
([#67](https://github.com/postcss/postcss-import/issues/67))
5+
16
# 6.1.1 - 2015-07-07
27

38
- Fixed: Prevent mutability issue, round 2

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ You can also provide manually multiples paths where to look at.
1010

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

1517
## Installation
1618

@@ -148,6 +150,16 @@ Default: `null`
148150

149151
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.
150152

153+
#### `skipDuplicates`
154+
155+
Type: `Boolean`
156+
Default: `true`
157+
158+
By default, similar files (based on the same content) are being skipped.
159+
It's to optimize output and skip similar files like `normalize.css` for example.
160+
If this behavior is not what you want, just set this option to `false` to
161+
disable it.
162+
151163
#### Example with some options
152164

153165
```js

index.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var moduleDirectories = [
2727

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

3232
/**
3333
* Inline `@import`ed files
@@ -39,6 +39,7 @@ function AtImport(options) {
3939
root: process.cwd(),
4040
async: false,
4141
path: [],
42+
skipDuplicates: true,
4243
}, options || {})
4344

4445
// convert string to an array of a single element
@@ -178,6 +179,7 @@ function parseGlob(atRule, options, imports) {
178179
var dir = options.source && options.source.input && options.source.input.file
179180
? path.dirname(path.resolve(options.root, options.source.input.file))
180181
: options.root
182+
181183
paths.forEach(function(p) {
182184
p = path.resolve(dir, p)
183185
var globbed = glob.sync(path.join(p, globPattern))
@@ -284,20 +286,22 @@ function readAtImport(
284286
options.resolve
285287
)
286288

287-
// skip files already imported at the same scope
288-
if (
289-
state.importedFiles[resolvedFilename] &&
290-
state.importedFiles[resolvedFilename][media]
291-
) {
292-
detach(atRule)
293-
return resolvedPromise
294-
}
289+
if (options.skipDuplicates) {
290+
// skip files already imported at the same scope
291+
if (
292+
state.importedFiles[resolvedFilename] &&
293+
state.importedFiles[resolvedFilename][media]
294+
) {
295+
detach(atRule)
296+
return resolvedPromise
297+
}
295298

296-
// save imported files to skip them next time
297-
if (!state.importedFiles[resolvedFilename]) {
298-
state.importedFiles[resolvedFilename] = {}
299+
// save imported files to skip them next time
300+
if (!state.importedFiles[resolvedFilename]) {
301+
state.importedFiles[resolvedFilename] = {}
302+
}
303+
state.importedFiles[resolvedFilename][media] = true
299304
}
300-
state.importedFiles[resolvedFilename][media] = true
301305

302306
return readImportedContent(
303307
result,
@@ -362,15 +366,17 @@ function readImportedContent(
362366
}
363367

364368
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) {
369-
// save hash files to skip them next time
370-
if (!state.hashFiles[fileContent]) {
371-
state.hashFiles[fileContent] = {}
369+
if (options.skipDuplicates) {
370+
var hasImport = newStyles.some(function(child) {
371+
return child.type === "atrule" && child.name === "import"
372+
})
373+
if (!hasImport) {
374+
// save hash files to skip them next time
375+
if (!state.hashFiles[fileContent]) {
376+
state.hashFiles[fileContent] = {}
377+
}
378+
state.hashFiles[fileContent][media] = true
372379
}
373-
state.hashFiles[fileContent][media] = true
374380
}
375381

376382
// recursion: import @import from imported file

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "postcss-import",
3-
"version": "6.1.1",
3+
"version": "6.2.0",
44
"description": "PostCSS plugin to import CSS files",
55
"keywords": [
66
"css",
77
"postcss",
8-
"postcss-plugins",
8+
"postcss-plugin",
99
"import",
1010
"node modules",
1111
"npm"

test/fixtures/duplicates.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import "foo.css";
2+
@import "foo.css";
3+
@import "foo-duplicate.css";
4+
5+
@import "foo.css" screen;
6+
@import "foo-duplicate2" screen;
7+
8+
@import "proxy-file/index.css";
9+
@import "proxy-file/sub-directory/index.css";
10+
11+
content{}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
foo{}
2+
foo{}
3+
foo{}
4+
5+
@media screen{
6+
foo{}
7+
}
8+
@media screen{
9+
foo{}
10+
}
11+
12+
proxy {}
13+
import {}
14+
15+
content{}

test/index.js

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

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

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

3939
compareFixtures(t, "no-duplicate", "should not import a stylsheet twice")
40+
compareFixtures(
41+
t,
42+
"duplicates",
43+
"should be able to import a stylsheet twice",
44+
{
45+
skipDuplicates: false,
46+
}
47+
)
4048

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

0 commit comments

Comments
 (0)