Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data url support #515

Merged
merged 4 commits into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
romainmenke marked this conversation as resolved.
Show resolved Hide resolved
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");