Skip to content

Commit 50d7de4

Browse files
authored
Merge pull request #104 from npavlov/combine-imported-styles
feat: combine classes from imported styles
2 parents c5cb9ec + c3a8cb7 commit 50d7de4

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/file-system-loader.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,25 @@ export default class FileSystemLoader {
6868
}
6969

7070
const { injectableSource, exportTokens } = await this.core.load(source, rootRelativePath, trace, this.fetch.bind(this));
71+
72+
const re = new RegExp(/@import\s'(\D+?.css)';/, 'gm');
73+
74+
let importTokens: Core.ExportTokens = {};
75+
76+
let result;
77+
78+
while (result = re.exec(injectableSource)) {
79+
const importFile = result?.[1];
80+
81+
if (importFile) {
82+
const localTokens = await this.fetch(importFile, rootRelativePath, undefined, initialContents);
83+
84+
Object.assign(importTokens, localTokens);
85+
}
86+
}
87+
7188
this.sources[trace] = injectableSource;
7289
this.tokensByFile[fileRelativePath] = exportTokens;
73-
return exportTokens;
90+
return {...exportTokens, ...importTokens};
7491
}
7592
}

test/combined/combined.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import './imported.css';
2+
@import '../composee.css';
3+
4+
.block {
5+
display: block;
6+
}

test/combined/imported.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.myClass {
2+
display: block;
3+
}

test/dts-creator.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ describe('DtsCreator', () => {
5151
done();
5252
});
5353
});
54+
it('returns DtsContent instance combined css', done => {
55+
creator.create('test/combined/combined.css').then(content => {
56+
assert.equal(content.contents.length, 3);
57+
assert.equal(content.contents[0], 'readonly "block": string;');
58+
assert.equal(content.contents[1], 'readonly "myClass": string;');
59+
assert.equal(content.contents[2], 'readonly "box": string;');
60+
done();
61+
});
62+
});
5463
});
5564

5665
describe('#modify path', () => {

0 commit comments

Comments
 (0)