Skip to content

Instantly share code, notes, and snippets.

@paul58914080
Created July 22, 2019 10:36
Show Gist options
  • Save paul58914080/046a372cb4bf190d423104a449ebe5eb to your computer and use it in GitHub Desktop.
Save paul58914080/046a372cb4bf190d423104a449ebe5eb to your computer and use it in GitHub Desktop.
01.generate-app-view-todo.sh
ng g application view-todo --style=scss --prefix=view --viewEncapsulation=ShadowDom
# navigate to projects/view-todo/app
ng g module view-todo --flat=true
ng g component view-todo --module=view-todo --selector=view-todo --entryComponent=true --flat=true
ng g service view-todo --flat=true
@NgModule({
declarations: [HomeComponent],
imports: [
CommonModule,
ViewTodoModule
],
})
export class HomeModule {
}
<div class="container-fluid">
<create-todo></create-todo>
<view-todo></view-todo>
</div>
# make it a web component
@NgModule({
declarations: [ ViewTodoComponent ],
providers: [ ViewTodoService ],
exports: [ ViewTodoComponent ],
imports: [ CommonModule, BrowserModule, HttpClientModule ],
entryComponents: [ ViewTodoComponent ],
bootstrap: [ViewTodoComponent],
})
export class ViewTodoModule {
constructor(private injector: Injector) {
}
ngDoBootstrap() {
const viewTodoElement = createCustomElement(ViewTodoComponent, {injector: this.injector});
customElements.define('view-todo-element', viewTodoElement);
}
}
const fs = require('fs-extra');
const concat = require('concat');
const del = require('del');
(async function build() {
const files = [
'dist/view-todo/runtime-es2015.js',
'dist/view-todo/polyfills-es2015.js',
'dist/view-todo/main-es2015.js'
];
await del([‘dist/elements/*.js']);
await fs.ensureDir('dist/elements');
await concat(files, 'dist/elements/view-todo.js');
})();
"build:view-todo:elements": "ng build view-todo --prod --output-hashing none && node projects/view-todo/build/view-todo.js”
"micro-frontend-todo": {
"architect": {
"build": {
"options": {
"scripts": [
"dist/elements/view-todo.js"
]
}
}
}
}
<div class="todo-create-container">
<view-todo-element></view-todo-element>
<view-todo></view-todo>
</div>
@NgModule({
declarations: [HomeComponent],
imports: [CommonModule, ViewTodoAppModule],
bootstrap: [HomeComponent],
entryComponents: [HomeComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class HomeModule {
}
"view-todo": {
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "projects/view-todo/build/extra-webpack.config.js",
"mergeStrategies": {
"externals": "replace"
}
}
}
}
}
}
module.exports = {
output: {
jsonpFunction: 'webpackJsonpViewTodo',
library: 'viewTodo'
}
};
<!-- Feature toggle -->
<div class="container-fluid" style="width:100%">
<div *ngIf="!loadElements">
<view-todo></view-todo>
</div>
<div *ngIf="loadElements">
<view-todo-element></view-todo-element>
</div>
</div>
@import url(‘https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment