Skip to content

Commit c01ae56

Browse files
committed
reordering the output css [fixes #3]
1 parent e8345f9 commit c01ae56

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

lib/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,17 @@ exports['default'] = function (css) {
102102
/* Perform replacements */
103103
(0, _icssReplaceSymbols2['default'])(css, definitions);
104104

105+
/* Add export rules if any */
106+
if (exportDeclarations.length > 0) {
107+
css.prepend(_postcss2['default'].rule({
108+
selector: ':export',
109+
raws: { after: "\n" },
110+
nodes: exportDeclarations
111+
}));
112+
}
113+
105114
/* Add import rules */
106-
importAliases.forEach(function (_ref) {
115+
importAliases.reverse().forEach(function (_ref) {
107116
var path = _ref.path;
108117
var imports = _ref.imports;
109118

@@ -122,15 +131,6 @@ exports['default'] = function (css) {
122131
})
123132
}));
124133
});
125-
126-
/* Add export rules if any */
127-
if (exportDeclarations.length > 0) {
128-
css.prepend(_postcss2['default'].rule({
129-
selector: ':export',
130-
raws: { after: "\n" },
131-
nodes: exportDeclarations
132-
}));
133-
}
134134
};
135135

136136
module.exports = exports['default'];

src/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ export default css => {
6868
/* Perform replacements */
6969
replaceSymbols(css, definitions)
7070

71+
/* Add export rules if any */
72+
if (exportDeclarations.length > 0) {
73+
css.prepend(postcss.rule({
74+
selector: `:export`,
75+
raws: { after: "\n" },
76+
nodes: exportDeclarations
77+
}))
78+
}
79+
7180
/* Add import rules */
72-
importAliases.forEach(({path, imports}) => {
81+
importAliases.reverse().forEach(({path, imports}) => {
7382
css.prepend(postcss.rule({
7483
selector: `:import(${path})`,
7584
raws: { after: "\n" },
@@ -81,13 +90,4 @@ export default css => {
8190
}))
8291
}))
8392
})
84-
85-
/* Add export rules if any */
86-
if (exportDeclarations.length > 0) {
87-
css.prepend(postcss.rule({
88-
selector: `:export`,
89-
raws: { after: "\n" },
90-
nodes: exportDeclarations
91-
}))
92-
}
9393
}

test/index.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,47 @@ describe('constants', () => {
2828
})
2929

3030
it('should import and re-export a simple constant', () => {
31-
test('@value red from "./colors.css";', ':export {\n red: i__const_red_0\n}\n:import("./colors.css") {\n i__const_red_0: red\n}')
31+
test('@value red from "./colors.css";', ':import("./colors.css") {\n i__const_red_0: red\n}\n:export {\n red: i__const_red_0\n}')
3232
})
3333

3434
it('should import a simple constant and replace usages', () => {
35-
test('@value red from "./colors.css"; .foo { color: red; }', ':export {\n red: i__const_red_1;\n}\n:import("./colors.css") {\n i__const_red_1: red;\n}\n.foo { color: i__const_red_1; }')
35+
test('@value red from "./colors.css"; .foo { color: red; }', ':import("./colors.css") {\n i__const_red_1: red;\n}\n:export {\n red: i__const_red_1;\n}\n.foo { color: i__const_red_1; }')
3636
})
3737

3838
it('should import and alias a constant and replace usages', () => {
39-
test('@value blue as red from "./colors.css"; .foo { color: red; }', ':export {\n red: i__const_red_2;\n}\n:import("./colors.css") {\n i__const_red_2: blue;\n}\n.foo { color: i__const_red_2; }')
39+
test('@value blue as red from "./colors.css"; .foo { color: red; }', ':import("./colors.css") {\n i__const_red_2: blue;\n}\n:export {\n red: i__const_red_2;\n}\n.foo { color: i__const_red_2; }')
4040
})
4141

4242
it('should import multiple from a single file', () => {
4343
test(
4444
`@value blue, red from "./colors.css";
4545
.foo { color: red; }
4646
.bar { color: blue }`,
47-
`:export {
48-
blue: i__const_blue_3;
49-
red: i__const_red_4;
50-
}
51-
:import("./colors.css") {
47+
`:import("./colors.css") {
5248
i__const_blue_3: blue;
5349
i__const_red_4: red;
5450
}
51+
:export {
52+
blue: i__const_blue_3;
53+
red: i__const_red_4;
54+
}
5555
.foo { color: i__const_red_4; }
5656
.bar { color: i__const_blue_3 }`)
5757
})
5858

5959
it('should import from a definition', () => {
6060
test(
6161
'@value colors: "./colors.css"; @value red from colors;',
62-
':export {\n colors: "./colors.css";\n red: i__const_red_5\n}\n' +
63-
':import("./colors.css") {\n i__const_red_5: red\n}'
62+
':import("./colors.css") {\n i__const_red_5: red\n}\n' +
63+
':export {\n colors: "./colors.css";\n red: i__const_red_5\n}'
6464
)
6565
})
6666

6767
it('should only allow values for paths if defined in the right order', () => {
6868
test(
6969
'@value red from colors; @value colors: "./colors.css";',
70-
':export {\n red: i__const_red_6;\n colors: "./colors.css"\n}\n' +
71-
':import(colors) {\n i__const_red_6: red\n}'
70+
':import(colors) {\n i__const_red_6: red\n}\n' +
71+
':export {\n red: i__const_red_6;\n colors: "./colors.css"\n}'
7272
)
7373
})
7474

@@ -85,5 +85,9 @@ describe('constants', () => {
8585
':export {\n base: 10px;\n large: calc(10px * 2);\n}\n.a { margin: calc(10px * 2); }'
8686
)
8787
})
88+
89+
it('should preserve import order', () => {
90+
test('@value a from "./a.css"; @value b from "./b.css";', ':import("./a.css") {\n i__const_a_7: a\n}\n:import("./b.css") {\n i__const_b_8: b\n}\n:export {\n a: i__const_a_7;\n b: i__const_b_8\n}')
91+
})
8892
})
8993

0 commit comments

Comments
 (0)