Skip to content

Commit f95d2a3

Browse files
committed
Add import keyword support
1 parent 5cbdd42 commit f95d2a3

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

src/index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const readFile = filepath =>
1515
});
1616
});
1717

18+
const getTokens = result =>
19+
result.messages.find(d => d.type === "icss").exportTokens;
20+
1821
const defaultFetch = (importee, importerDir, processor) => {
1922
const ext = path.extname(importee);
2023
if (ext !== ".css") {
@@ -25,7 +28,21 @@ const defaultFetch = (importee, importerDir, processor) => {
2528
const from = path.resolve(importerDir, importee);
2629
return readFile(from)
2730
.then(content => processor.process(content, { from }))
28-
.then(result => result.messages.find(d => d.type === "icss").exportTokens);
31+
.then(result =>
32+
Object.assign({}, getTokens(result), { default: result.css })
33+
);
34+
};
35+
36+
const importParamsPattern = /^(\w+)(.+)?/;
37+
38+
const importToken = (params, tokens) => {
39+
const matches = importParamsPattern.exec(params);
40+
if (matches) {
41+
const content = tokens[matches[1]];
42+
const media = matches[2];
43+
return media ? `@media ${media.trim()} {\n${content}\n}` : content;
44+
}
45+
return "";
2946
};
3047

3148
module.exports = postcss.plugin("postcss-icss", (options = {}) => (
@@ -44,15 +61,24 @@ module.exports = postcss.plugin("postcss-icss", (options = {}) => (
4461
return fetch(importee, importerDir, result.processor).then(exportTokens => {
4562
const importTokens = icssImports[key];
4663
return Object.keys(importTokens).reduce((acc, token) => {
47-
acc[token] = exportTokens[importTokens[token]];
64+
if (token === "import") {
65+
acc[token] = importToken(importTokens[token], exportTokens);
66+
} else {
67+
acc[token] = exportTokens[importTokens[token]];
68+
}
4869
return acc;
4970
}, {});
5071
});
5172
});
5273

5374
return Promise.all(promises).then(results => {
75+
const imports = results
76+
.map(result => result.import)
77+
.filter(content => Boolean(content))
78+
.join("\n");
5479
const replacements = Object.assign({}, ...results);
5580
replaceSymbols(css, replacements);
81+
css.prepend(imports);
5682

5783
Object.keys(icssExports).forEach(key => {
5884
icssExports[key] = replaceValueSymbols(icssExports[key], replacements);

test/fixtures/inline.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.a {}
2+
3+
@media screen and (min-width: 560px) {
4+
.b {}
5+
}
6+
7+
.c {}

test/test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,71 @@ test("import non-css files as resource", () => {
7676
`)
7777
);
7878
});
79+
80+
test("use 'import' keyword to inline default content", () => {
81+
return expect(
82+
runCSS(`
83+
:import('./test/fixtures/inline.css') {
84+
import: default
85+
}
86+
87+
.foo {
88+
display: block;
89+
}
90+
`)
91+
).resolves.toEqual(
92+
strip(`
93+
.a {}
94+
@media screen and (min-width: 560px) {
95+
.b {}
96+
}
97+
.c {}
98+
.foo {
99+
display: block;
100+
}
101+
`)
102+
);
103+
});
104+
105+
test("use 'import' keyword with media query", () => {
106+
return expect(
107+
runCSS(`
108+
:import('./test/fixtures/inline.css') {
109+
import: default tv, print
110+
}
111+
112+
.foo {
113+
display: block;
114+
}
115+
`)
116+
).resolves.toEqual(
117+
strip(`
118+
@media tv, print {
119+
.a {}
120+
@media screen and (min-width: 560px) {
121+
.b {}
122+
}
123+
.c {}
124+
}
125+
.foo {
126+
display: block;
127+
}
128+
`)
129+
);
130+
});
131+
132+
test("skips invalid 'import'", () => {
133+
return expect(
134+
runCSS(`
135+
:import('./test/fixtures/inline.css') {
136+
import: -
137+
}
138+
139+
.foo {}
140+
`)
141+
).resolves.toEqual(
142+
strip(`
143+
.foo {}
144+
`)
145+
);
146+
});

0 commit comments

Comments
 (0)