From 00e032c9e7df163c3ddf72b93efcc9359f98906c Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 14 Mar 2023 08:18:22 -0500 Subject: [PATCH 01/11] Status --- src/ast.js | 19 ++++++++++++++----- src/path.js | 8 ++++++++ webc.js | 20 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/ast.js b/src/ast.js index 5b18af7..aa78e88 100644 --- a/src/ast.js +++ b/src/ast.js @@ -137,6 +137,19 @@ class AstSerializer { SCOPED: "css:scoped", }; + getComponentCache() { + return this.components; + } + + restorePreparsedComponents(components = {}) { + for(let filePath in components) { + let name = Path.getComponentNameFromFilePath(filePath); + this.componentMapNameToFilePath[name] = filePath; + } + + Object.assign(this.components, components); + } + setBundlerMode(mode) { this.bundlerMode = !!mode; } @@ -179,10 +192,6 @@ class AstSerializer { this.dataCascade.setGlobalData(data); } - restorePreparsedComponents(components) { - Object.assign(this.components, components); - } - setUidFunction(fn) { this.uidFn = fn; } @@ -486,7 +495,7 @@ class AstSerializer { promises.push(this.preparseComponentByFilePath(this.componentMapNameToFilePath[name])); } - return Promise.all(promises); + await Promise.all(promises); } // This *needs* to be depth first instead of breadth first for **streaming** diff --git a/src/path.js b/src/path.js index 800c95c..d7d9a02 100644 --- a/src/path.js +++ b/src/path.js @@ -9,6 +9,14 @@ class Path { } return filePath; } + + static getComponentNameFromFilePath(filePath) { + if(typeof filePath === "string") { + let p = path.parse(filePath); + return p.name; + } + return filePath; + } } export { Path }; \ No newline at end of file diff --git a/webc.js b/webc.js index 1f596d1..af25e38 100644 --- a/webc.js +++ b/webc.js @@ -15,6 +15,7 @@ class WebC { constructor(options = {}) { let { file, input } = options; + this.componentCache = {}; this.customTransforms = {}; this.customHelpers = {}; this.globalComponents = {}; @@ -207,11 +208,30 @@ class WebC { this.uidFn = fn; } + static async preparseComponents(components) { + let ast = new AstSerializer(); + await ast.setComponentsByFilePath(components); + + return ast.getComponentCache(); + } + + static async preparseContent(inputPath, content) { + let node = await WebC.getASTFromString(content); + let ast = new AstSerializer(); + await ast.preparseComponentByFilePath(inputPath, node, content); + return ast.getComponentCache(); + } + + setPreparsedComponents(components = {}) { + Object.assign(this.componentCache, components); + } + async setup(options = {}) { let { content, mode } = this.getContent(); let rawAst = this.getAST(content); let ast = new AstSerializer(this.astOptions); + ast.restorePreparsedComponents(this.componentCache); ast.setBundlerMode(this.bundlerMode); ast.setMode(mode); ast.setContent(content); From dd451ff27ca1aa3039a216b2783e505ad45545db Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 14 Mar 2023 08:48:58 -0500 Subject: [PATCH 02/11] First draft of component manager --- src/ast.js | 284 ++++------------------------------------ src/astQuery.js | 64 +++++++++ src/componentManager.js | 185 ++++++++++++++++++++++++++ src/path.js | 8 -- webc.js | 20 --- 5 files changed, 277 insertions(+), 284 deletions(-) create mode 100644 src/componentManager.js diff --git a/src/ast.js b/src/ast.js index aa78e88..0ae1cd5 100644 --- a/src/ast.js +++ b/src/ast.js @@ -1,6 +1,5 @@ import path from "path"; import os from "os"; -import { createHash } from "crypto"; import { DepGraph } from "dependency-graph"; import { WebC } from "../webc.js"; @@ -16,6 +15,7 @@ import { nanoid } from "nanoid"; import { ModuleResolution } from "./moduleResolution.js"; import { FileSystemCache } from "./fsCache.js" import { DataCascade } from "./dataCascade.js" +import { ComponentManager } from "./componentManager.js" /** @typedef {import('parse5/dist/tree-adapters/default').Node} Node */ /** @typedef {import('parse5/dist/tree-adapters/default').Template} Template */ @@ -82,9 +82,6 @@ class AstSerializer { // Component cache this.componentMapNameToFilePath = {}; - this.components = {}; - - this.hashOverrides = {}; this.streams = new Streams(["html", "css", "js"]); @@ -96,7 +93,9 @@ class AstSerializer { renderAttributes: (attributesObject) => { return AttributeSerializer.getString(attributesObject); } - }) + }); + + this.componentManager = new ComponentManager(); } set filePath(value) { @@ -137,19 +136,6 @@ class AstSerializer { SCOPED: "css:scoped", }; - getComponentCache() { - return this.components; - } - - restorePreparsedComponents(components = {}) { - for(let filePath in components) { - let name = Path.getComponentNameFromFilePath(filePath); - this.componentMapNameToFilePath[name] = filePath; - } - - Object.assign(this.components, components); - } - setBundlerMode(mode) { this.bundlerMode = !!mode; } @@ -211,122 +197,9 @@ class AstSerializer { } } - // Support for `base64url` needs gating e.g. is not available on Stackblitz on Node 16 - // https://github.com/nodejs/node/issues/26512 - getDigest(hash) { - let prefix = "w"; - let hashLength = 8; - let digest; - if(Buffer.isEncoding('base64url')) { - digest = hash.digest("base64url"); - } else { - // https://github.com/11ty/eleventy-img/blob/e51ad8e1da4a7e6528f3cc8f4b682972ba402a67/img.js#L343 - digest = hash.digest('base64').replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); - } - return prefix + digest.toLowerCase().slice(0, hashLength); - } - - async getSetupScriptValue(component, filePath) { - //