Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ class AstSerializer {
let childContent;
if(externalSource) { // fetch file contents, note that child content of the node is ignored here
// We could check to make sure this isn’t already in the asset aggregation bucket *before* we read but that could result in out-of-date content
let fileContent = this.fileCache.read(externalSource, options.closestParentComponent || this.filePath);
let fileContent = await this.fileCache.read(externalSource, options.closestParentComponent || this.filePath);
childContent = await this.transformContent(fileContent, options.currentTransformTypes, node, slots, options, streamEnabled);
} else {
let { html } = await this.getChildContent(node, slots, options, false);
Expand Down
12 changes: 5 additions & 7 deletions src/fsCache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { customLoader } from "../webc.js";

class FileSystemCache {
constructor() {
Expand All @@ -24,12 +25,12 @@ class FileSystemCache {
}

isFileInProjectDirectory(filePath) {
let workingDir = path.resolve();
let absoluteFile = path.resolve(filePath);
let workingDir = customLoader.resolve();
let absoluteFile = customLoader.resolve(filePath);
return absoluteFile.startsWith(workingDir);
}

read(filePath, relativeTo) {
async read(filePath, relativeTo) {
if(this.isFullUrl(filePath)) {
throw new Error(`Full URLs in <script> and <link rel="stylesheet"> are not yet supported without webc:keep.`);
}
Expand All @@ -41,11 +42,8 @@ class FileSystemCache {
}

if(!this.contents[filePath]) {
this.contents[filePath] = fs.readFileSync(filePath, {
encoding: "utf8"
});
this.contents[filePath] = await customLoader.load(filePath);
}

return this.contents[filePath];
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/parserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test("No Quirks mode default (HTML file without doctype)", async t => {
let component = new WebC();
component.setContent(`<html><div class="red"></div></html>`);

let {content} = component.getContent();
let {content} = await component.getContent();
let ast = await component.getAST(content);

t.is("no-quirks", ast.mode);
Expand All @@ -96,7 +96,7 @@ test("No Quirks mode default", async t => {
let component = new WebC();
component.setContent(`<div class="red"></div>`);

let {content} = component.getContent();
let {content} = await component.getContent();
let ast = await component.getAST(content);

t.is("no-quirks", ast.mode);
Expand Down
27 changes: 15 additions & 12 deletions webc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from "node:fs";
import fs from "node:fs/promises";
import fastglob from "fast-glob";
import isGlob from "is-glob";
import path from "node:path";
Expand All @@ -12,6 +12,12 @@ export { ComponentManager } from "./src/componentManager.js";

const localAstCache = new AstCache();

// VFS support
export const customLoader = {
"load": (path) => fs.readFile(path, {encoding: "utf8"}),
"resolve": path.resolve,
}

export class WebC {
constructor(options = {}) {
let { file, input } = options;
Expand Down Expand Up @@ -59,24 +65,21 @@ export class WebC {
return "page";
}

_getRawContent() {
async _getRawContent() {
if(this.rawInput || this.rawInput === "") {
return this.rawInput;
} else if(this.filePath) {
if(!this._cachedContent) {
this._cachedContent = fs.readFileSync(this.filePath, {
encoding: "utf8"
});
this._cachedContent = await customLoader.load(this.filePath);
}

return this._cachedContent;
} else {
throw new Error("Missing a setInput or setInputPath method call to set the input.");
}
}

getContent() {
let content = this._getRawContent();
async getContent() {
let content = await this._getRawContent();
let mode = this.getRenderingMode(content.trimStart());

// prepend for no-quirks mode on components or implicit page rendering modes (starts with <html>)
Expand All @@ -94,7 +97,7 @@ export class WebC {
let wc = new WebC({
input: string
});
let { content } = wc.getContent();
let { content } = await wc.getContent();
return wc.getAST(content);
}

Expand All @@ -103,15 +106,15 @@ export class WebC {
let wc = new WebC({
file: filePath
});
let { content } = wc.getContent();
let { content } = await wc.getContent();
return wc.getAST(content);
}

static async getFromFilePath(filePath) {
let wc = new WebC({
file: filePath
});
let { content, mode } = wc.getContent();
let { content, mode } = await wc.getContent();

return {
content,
Expand Down Expand Up @@ -219,7 +222,7 @@ export class WebC {
}

async setup(options = {}) {
let { content, mode } = this.getContent();
let { content, mode } = await this.getContent();
let rawAst = this.getAST(content);

let ast = new AstSerializer(this.astOptions);
Expand Down