Creating a basic Angular Web Component. Check out the Github repository that showcases the process here:
https://github.com/fireflysemantics/fs-gist
The component code can be found here: https://github.com/fireflysemantics/fs-gist/blob/master/src/app/fs-gist/fs-gist.component.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'fs-gist',
template: `
<p>
fs-gist works!
</p>
`,
styles: [
],
encapsulation: ViewEncapsulation.Native
})
export class FsGistComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
Here is the module code: https://github.com/fireflysemantics/fs-gist/blob/master/src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FsGistComponent } from './fs-gist/fs-gist.component';
import { Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
@NgModule({
declarations: [
FsGistComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [FsGistComponent]
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const el = createCustomElement(
FsGistComponent,
{ injector: this.injector });
customElements.define('fs-gist', el);
}
}
Explore the build scripts in package.json
:
https://github.com/fireflysemantics/fs-gist/blob/master/package.json
"b": "ng build --prod --output-hashing=none",
"c": "bash -c 'cat dist/fs-gist/{runtime-es5,polyfills-es5,main-es5}.js > fs-gist.js'",
"c1": "bash -c 'cat dist/fs-gist/{runtime-es2015,polyfills-es2015,main-es2015}.js > fs-gist2015.js'",
"p": "npm run b && npm run c",
"p1": "npm run b && npm run c1",
"cp-fs-gist": "cp fs-gist.js ../wctest/src/assets/js/"
Executing npm run p
will generate the web component fs-gist
in the root folder.
This component was then copied into the src/assets/js/
folder of another test project:
https://github.com/fireflysemantics/wc-test
Upon loading the application, an error occurs as seen in the console logs:
fs-gist.js:1 ERROR TypeError: i.createShadowRoot is not a function at new n (fs-gist.js:1) at e.value (fs-gist.js:1) at fs-gist.js:1 at n.value (fs-gist.js:1) at e.value (fs-gist.js:1) at e.value (fs-gist.js:1) at HTMLElement.value (fs-gist.js:1) at ZoneDelegate.invoke (zone-evergreen.js:364) at Object.onInvoke (fs-gist.js:1) at ZoneDelegate.invoke (zone-evergreen.js:363)
To address this issue, polyfills for web components are included in polyfills.js
:
import "@webcomponents/custom-elements/src/native-shim";
import "@webcomponents/custom-elements/custom-elements.min";
import '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js'
Additionally, web component loading has been added to index.html
:
<script src="webcomponents/webcomponents-loader.js"></script>
<script>
if ( !window.customElements )
{ document.write('<!--') }
</script>
<script src="webcomponents/custom-elements-es5-adapter.js"></script>
<!-- ! KEEP ME -->
Looking for ideas or solutions to this problem.
In an attempt to simplify things, a minimal environment using the fs-gist
web component was created in StackBlitz. The demo imports the web component polyfill from a CDN:
https://stackblitz.com/edit/js-custome-element-test
However, the following error surfaced:
ERROR Error: Failed to execute 'define' on 'CustomElementRegistry': the name "fs-gist" has already been used with this registry
Bug Report
A potential bug is suspected in the Angular compilation process of the web component.