Skip to content

Instantly share code, notes, and snippets.

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