This library is part of the Aurelia platform and contains a plugin capable of transforming Aurelia components into standards-compliant Web Components.
To keep up to date on Aurelia, please visit and subscribe to the official blog and our email list. We also invite you to follow us on twitter. If you have questions, please join our community on Gitter or use stack overflow. Documentation can be found in our developer hub.
This library can be used in the browser.
Here's an example configuration for main.js to show how to convert all global custom elements into web components:
import { CustomElementRegistry } from 'aurelia-web-components';
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.globalResources('resources/my-component');
aurelia
.start()
.then(() => {
const registry = aurelia.container.get(CustomElementRegistry);
//The following line takes all global resource custom elements and registers them as web components.
//default fallback prefix for custom elements is `au-`, set different
//registry.fallbackPrefix = 'au-';
//registry.forcePrefix = false; //whether all custom elements will be prefixed, false means that only those without hyphen ('-')
//Once the element is registered, in-page elements will begin rendering.
registry.useGlobalElements();
});
}
Warning: Note that calling
.useGlobalElements()
will remove all global elements from your global resources, including<router-view>
and<compose>
.
Alternatively, custom elements can also be registered on the fly:
import { InlineViewStrategy, useView } from 'aurelia-templating';
import { CustomElementRegistry } from 'aurelia-web-components';
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.globalResources('resources/my-component');
aurelia
.start()
.then(() => {
const registry = aurelia.container.get(CustomElementRegistry);
// the following register
return registry.register(class MyButton {
static $view = `<template>
<button>\${icon}<slot></slot></button>
</template>`
@bindable icon
});
})
// or
.then(() => {
const registry = aurelia.container.get(CustomElementRegistry);
// with useView path strategy & globalResource
@useView('path/to/view.html')
class MyCarousel {
// ...
}
aurelia.use.globalResources(MyCarousel);
// the following register
return Promise.all([
// with static view strategy
registry.register(class MyButton {
static $view = `<template>
<button>\${icon}<slot></slot></button>
</template>`
// ...
}),
// with a pre-defined view-model class that has already been registered using aurelia.globalResources(MyCarousel)
registry.register(MyCarousel)
]);
})
.then(() => {
aurelia.setRoot('app')
});
}
Note: This plugin requires that your browser have native support for the CustomElements v1 spec or that you have configured a v1 spec-compliant polyfill prior to calling registry methods.
Web components require the es6/es2015 constructor call type to be used rather than es5 or earlier function prototype-based calls.
In order to use this plugin with webpack, ensure the dist
configuration of the AureliaPlugin
is set to es2015
or later in webpack.config.js
module.exports = ({...} = {}) => ({
...
plugins: [
....
new AureliaPlugin({
dist: 'es2015'
}),
]
});
To build the code, follow these steps.
- Ensure that NodeJS is installed. This provides the platform on which the build tooling runs.
- From the project folder, execute the following command:
npm install
- To build the code, you can now run:
npm run build
- You will find the compiled code in the
dist
folder, available in module formats: UMD, AMD, CommonJS and ES6.
To run the unit tests, first ensure that you have followed the steps above in order to install all dependencies and successfully build the library. Once you have done that, proceed with these additional steps:
- You can now run the tests with this command:
npm test
- Each of custom element will be backed by a view model.
- For each view model class, a corresponding native custom element class will be created and defined, with the name derived from metadata and fallbacks to view model class name. If there is no hyphen
-
in the name of a custom element view model, a prefix (au-
by default) will be added to the name. This can be changed inCustomElementRegistry
instance. - Slot: By default, content projection is done using Aurelia slot emulation. This is to keep it consistent with the rest of Aurelia ecosystem. To use native slot/shadow dom for content projection, decorate view model class with
@useShadowDOM
. - To comply with Custom element v1 specs, the element created by by Aurelia when calling
document.createElement
is empty until an attribute is modified or the element is added to a document. Specifically, the child elements are not created until theconnectedCallback
orattributeChangedCallback
hooks are triggered.