GithubHelp home page GithubHelp logo

meeroslav / ngx-build-plus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from manfredsteyer/ngx-build-plus

0.0 2.0 0.0 4.18 MB

Extend the Angular CLI's default build behavior without ejecting, e. g. for Angular Elements

TypeScript 100.00%

ngx-build-plus's Introduction

ngx-build-plus

Extend the Angular CLI's default build behavior without ejecting:

  • ๐Ÿ“ฆ Build a single bundle (e. g. for Angular Elements)
  • ๐Ÿ“„ Extend the default behavior by providing a partial config that just contains your additional settings
  • โ˜‘๏ธ Inherits from the default builder, hence you have the same options
  • ๐Ÿฐ Simple to use
  • โ๏ธ No eject needed

Useful not only for Angular Elements

The original use case for this was to create a bundle for Angular Elements by extending the CLI's default builder. Besides this,ngx-build-plus is also usable when you want to enhance other build setups with a partial webpack config.

It allows you to provide a single bundle that can be distributed easily.

Using an partial webpack config, you could e. g. define packages as externals. They can be loaded separately into the shell (hosting application). This allows several individually loaded Custom Elements sharing common packages like @angular/core etc.

Credits

Big thanks to Rob Wormald and David Herges!

Tested with CLI 6.0.0

This package has been created and tested with Angular CLI 6.0.0. If the CLI's underlying API changes in future, I'll provide an respective update for this version too until the CLI has build-in features for the covered use cases.

Example

https://github.com/manfredsteyer/ngx-build-plus

Usage

The next steps guides you trough getting started with ngx-build-plus by an example that uses Angular Elements. The result of this description can be found in the repository's sample directory.

  1. Create a new Angular CLI based project and install @angular/elements as well as @webcomponents/custom-elements which provides needed polyfills:

    ng add @angular/elements 
    npm install @webcomponents/custom-elements --save
    
  2. Expose a component as an Custom Element:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule, Injector } from '@angular/core';
    import { createCustomElement } from '@angular/elements';
    
    import { AppComponent } from './app.component';
    
    @NgModule({
        imports: [
            BrowserModule
        ],
        declarations: [
            AppComponent
        ],
        providers: [],
        bootstrap: [],
        entryComponents:[AppComponent]
    })
    export class AppModule { 
    
        constructor(private injector: Injector) {
        }
    
        ngDoBootstrap() {
            const elm = createCustomElement(AppComponent, { injector: this.injector });
            customElements.define('custom-element', elm);
        }
    
    }
  3. Install ngx-build-plus:

    npm install ngx-build-plus --save-dev
    
  4. Open your angular.json and tell the CLI to use ngx-builder-plus instead of the default one:

      [...]
      "architect": {
        "build": {
          "builder": "ngx-build-plus:build",
          [...]
        }
      }
      [...]
    
  5. Create a file webpack.extra.js with a partial webpack config that tells webpack to exclude packages like @angular/core:

    module.exports = {
        "externals": {
            "rxjs": "rxjs",
            "@angular/core": "ng.core",
            "@angular/common": "ng.common",
            "@angular/platform-browser": "ng.platformBrowser",
            "@angular/elements": "ng.elements"
        }
    }
  6. Build your application:

    ng build --prod --extraWebpackConfig webpack.extra.js --output-hashing none
    
  7. You will see that just one bundle (besides the script.js that could also be shared) is built. The size of the main.js tells you, that the mentioned packages have been excluded.

    Result

    Hint: If you don't want to get just one bundle you can set the new flag single-bundle to false (ng build ... --single-bundle false)

  8. Copy the bundle into a project that references the UMD versions of all external libraries and your main.ts. You can find such a project with all the necessary script files in the deploy folder of the sample.

    <html>
    <body>
        <!-- Polyfills for Browsers supporting 
            Custom Elements. Needed b/c we downlevel
            to ES5. See: @webcomponents/custom-elements
        -->
        <script src="./native-shim.js"></script>
    
        <!-- Polyfills for Browsers not supporting
            Custom Elements. See: @webcomponents/custom-elements
        -->
        <script src="./custom-elements.min.js"></script>
        
        <!-- Zone.js -->
        <!-- 
        Consider excluding zone.js when creating
        Custom Elements by using the noop zone.
        -->
        <script src="./zone.js"></script>
        
        <!-- Rx -->
        <script src="./rxjs.umd.js"></script>
    
        <!-- Angular Packages -->
        <script src="./core.umd.js"></script>
        <script src="./common.umd.js"></script>
        <script src="./platform-browser.umd.js"></script>
        <script src="./elements.umd.js"></script>
    
        <!-- Custom Element -->
        <script src="./custom-element.bundle.js"></script>
    
        <!-- Calling Custom Element -->
        <custom-element></custom-element>
    
        <!-- 
        Here we could call additional custom elements
        reusing the loaded packages above.
        -->
    
    </body>
    </html>
  9. Test your solution.

Hint: For production, consider using the minified versions of those bundles. They can be found in the node_modules folder after npm installing them.

ngx-build-plus's People

Contributors

manfredsteyer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.