Skip to content

Commit

Permalink
Properly resolve relative paths in CSS imports (#527)
Browse files Browse the repository at this point in the history
• Fix inconsistency in function argument type formatting.
• Resolve relative paths when during import resolves in CSS files.

Co-authored-by: SpacingBat3 <[email protected]>
  • Loading branch information
MatthewCash and SpacingBat3 authored Mar 18, 2024
1 parent 92a6302 commit 83a912d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions sources/code/main/modules/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { resolve } from "url";
import { commonCatches } from "./error";

const safeStoragePromise = (import("electron/main"))
.then(main => main.safeStorage);

async function fetchOrRead(file:string, signal?:AbortSignal) {
async function fetchOrRead(url:URL, signal?:AbortSignal) {
const readFile = import("fs/promises").then(fs => fs.readFile);

const url = new URL(file);
if(url.protocol === "file:")
return { read: (await readFile)(url.pathname, {signal}) };
else
Expand All @@ -19,19 +19,19 @@ async function fetchOrRead(file:string, signal?:AbortSignal) {
*
* **Experimental** – it is unknown if that would work properly for all themes.
*/
async function parseImports(cssString: string, importCalls:string[], maxTries=5):Promise<string> {
async function parseImports(cssString: string, importCalls: string[], maxTries=5):Promise<string> {
const anyImport = /^@import .+?$/gm;
if(!anyImport.test(cssString)) return cssString;
const promises:Promise<unknown>[] = [];
cssString.match(anyImport)?.forEach(singleImport => {
const matches = /^@import (?:(?:url\()?["']?([^"';)]*)["']?)\)?;?/m.exec(singleImport);
if(matches?.[0] === undefined || matches[1] === undefined) return;
const file = matches[1];
const file = resolve(importCalls.at(-1) ?? "", matches[1]);
if(importCalls.includes(file)) {
promises.push(Promise.reject(new Error("Circular reference in CSS imports are disallowed!")));
promises.push(Promise.reject(new Error("Circular reference in CSS imports are disallowed: " + file)));
return;
}
promises.push(fetchOrRead(file)
promises.push(fetchOrRead(new URL(file))
.then(data => {
if (data.download)
return data.download.then(data => data.text());
Expand All @@ -46,8 +46,8 @@ async function parseImports(cssString: string, importCalls:string[], maxTries=5)
const rejection = result.findIndex(({status})=> status === "rejected");
if(rejection >= 0) {
if(maxTries > 0) {
console.warn("Couldn't resolve CSS theme imports, retrying again...");
maxTries--;
console.warn("Couldn't resolve CSS theme imports, retrying...");
return parseImports(cssString, importCalls, maxTries - 1);
}
else await promises[rejection];
}
Expand Down

0 comments on commit 83a912d

Please sign in to comment.