-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: a (revised) minimal plugin and template system #374
Draft
ramboz
wants to merge
29
commits into
adobe:main
Choose a base branch
from
ramboz:plugin-system2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
|
ramboz
changed the title
Plugin system2
feat: a (revised) minimal plugin and template system
Jun 24, 2024
Test URL: https://update-lib-aem-minor-2-3-0--aem-boilerplate--adobe.hlx.live/ Release Notes: https://github.com/adobe/aem-lib/releases/tag/v2.3.0 Co-authored-by: semantic-release-bot <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…#380) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…dobe#373) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Test URL: https://update-lib-aem-minor-2-5-0--aem-boilerplate--adobe.hlx.live/ Release Notes: https://github.com/adobe/aem-lib/releases/tag/v2.5.0 Co-authored-by: semantic-release-bot <[email protected]>
ramboz
commented
Jul 23, 2024
*/ | ||
function setup() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved further down in the file.
ramboz
commented
Jul 23, 2024
@@ -541,29 +559,25 @@ async function loadBlock(block) { | |||
block.dataset.blockStatus = 'loading'; | |||
const { blockName } = block.dataset; | |||
try { | |||
const cssLoaded = loadCSS(`${window.hlx.codeBasePath}/blocks/${blockName}/${blockName}.css`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted above in loadModule
so we have a generic logic to use also to load the plugins
10 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✏️ This is a revised version of #254 that relies more on custom DOM events and reduces the custom JS APIs a bit.
Use case
As a developer, I want to be able to easily organize my code into different files, and execute some of the logic in each phase of the page load as needed.
In particular, I want a way to:
templates
that have some JS logicplugins
for more advanced features like experimentation, conversion tracking, tag management, etc.templates
andplugins
only when they are neededplugin
is loaded to avoid negatively impacting performances if it is not needed immediatelyTechnical Requirements
plugins
andtemplates
should use the same logic to reduce code duplicationplugins
andtemplates
should re-use the block loading approach to load both JS and CSS files, if applicablewindow.hlx.*
namespaceProposal
Introduce a new custom event dispatching logic with the capability to await its listeners so we can have blocking logic if needed (i.e. experiments need to replace the content before the page continues rendering). Event handlers can call the
ev.await(…)
method with a promise.Introduce new page lifecycle events:
aem:section:loaded
: triggered when the section is shownaem:block:config
: triggered before we load the block js/css so the path may be altered if neededaem:block:decorated
: triggered right after the decoration is done, but before block is shown in case we need additional DOM patchingaem:block:loaded
: triggered when the block is shownaem:eager
/aem:lazy
/aem:delayed
: each dispatched at the start of the respective phaseIntroduce 2 new namespaces on
window.hlx.*
:window.hlx.templates
add(id, url)
: register a new templateget(id)
: get the templatehas(id)
: check if a template existswindow.hlx.plugins
add(id, config)
: add a new pluginget(id)
: get the pluginhas(id)
: check if a plugin existsPlugins and templates would follow the same API:
export default function init(document, options)
: logic executed immediately when the plugin/template is loadeddocument.addEventListener('aem:eager', (ev) => { … })
: logic executed in theeager
phasedocument.addEventListener('aem:lazy', (ev) => { … })
: logic executed in thelazy
phasedocument.addEventListener('aem:delayed', (ev) => { … })
: logic executed in thedelayed
phaseExtracting a new
loadModule(name, cssPath, jsPath, args)
method that both theloadBlock
and the new plugin system use.Usage
For ease of use, we export 2 convenience methods in
aem.js
, namelywithPlugin
&withTemplate
.Adding Templates
Add a new template to the project:
or the shorthand version:
or the bulk version:
or the the module approach that loads both JS and CSS:
Adding Plugins
Add a new inline plugin to the project:
Add a regular plugin to the project that will always load (no
condition
):or the module approach that loads both JS and CSS:
or the shorthand version:
All plugins load by default in the lazy phase, to offer best performance out of the box. If a plugin needs to load in another phase, this can be done via:
Plugin Template
/plugins/qux.js
Examples
A barebone demo site built for this PR:
Test URLs: