From 782215535e0cbc3dbb0aa82eeb2953f503754328 Mon Sep 17 00:00:00 2001 From: tomastrajan Date: Thu, 11 Jul 2024 23:03:23 +0200 Subject: [PATCH] feat(lib): add provideAxLazyElements() & provideAxLazyElementsConfigs() (use without NgModules) - library can now be used without NgModules --- .../app/features/docs/faq/faq.component.ts | 2 +- .../examples/advanced/advanced.component.ts | 10 +- .../examples/advanced/advanced.routes.ts | 72 ++++++------ .../examples/basic/basic.component.html | 4 +- .../examples/basic/basic.component.ts | 13 ++- .../examples/dynamic/dynamic.component.html | 2 +- .../examples/dynamic/dynamic.component.ts | 14 +-- .../examples/dynamic/dynamic.routes.ts | 27 +++-- .../examples/testing/testing.component.ts | 4 +- .../src/app/features/home/home.component.html | 10 +- .../src/app/features/home/home.component.ts | 5 +- projects/elements-demo/src/main.ts | 14 +-- .../lazy-elements/lazy-elements.interface.ts | 6 + .../lazy-elements.module.spec.ts | 2 +- .../lib/lazy-elements/lazy-elements.module.ts | 2 +- .../lazy-elements.providers.spec.ts | 81 +++++++++++++ .../lazy-elements/lazy-elements.providers.ts | 106 ++++++++++++++++++ .../src/lib/lazy-elements/public_api.ts | 1 + 18 files changed, 285 insertions(+), 90 deletions(-) create mode 100644 projects/elements/src/lib/lazy-elements/lazy-elements.providers.spec.ts create mode 100644 projects/elements/src/lib/lazy-elements/lazy-elements.providers.ts diff --git a/projects/elements-demo/src/app/features/docs/faq/faq.component.ts b/projects/elements-demo/src/app/features/docs/faq/faq.component.ts index 466ca50..47a3cf2 100644 --- a/projects/elements-demo/src/app/features/docs/faq/faq.component.ts +++ b/projects/elements-demo/src/app/features/docs/faq/faq.component.ts @@ -46,7 +46,7 @@ const FAQ = [ question: 'When do we need to use *axLazyElement="module: true" flag?', answer: - 'Some web components libraries might be distributed using ES modules, for example if we look at @material/mwc-button we will see that it imports additional things like @material/mwc-base using import statement in its implementation. This means it will need to download multiple files compared to a single bundle when using Angular elements. To make this work, library has to generate \t<script type="module"> instead of just plain \t<script> tag.', + 'Some web components libraries might be distributed using ES modules, for example if we look at @material/mwc-button we will see that it imports additional things like @material/mwc-base using import statement in its implementation. This means it will need to download multiple files compared to a single bundle when using Angular elements. To make this work, library has to generate \t<script type="module"> instead of just plain \t<script> tag.', }, { question: 'Angular elements / web components / custom elements / what?', diff --git a/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts b/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts index 104ea86..866bea4 100644 --- a/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts +++ b/projects/elements-demo/src/app/features/examples/advanced/advanced.component.ts @@ -7,8 +7,8 @@ import { import { NgIf } from '@angular/common'; import { RouterLink } from '@angular/router'; import { + LazyElementDirective, LazyElementsLoaderService, - LazyElementsModule, } from '@angular-extensions/elements'; import { MatIconModule } from '@angular/material/icon'; import { MatButtonModule } from '@angular/material/button'; @@ -25,7 +25,7 @@ import { HighlightModule } from 'ngx-highlightjs'; MatIconModule, MatButtonModule, HighlightModule, - LazyElementsModule, + LazyElementDirective, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], }) @@ -110,7 +110,7 @@ const options: LazyElementModuleOptions = { elementConfigs: [ { tag: 'mwc-checkbox', - url: 'https://unpkg.com/@material/mwc-checkbox@0.18.0/mwc-checkbox.js?module' + url: 'https://unpkg.com/@material/mwc-checkbox@0.27.0/mwc-checkbox.js?module' } ] }; @@ -133,7 +133,7 @@ const options: LazyElementModuleOptions = { elementConfigs: [ { tag: 'mwc-switch', - url: 'https://unpkg.com/@material/mwc-switch@0.18.0/mwc-switch.js?module', + url: 'https://unpkg.com/@material/mwc-switch@0.27.0/mwc-switch.js?module', isModule: true } ] @@ -165,7 +165,7 @@ const options: LazyElementModuleRootOptions = { elementConfigs: [ { tag: 'mwc-switch', - url: 'https://unpkg.com/@material/mwc-switch@0.18.0/mwc-switch.js?module' + url: 'https://unpkg.com/@material/mwc-switch@0.27.0/mwc-switch.js?module' } ] }; diff --git a/projects/elements-demo/src/app/features/examples/advanced/advanced.routes.ts b/projects/elements-demo/src/app/features/examples/advanced/advanced.routes.ts index 405c888..7b582f3 100644 --- a/projects/elements-demo/src/app/features/examples/advanced/advanced.routes.ts +++ b/projects/elements-demo/src/app/features/examples/advanced/advanced.routes.ts @@ -1,10 +1,8 @@ -import { importProvidersFrom } from '@angular/core'; import { Routes } from '@angular/router'; import { ElementConfig, LAZY_ELEMENT_CONFIGS, - LazyElementModuleOptions, - LazyElementsModule, + provideAxLazyElementsConfigs, } from '@angular-extensions/elements'; import { AdvancedComponent } from './advanced.component'; @@ -29,48 +27,46 @@ export function elementConfigsFactory(): ElementConfig[] { ]; } -const options: LazyElementModuleOptions = { - elementConfigs: [ - { - tag: 'wired-button', - url: 'https://unpkg.com/wired-elements@1.0.0/dist/wired-elements.bundled.js', - loadingComponent: SpinnerComponent, - errorComponent: ErrorComponent, - preload: true, - }, - { - tag: 'mwc-switch', - url: 'https://unpkg.com/@material/mwc-switch@0.18.0/mwc-switch.js?module', - isModule: true, - }, - { - tag: 'mwc-checkbox', - url: 'https://unpkg.com/@material/mwc-checkbox@0.18.0/mwc-checkbox.js?module', - isModule: true, - }, - { - tag: 'mwc-fab', - url: 'https://unpkg.com/@material/mwc-fab@0.18.0/mwc-fab.js?module', - isModule: true, - loadingComponent: SpinnerComponent, - }, - { - tag: 'mwc-slider', - url: 'https://unpkg.com/@material/mwc-slider@0.14.1/mwc-slider.js?module', - isModule: true, - hooks: { - beforeLoad: beforeLoadHook, - }, +const configs: ElementConfig[] = [ + { + tag: 'wired-button', + url: 'https://unpkg.com/wired-elements@1.0.0/dist/wired-elements.bundled.js', + loadingComponent: SpinnerComponent, + errorComponent: ErrorComponent, + preload: true, + }, + { + tag: 'mwc-switch', + url: 'https://unpkg.com/@material/mwc-switch@0.27.0/mwc-switch.js?module', + isModule: true, + }, + { + tag: 'mwc-checkbox', + url: 'https://unpkg.com/@material/mwc-checkbox@0.27.0/mwc-checkbox.js?module', + isModule: true, + }, + { + tag: 'mwc-fab', + url: 'https://unpkg.com/@material/mwc-fab@0.27.0/mwc-fab.js?module', + isModule: true, + loadingComponent: SpinnerComponent, + }, + { + tag: 'mwc-slider', + url: 'https://unpkg.com/@material/mwc-slider@0.14.1/mwc-slider.js?module', + isModule: true, + hooks: { + beforeLoad: beforeLoadHook, }, - ], -}; + }, +]; export default [ { path: '', component: AdvancedComponent, providers: [ - importProvidersFrom(LazyElementsModule.forFeature(options)), + provideAxLazyElementsConfigs(configs), { provide: LAZY_ELEMENT_CONFIGS, useFactory: elementConfigsFactory, diff --git a/projects/elements-demo/src/app/features/examples/basic/basic.component.html b/projects/elements-demo/src/app/features/examples/basic/basic.component.html index d5342c9..2fac876 100644 --- a/projects/elements-demo/src/app/features/examples/basic/basic.component.html +++ b/projects/elements-demo/src/app/features/examples/basic/basic.component.html @@ -26,7 +26,7 @@

Basic usage

@@ -58,7 +58,7 @@

Loading... +const CODE_EXAMPLE_1 = ` favorite `; -const CODE_EXAMPLE_2 = `; +const CODE_EXAMPLE_2 = `; Loading... @@ -82,7 +83,7 @@ const CODE_EXAMPLE_4 = `; `; -const CODE_EXAMPLE_5 = ` +const CODE_EXAMPLE_5 = ` `; const CODE_EXAMPLE_5_IMPORT_MAP = ` @@ -90,7 +91,7 @@ const CODE_EXAMPLE_5_IMPORT_MAP = ` `; diff --git a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html index 3b514e6..8ee4e31 100644 --- a/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html +++ b/projects/elements-demo/src/app/features/examples/dynamic/dynamic.component.html @@ -41,7 +41,7 @@

Dynamic element tag

; +export const CODE_EXAMPLE_1 = `; @@ -140,7 +140,7 @@ export const CODE_EXAMPLE_3_HTML = `