summaryrefslogtreecommitdiff
path: root/src/compiler/optimize/index.ts
diff options
context:
space:
mode:
authorGravatar Drew Powers <1369770+drwpow@users.noreply.github.com> 2021-04-01 10:25:28 -0600
committerGravatar GitHub <noreply@github.com> 2021-04-01 10:25:28 -0600
commitc26c244ca2634d462616d6cf71072fbe26becba2 (patch)
tree1bd5e83eff9ca88200aacb272c84439f192015ec /src/compiler/optimize/index.ts
parentf6a7ac67befff863e34133673efb78ea7ac0fe48 (diff)
downloadastro-c26c244ca2634d462616d6cf71072fbe26becba2.tar.gz
astro-c26c244ca2634d462616d6cf71072fbe26becba2.tar.zst
astro-c26c244ca2634d462616d6cf71072fbe26becba2.zip
Annoying Lint PR #2 (#47)
Diffstat (limited to 'src/compiler/optimize/index.ts')
-rw-r--r--src/compiler/optimize/index.ts10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/compiler/optimize/index.ts b/src/compiler/optimize/index.ts
index 53dd3f2d6..a7bf828e0 100644
--- a/src/compiler/optimize/index.ts
+++ b/src/compiler/optimize/index.ts
@@ -13,6 +13,7 @@ interface VisitorCollection {
leave: Map<string, VisitorFn[]>;
}
+/** Add visitors to given collection */
function addVisitor(visitor: NodeVisitor, collection: VisitorCollection, nodeName: string, event: 'enter' | 'leave') {
if (typeof visitor[event] !== 'function') return;
if (!collection[event]) collection[event] = new Map<string, VisitorFn[]>();
@@ -22,6 +23,7 @@ function addVisitor(visitor: NodeVisitor, collection: VisitorCollection, nodeNam
collection[event].set(nodeName, visitors);
}
+/** Compile visitor actions from optimizer */
function collectVisitors(optimizer: Optimizer, htmlVisitors: VisitorCollection, cssVisitors: VisitorCollection, finalizers: Array<() => Promise<void>>) {
if (optimizer.visitors) {
if (optimizer.visitors.html) {
@@ -40,6 +42,7 @@ function collectVisitors(optimizer: Optimizer, htmlVisitors: VisitorCollection,
finalizers.push(optimizer.finalize);
}
+/** Utility for formatting visitors */
function createVisitorCollection() {
return {
enter: new Map<string, VisitorFn[]>(),
@@ -47,6 +50,7 @@ function createVisitorCollection() {
};
}
+/** Walk AST with collected visitors */
function walkAstWithVisitors(tmpl: TemplateNode, collection: VisitorCollection) {
walk(tmpl, {
enter(node, parent, key, index) {
@@ -68,6 +72,12 @@ function walkAstWithVisitors(tmpl: TemplateNode, collection: VisitorCollection)
});
}
+/**
+ * Optimize
+ * Step 2/3 in Astro SSR.
+ * Optimize is the point at which we mutate the AST before sending off to
+ * Codegen, and then to Snowpack. In some ways, it‘s a preprocessor.
+ */
export async function optimize(ast: Ast, opts: OptimizeOptions) {
const htmlVisitors = createVisitorCollection();
const cssVisitors = createVisitorCollection();