-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
6 changed files
with
250 additions
and
18 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,87 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import { loadModule } from './dom-utils.js'; | ||
|
||
export default class PluginsRegistry { | ||
#plugins; | ||
|
||
static parsePluginParams(id, config) { | ||
const pluginId = !config | ||
? id.split('/').splice(id.endsWith('/') ? -2 : -1, 1)[0].replace(/\.js/, '') | ||
: id; | ||
const pluginConfig = typeof config === 'string' || !config | ||
? { url: (config || id).replace(/\/$/, '') } | ||
: { load: 'eager', ...config }; | ||
pluginConfig.options ||= {}; | ||
return { id: pluginId, config: pluginConfig }; | ||
} | ||
|
||
constructor() { | ||
this.#plugins = new Map(); | ||
} | ||
|
||
// Register a new plugin | ||
add(id, config) { | ||
const { id: pluginId, config: pluginConfig } = PluginsRegistry.parsePluginParams(id, config); | ||
this.#plugins.set(pluginId, pluginConfig); | ||
} | ||
|
||
// Get the plugin | ||
get(id) { return this.#plugins.get(id); } | ||
|
||
// Check if the plugin exists | ||
includes(id) { return !!this.#plugins.has(id); } | ||
|
||
// Load all plugins that are referenced by URL, and updated their configuration with the | ||
// actual API they expose | ||
async load(phase, context) { | ||
[...this.#plugins.entries()] | ||
.filter(([, plugin]) => plugin.condition | ||
&& !plugin.condition(document, plugin.options, context)) | ||
.map(([id]) => this.#plugins.delete(id)); | ||
return Promise.all([...this.#plugins.entries()] | ||
// Filter plugins that don't match the execution conditions | ||
.filter(([, plugin]) => ( | ||
(!plugin.condition || plugin.condition(document, plugin.options, context)) | ||
&& phase === plugin.load && plugin.url | ||
)) | ||
.map(async ([key, plugin]) => { | ||
try { | ||
// If the plugin has a default export, it will be executed immediately | ||
const pluginApi = (await loadModule( | ||
key, | ||
!plugin.url.endsWith('.js') ? `${plugin.url}/${key}.js` : plugin.url, | ||
!plugin.url.endsWith('.js') ? `${plugin.url}/${key}.css` : null, | ||
document, | ||
plugin.options, | ||
context, | ||
)) || {}; | ||
this.#plugins.set(key, { ...plugin, ...pluginApi }); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Could not load specified plugin', key); | ||
} | ||
})); | ||
} | ||
|
||
// Run a specific phase in the plugin | ||
async run(phase, context) { | ||
return [...this.#plugins.values()] | ||
.reduce((promise, plugin) => ( // Using reduce to execute plugins sequencially | ||
plugin[phase] && (!plugin.condition | ||
|| plugin.condition(document, plugin.options, context)) | ||
? promise.then(() => plugin[phase](document, plugin.options, context)) | ||
: promise | ||
), Promise.resolve()); | ||
} | ||
} |
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,37 @@ | ||
/* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import { getMetadata } from './dom-utils.js'; | ||
import PluginsRegistry from './plugins-registry.js'; | ||
import { toClassName } from './utils.js'; | ||
|
||
export default class TemplatesRegistry { | ||
// Register a new template | ||
// eslint-disable-next-line class-methods-use-this | ||
add(id, url) { | ||
if (Array.isArray(id)) { | ||
id.forEach((i) => this.add(i)); | ||
return; | ||
} | ||
const { id: templateId, config: templateConfig } = PluginsRegistry.parsePluginParams(id, url); | ||
templateConfig.condition = () => toClassName(getMetadata('template')) === templateId; | ||
window.hlx.plugins.add(templateId, templateConfig); | ||
} | ||
|
||
// Get the template | ||
// eslint-disable-next-line class-methods-use-this | ||
get(id) { return window.hlx.plugins.get(id); } | ||
|
||
// Check if the template exists | ||
// eslint-disable-next-line class-methods-use-this | ||
includes(id) { return window.hlx.plugins.includes(id); } | ||
} |
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,51 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta property="foo:bar" content="baz"> | ||
<meta name="foo-qux" content="corge"> | ||
</head> | ||
<body> | ||
<script type="module"> | ||
/* eslint-env mocha */ | ||
import { runTests } from '@web/test-runner-mocha'; | ||
import { expect } from '@esm-bundle/chai'; | ||
import { getAllMetadata } from '../../src/dom-utils.js'; | ||
|
||
runTests(() => { | ||
it('get scoped properties', () => { | ||
expect(getAllMetadata('foo')).to.eql({ | ||
bar: 'baz', | ||
qux: 'corge', | ||
}); | ||
}); | ||
|
||
it('get properties for unknown scope', () => { | ||
expect(getAllMetadata('bar')).to.eql({}); | ||
}); | ||
|
||
// Test a custom document | ||
const testDoc = document.implementation.createHTMLDocument(); | ||
const titleMeta = testDoc.createElement('meta'); | ||
titleMeta.setAttribute('property', 'grault:bar'); | ||
titleMeta.setAttribute('content', 'baz'); | ||
testDoc.head.appendChild(titleMeta); | ||
|
||
const descriptionMeta = testDoc.createElement('meta'); | ||
descriptionMeta.setAttribute('name', 'grault-qux'); | ||
descriptionMeta.setAttribute('content', 'corge'); | ||
testDoc.head.appendChild(descriptionMeta); | ||
|
||
it('get scoped properties from custom document', () => { | ||
expect(getAllMetadata('grault', testDoc)).to.eql({ | ||
bar: 'baz', | ||
qux: 'corge', | ||
}); | ||
}); | ||
|
||
it('get properties for unknown scope from custom document', () => { | ||
expect(getAllMetadata('bar', testDoc)).to.eql({}); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |