Skip to content

Instantly share code, notes, and snippets.

@paul58914080
Last active March 13, 2020 03:16
Show Gist options
  • Save paul58914080/22bfaf7e1f90fda0ec3f63bf7c905201 to your computer and use it in GitHub Desktop.
Save paul58914080/22bfaf7e1f90fda0ec3f63bf7c905201 to your computer and use it in GitHub Desktop.
view-todo-micro-app

$ ng g application view-todo --style=scss --prefix=view --viewEncapsulation=ShadowDom --routing=false

Only delete the content inside projects/view-todo/app and not the app directory

$ ng g module view-todo --flat=true --project=view-todo

$ ng g component view-todo --module=view-todo --selector=view-todo --entryComponent=true --flat=true --project=create-todo

$ ng g service view-todo --project=view-todo --flat=true --project=create-todo

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {HomeComponent} from './home.component';
import {ViewTodoModule} from '../../../projects/view-todo/src/app/view-todo.module';
@NgModule({
declarations: [ HomeComponent ],
imports: [
CommonModule,
ViewTodoModule
]
})
export class HomeModule {
}
<div class="container-fluid">
<view-todo></view-todo>
</div>

$ ng add @angular/elements --project=view-todo

import {Injector, NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { ViewTodoComponent } from './view-todo.component';
import {BrowserModule} from '@angular/platform-browser';
import {createCustomElement} from '@angular/elements';
@NgModule({
declarations: [ViewTodoComponent],
imports: [
CommonModule,
BrowserModule
],
entryComponents: [ViewTodoComponent],
bootstrap: [ViewTodoComponent],
exports: [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/view-todo.js']);
await fs.ensureDir('dist/elements');
await concat(files, 'dist/elements/view-todo.js');
})();
export const environment = {
production: false,
loadBootstrap: true
};
export const environment = {
production: true,
loadBootstrap: false
};
import {Injector, NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ViewTodoComponent} from './view-todo.component';
import {BrowserModule} from '@angular/platform-browser';
import {createCustomElement} from '@angular/elements';
import {environment} from '../environments/environment';
@NgModule({
declarations: [ ViewTodoComponent ],
imports: [
CommonModule,
BrowserModule
],
entryComponents: [ ViewTodoComponent ],
bootstrap: environment.loadBootstrap ? [ ViewTodoComponent ] : [],
exports: [ ViewTodoComponent ]
})
export class ViewTodoModule {
constructor(private injector: Injector) {
}
ngDoBootstrap() {
const viewTodoElement = createCustomElement(ViewTodoComponent, { injector: this.injector });
customElements.define('view-todo-element', viewTodoElement);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Web component</title>
</head>
<body>
<view-todo-element></view-todo-element>
<script type='application/javascript' src='view-todo.js'></script>
</body>
</html>

$ npm i --save @angular-builders/custom-webpack

"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'
}
};
"micro-frontend-todo": {
"architect": {
"build": {
"options": {
"scripts": [
{
"input": "node_modules/document-register-element/build/document-register-element.js"
},
"dist/elements/view-todo.js"
]
}
}
}
}
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'todo-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
loadElement = false;
constructor() { }
ngOnInit() {
}
}
@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");
@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