Skip to content

Commit a70fc3c

Browse files
romainmenkeunlight
authored andcommitted
feat: Add support for base64 encoded data urls
close: #30
1 parent 45e3eff commit a70fc3c

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ See [PostCSS](https://github.com/postcss/postcss#usage) docs for examples for yo
4343
- `resolveUrls` (boolean) To transform relative URLs found in remote stylesheets into fully qualified URLs ([see #18](https://github.com/unlight/postcss-import-url/pull/18)) (default: `false`)
4444
- `modernBrowser` (boolean) Set user-agent string to 'Mozilla/5.0 AppleWebKit/537.36 Chrome/80.0.0.0 Safari/537.36', this option maybe useful for importing fonts from Google. Google check `user-agent` header string and respond can be different (default: `false`)
4545
- `userAgent` (string) Custom user-agent header (default: `null`)
46+
- `dataUrls` (boolean) Store fetched CSS as base64 encoded data URLs (default: `false`)
4647

4748
## Known Issues
4849

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const defaults = {
1111
resolveUrls: false,
1212
modernBrowser: false,
1313
userAgent: null,
14+
dataUrls: false,
1415
};
1516
const space = postcss.list.space;
1617
const urlRegexp = /url\(["']?.+?['"]?\)/g;
@@ -96,10 +97,15 @@ function postcssImportUrl(options) {
9697
);
9798
}
9899

99-
const tree = await (options.recursive
100+
const importedTree = await (options.recursive
100101
? importUrl(newNode, null, r.parent)
101102
: Promise.resolve(newNode));
102-
atRule.replaceWith(tree);
103+
104+
if (options.dataUrls) {
105+
atRule.params = `url(data:text/css;base64,${Buffer.from(importedTree.toString()).toString('base64')})`;
106+
} else {
107+
atRule.replaceWith(importedTree);
108+
}
103109
},
104110
);
105111
});

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,11 @@ describe('source property', () => {
457457
expect(result.root.source.input.css).toEqual(input);
458458
});
459459
});
460+
461+
describe('base64 data urls', function () {
462+
it('option dataUrls should converts imports to base64 encoded data urls', async () => {
463+
const input = '@import url(http://fonts.googleapis.com/css?family=Tangerine);';
464+
const result = await getResult(input, { dataUrls: true });
465+
expect(result.css.trim()).toEqual('@import url(data:text/css;base64,QGZvbnQtZmFjZSB7CiAgZm9udC1mYW1pbHk6ICdUYW5nZXJpbmUnOwogIGZvbnQtc3R5bGU6IG5vcm1hbDsKICBmb250LXdlaWdodDogNDAwOwogIHNyYzogdXJsKGh0dHA6Ly9mb250cy5nc3RhdGljLmNvbS9zL3RhbmdlcmluZS92MTcvSXVyWTZZNWpfb1NjWlpvdzRWT3hDWlpKLnR0ZikgZm9ybWF0KCd0cnVldHlwZScpOwp9Cg==);');
466+
});
467+
});

0 commit comments

Comments
 (0)