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 @@ -1282,7 +1282,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
25 changes: 14 additions & 11 deletions webc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -58,24 +64,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 @@ -93,7 +96,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 @@ -102,15 +105,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 @@ -218,7 +221,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