-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harminder Virk
authored and
Harminder Virk
committed
Aug 13, 2023
1 parent
3b25406
commit 936b3f0
Showing
5 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* edge.js | ||
* | ||
* (c) EdgeJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Edge } from '../edge/main.js' | ||
import { PluginFn } from '../types.js' | ||
|
||
/** | ||
* Hooks into the compiler phase of Edge and converts | ||
* tags to components. | ||
* | ||
* The components are discovered from the components directory | ||
* inside every registered disk. | ||
*/ | ||
class SuperChargedComponents { | ||
#edge: Edge | ||
#components: Record<string, string> = {} | ||
|
||
constructor(edge: Edge) { | ||
this.#edge = edge | ||
this.#claimTags() | ||
this.#transformTags() | ||
} | ||
|
||
/** | ||
* Refreshes the list of components | ||
*/ | ||
refreshComponents() { | ||
this.#components = this.#edge.loader | ||
.listComponents() | ||
.reduce<Record<string, string>>((result, { components }) => { | ||
components.forEach((component) => { | ||
result[component.tagName] = component.componentName | ||
}) | ||
return result | ||
}, {}) | ||
} | ||
|
||
/** | ||
* Registers hook to claim self processing of tags that | ||
* are references to components | ||
*/ | ||
#claimTags() { | ||
this.#edge.compiler.claimTag((name) => { | ||
if (this.#components[name]) { | ||
return { seekable: true, block: true } | ||
} | ||
return null | ||
}) | ||
|
||
this.#edge.asyncCompiler.claimTag((name) => { | ||
if (this.#components[name]) { | ||
return { seekable: true, block: true } | ||
} | ||
return null | ||
}) | ||
} | ||
|
||
/** | ||
* Transforms tags to component calls | ||
*/ | ||
#transformTags() { | ||
this.#edge.processor.process('tag', ({ tag }) => { | ||
const component = this.#components[tag.properties.name] | ||
if (!component) { | ||
return | ||
} | ||
|
||
tag.properties.name = 'component' | ||
if (tag.properties.jsArg.trim() === '') { | ||
tag.properties.jsArg = `'${component}'` | ||
} else { | ||
tag.properties.jsArg = `'${component}',${tag.properties.jsArg}` | ||
} | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* The superCharged plugin converts components stored within the | ||
* components directory of all the disk to Edge tags. | ||
*/ | ||
let superCharged: SuperChargedComponents | ||
export const pluginSuperCharged: PluginFn<{ recurring: boolean }> = (edge, firstRun) => { | ||
if (firstRun) { | ||
superCharged = new SuperChargedComponents(edge) | ||
} | ||
superCharged.refreshComponents() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters