Skip to content

Commit

Permalink
add data url support (#515)
Browse files Browse the repository at this point in the history
* add data url support

* more complex tests

* fix older node

* Update lib/data-url.js

Co-authored-by: Ryan Zimmerman <opensrc@ryanzim.com>

Co-authored-by: Ryan Zimmerman <opensrc@ryanzim.com>
  • Loading branch information
romainmenke and RyanZim committed Dec 7, 2022
1 parent 2c0c3e9 commit 90e035b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
9 changes: 9 additions & 0 deletions index.js
Expand Up @@ -10,6 +10,7 @@ const loadContent = require("./lib/load-content")
const processContent = require("./lib/process-content")
const parseStatements = require("./lib/parse-statements")
const assignLayerNames = require("./lib/assign-layer-names")
const dataURL = require("./lib/data-url")

function AtImport(options) {
options = {
Expand Down Expand Up @@ -290,6 +291,14 @@ function AtImport(options) {
}

function resolveImportId(result, stmt, options, state) {
if (dataURL.isValid(stmt.uri)) {
return loadImportContent(result, stmt, stmt.uri, options, state).then(
result => {
stmt.children = result
}
)
}

const atRule = stmt.node
let sourceFile
if (atRule.source?.input?.file) {
Expand Down
17 changes: 17 additions & 0 deletions lib/data-url.js
@@ -0,0 +1,17 @@
"use strict"

const dataURLRegexp = /^data:text\/css;base64,/i

function isValid(url) {
return dataURLRegexp.test(url)
}

function contents(url) {
// "data:text/css;base64,".length === 21
return Buffer.from(url.slice(21), "base64").toString()
}

module.exports = {
isValid,
contents,
}
9 changes: 8 additions & 1 deletion lib/load-content.js
@@ -1,5 +1,12 @@
"use strict"

const readCache = require("read-cache")
const dataURL = require("./data-url")

module.exports = filename => readCache(filename, "utf-8")
module.exports = filename => {
if (dataURL.isValid(filename)) {
return dataURL.contents(filename)
}

return readCache(filename, "utf-8")
}
8 changes: 8 additions & 0 deletions test/data-url.js
@@ -0,0 +1,8 @@
"use strict"
// external tooling
const test = require("ava")

// internal tooling
const checkFixture = require("./helpers/check-fixture")

test("should inline data urls", checkFixture, "data-url")
6 changes: 6 additions & 0 deletions test/fixtures/data-url.css
@@ -0,0 +1,6 @@
@import url(data:text/css;base64,QGltcG9ydCB1cmwoZGF0YTp0ZXh0L2NzcztiYXNlNjQsY0NCN0lHTnZiRzl5T2lCbmNtVmxianNnZlE9PSk7CgpwIHsgY29sb3I6IGJsdWU7IH0K);
@import url("DATA:TEXT/CSS;BASE64,QGltcG9ydCB1cmwoZGF0YTp0ZXh0L2NzcztiYXNlNjQsY0NCN0lHTnZiRzl5T2lCbmNtVmxianNnZlE9PSk7CgpwIHsgY29sb3I6IGJsdWU7IH0K") layer(foo) (min-width: 320px);

/* Mixed imports: */
@import url(data:text/css;base64,QGltcG9ydCB1cmwoZm9vLmNzcyk7CgpwIHsKICBjb2xvcjogYmx1ZTsKfQo=);
@import url(data-url.css);
24 changes: 24 additions & 0 deletions test/fixtures/data-url.expected.css
@@ -0,0 +1,24 @@
p { color: green; }

p { color: blue; }

@media (min-width: 320px) {

@layer foo {
p { color: green; } } }

@media (min-width: 320px) {

@layer foo {

p { color: blue; } } }

/* Mixed imports: */

foo{}

p {
color: blue;
}

p { color: pink; }
1 change: 1 addition & 0 deletions test/fixtures/imports/data-url.css
@@ -0,0 +1 @@
@import url("DATA:text/CSS;BASE64,cCB7IGNvbG9yOiBwaW5rOyB9");

0 comments on commit 90e035b

Please sign in to comment.