Skip to content

Commit e985ca6

Browse files
committed
Fixed: indentation and code style are now preserved (5.0.2)
Close #20 Ref #184
1 parent 4cfa06b commit e985ca6

14 files changed

+85
-59
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 5.0.2 - 2015-02-14
2+
3+
- Fixed: indentation and code style are now preserved ([#20](https://github.com/postcss/postcss-import/issues/20))
4+
15
# 5.0.1 - 2015-02-13
26

37
- Fixed: breaking bug with remote stylesheets ([#21](https://github.com/postcss/postcss-import/issues/21) & [#22](https://github.com/postcss/postcss-import/issues/22))

index.js

+50-29
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ function AtImport(options) {
7777
* @param {Object} options
7878
*/
7979
function parseStyles(styles, options, cb, importedFiles, ignoredAtRules, media) {
80-
styles.eachAtRule(function checkAtRule(atRule) {
81-
if (atRule.name !== "import") {
82-
return
83-
}
84-
80+
var imports = []
81+
styles.eachAtRule("import", function checkAtRule(atRule) {imports.push(atRule)})
82+
imports.forEach(function(atRule) {
8583
helpers.try(function transformAtImport() {
8684
readAtImport(atRule, options, cb, importedFiles, ignoredAtRules, media)
8785
}, atRule.source)
@@ -102,14 +100,16 @@ function addIgnoredAtRulesOnTop(styles, ignoredAtRules) {
102100
while (i--) {
103101
var ignoredAtRule = ignoredAtRules[i][0]
104102
ignoredAtRule.params = ignoredAtRules[i][1].fullUri + (ignoredAtRules[i][1].media ? " " + ignoredAtRules[i][1].media : "")
105-
ignoredAtRule.before = "\n"
106-
ignoredAtRule.after = ""
103+
104+
// keep ast ref
105+
ignoredAtRule.parent = styles
107106

108107
// don't use prepend() to avoid weird behavior of normalize()
109108
styles.nodes.unshift(ignoredAtRule)
110109
}
111110

112-
if (first && first.before !== undefined) {
111+
// separate remote import a little with others rules if no newlines already
112+
if (first && first.before.indexOf("\n") === -1) {
113113
first.before = "\n\n" + first.before
114114
}
115115
}
@@ -126,33 +126,38 @@ function readAtImport(atRule, options, cb, importedFiles, ignoredAtRules, media)
126126
// @todo extract what can be interesting from this one
127127
var parsedAtImport = parseImport(atRule.params, atRule.source)
128128

129-
130-
131129
// adjust media according to current scope
132130
media = parsedAtImport.media ? (media ? media + " and " : "") + parsedAtImport.media : (media ? media : null)
133131

134132
// just update protocol base uri (protocol://url) or protocol-relative (//url) if media needed
135133
if (parsedAtImport.uri.match(/^(?:[a-z]+:)?\/\//i)) {
136134
parsedAtImport.media = media
137-
var atRuleCloned = atRule.clone()
138-
atRuleCloned.parent = atRule.parent.clone()
139-
ignoredAtRules.push([atRuleCloned, parsedAtImport])
140-
atRule.removeSelf()
135+
136+
// save
137+
ignoredAtRules.push([atRule, parsedAtImport])
138+
139+
// detach
140+
detach(atRule)
141+
141142
return
142143
}
143144

144145
addInputToPath(options)
145146
var resolvedFilename = resolveFilename(parsedAtImport.uri, options.root, options.path, atRule.source)
146147

148+
// skip files already imported at the same scope
147149
if (importedFiles[resolvedFilename] && importedFiles[resolvedFilename][media]) {
148-
atRule.removeSelf()
150+
detach(atRule)
149151
return
150152
}
153+
154+
// save imported files to skip them next time
151155
if (!importedFiles[resolvedFilename]) {
152156
importedFiles[resolvedFilename] = {}
153157
}
154158
importedFiles[resolvedFilename][media] = true
155159

160+
156161
readImportedContent(atRule, parsedAtImport, clone(options), resolvedFilename, cb, importedFiles, ignoredAtRules)
157162
}
158163

@@ -179,7 +184,7 @@ function readImportedContent(atRule, parsedAtImport, options, resolvedFilename,
179184

180185
if (fileContent.trim() === "") {
181186
console.log(helpers.message(resolvedFilename + " is empty", atRule.source))
182-
atRule.removeSelf()
187+
detach(atRule)
183188
return
184189
}
185190

@@ -199,29 +204,41 @@ function readImportedContent(atRule, parsedAtImport, options, resolvedFilename,
199204
* @param {Object} newStyles
200205
*/
201206
function insertRules(atRule, parsedAtImport, newStyles) {
207+
var newNodes = newStyles.nodes
208+
202209
// wrap rules if the @import have a media query
203210
if (parsedAtImport.media && parsedAtImport.media.length) {
211+
// better output
212+
if (newStyles.nodes && newStyles.nodes.length) {
213+
newStyles.nodes[0].before = newStyles.nodes[0].before || "\n"
214+
// newStyles.nodes[newStyles.nodes.length - 1].after = (newStyles.nodes[newStyles.nodes.length - 1].after || "") + "\n"
215+
}
216+
204217
// wrap new rules with media (media query)
205218
var wrapper = postcss.atRule({
206219
name: "media",
207220
params: parsedAtImport.media
208221
})
209-
wrapper.append(newStyles)
210-
newStyles = wrapper
211222

212-
// better output
213-
newStyles.before = atRule.before
214-
if (newStyles.nodes && newStyles.nodes.length) {
215-
newStyles.nodes[0].before = newStyles.nodes[0].before || "\n"
216-
}
217-
newStyles.after = atRule.after || "\n"
223+
// keep ast clean
224+
newNodes.forEach(function(node) {node.parent = wrapper})
225+
wrapper.source = atRule.source
226+
227+
// copy code style
228+
wrapper.before = atRule.before
229+
wrapper.after = atRule.after
230+
231+
// move nodes
232+
wrapper.nodes = newNodes
233+
newNodes = [wrapper]
218234
}
219-
else if (newStyles.nodes && newStyles.nodes.length) {
220-
newStyles.nodes[0].before = atRule.before
235+
else if (newNodes && newNodes.length) {
236+
newNodes[0].before = atRule.before
221237
}
222-
223-
atRule.parent.insertBefore(atRule, newStyles)
224-
atRule.removeSelf()
238+
// replace atRule by imported nodes
239+
var nodes = atRule.parent.nodes
240+
nodes.splice.apply(nodes, [nodes.indexOf(atRule), 0].concat(newNodes))
241+
detach(atRule)
225242
}
226243

227244
/**
@@ -317,3 +334,7 @@ function addInputToPath(options) {
317334
}
318335
}
319336
}
337+
338+
function detach(node) {
339+
node.parent.nodes.splice(node.parent.nodes.indexOf(node), 1)
340+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-import",
3-
"version": "5.0.1",
3+
"version": "5.0.2",
44
"description": "PostCSS plugin to import CSS files",
55
"keywords": [
66
"css",

test/fixtures/cwd.expected.css

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
foo {}
2-
bar {}
3-
foo.recursive {}
1+
foo{}
2+
bar{}
3+
4+
foo.recursive{}

test/fixtures/ignore.expected.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@import url('//css');
1515
@import url(//css);
1616

17-
@media (min-width: 25em){
17+
@media (min-width: 25em) {
1818
ignore{}
1919
}
2020

test/fixtures/imports/qux.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
quux{
2-
not: "relative/qux"
2+
not: "relative/qux"
33
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@import "qux.css";
22

33
baz{
4-
is: relative
4+
is: relative
55
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
qux{
2-
is: relative
2+
is: relative
33
}

test/fixtures/modules.expected.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.nested{}
44
.byHand{}
55
.dep{}
6-
@media screen{
6+
@media screen {
77
.dep{}
88
}
99

@@ -12,7 +12,7 @@
1212
.web-nested{}
1313
.web-byHand{}
1414
.web-dep{}
15-
@media screen{
15+
@media screen {
1616
.web-dep{}
1717
}
1818

test/fixtures/recursive.expected.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
@media (min-width: 25em){
1+
@media (min-width: 25em) {
22
bar{}
3+
34
foo.recursive{}
45
}
56

test/fixtures/relative.expected.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
qux{
22
is: relative
33
}
4+
45
baz{
5-
is: relative
6+
is: relative
67
}
78
quux{
8-
not: "relative/qux"
9+
not: "relative/qux"
910
}
1011
content{}

test/fixtures/simple.expected.css

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
foo{}
2-
@media screen{
2+
@media screen {
33
foo{}
44
}
55
bar{}
6-
@media screen{
6+
@media screen {
77
bar{}
88
}
99
baz{}
10-
@media screen{
10+
@media screen {
1111
baz{}
1212
}
1313
foobar{}
14-
@media screen and (min-width: 25em){
14+
@media screen and (min-width: 25em) {
1515
foobar{}
1616
}
1717
foobarbaz{}
18-
@media print, screen and (min-width: 25em){
18+
@media print, screen and (min-width: 25em) {
1919
foobarbaz{}
2020
}
2121

test/fixtures/transform.expected.css

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
foo{
2-
styl: true
1+
foo {
2+
styl: true;
33
}
4-
@media (min-width: 25em){
5-
foo{
6-
styl: true
4+
@media (min-width: 25em) {
5+
foo {
6+
styl: true;
77
}
88
}
99

10-
content{}
10+
content{}

test/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ test("@import", function(t) {
6363
"should not fail with only one absolute import"
6464
)
6565

66-
base = "@import url('http://');\n@import 'test/fixtures/imports/foo.css';";
6766
t.equal(
6867
postcss()
6968
.use(atImport())
70-
.process(base)
69+
.process("@import url('http://');\n@import 'test/fixtures/imports/foo.css';")
7170
.css.trim(),
72-
"@import url('http://');\nfoo {}",
71+
"@import url('http://');\nfoo{}",
7372
"should not fail with absolute and local import"
7473
)
7574

@@ -130,13 +129,12 @@ test("@import callback", function(t) {
130129
})
131130

132131
test("import relative files using path option only", function(t) {
133-
var base = " content{}"
134132
t.equal(
135133
postcss()
136134
.use(atImport({path: "test/fixtures/imports/relative"}))
137-
.process(read("fixtures/imports/relative/import") + base)
135+
.process(read("fixtures/imports/relative/import"))
138136
.css.trim(),
139-
read("fixtures/imports/bar") + base
137+
read("fixtures/imports/bar")
140138
)
141139
t.end()
142140
})

0 commit comments

Comments
 (0)