All Projects → shlomiassaf → Ng Router Loader

shlomiassaf / Ng Router Loader

Licence: mit
Webpack loader for NgModule lazy loading using the angular router

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ng Router Loader

Angular Router Loader
A Webpack loader that enables string-based module loading with the Angular Router
Stars: ✭ 194 (+312.77%)
Mutual labels:  webpack, webpack-loader, router
Inline Style Loader
inline style loader for webpack
Stars: ✭ 16 (-65.96%)
Mutual labels:  webpack, webpack-loader
Wasm Loader
✨ WASM webpack loader
Stars: ✭ 604 (+1185.11%)
Mutual labels:  webpack, webpack-loader
Webvr Webpack Boilerplate
A webvr multi-scenes Single-page application for three.js, webpack
Stars: ✭ 47 (+0%)
Mutual labels:  webpack, router
Css Loader
CSS Loader
Stars: ✭ 4,067 (+8553.19%)
Mutual labels:  webpack, webpack-loader
Comlink Loader
Webpack loader to offload modules to Worker threads seamlessly using Comlink.
Stars: ✭ 535 (+1038.3%)
Mutual labels:  webpack, webpack-loader
Nunjucks Isomorphic Loader
Nunjucks loader for webpack, supporting both javascript templating and generating static HTML files through the HtmlWebpackPlugin.
Stars: ✭ 17 (-63.83%)
Mutual labels:  webpack, webpack-loader
Markdown Loader
markdown loader for webpack
Stars: ✭ 335 (+612.77%)
Mutual labels:  webpack, webpack-loader
Flow Bin Loader
webpack loader for Flow
Stars: ✭ 11 (-76.6%)
Mutual labels:  webpack, webpack-loader
Gt React Scaffold
🥚A boilerplate for client apps of webpack, react, redux, router...
Stars: ✭ 15 (-68.09%)
Mutual labels:  webpack, router
Thread Loader
Runs the following loaders in a worker pool
Stars: ✭ 945 (+1910.64%)
Mutual labels:  webpack, webpack-loader
Miox
Modern infrastructure of complex SPA
Stars: ✭ 374 (+695.74%)
Mutual labels:  webpack, router
Sass Loader
Compiles Sass to CSS
Stars: ✭ 3,718 (+7810.64%)
Mutual labels:  webpack, webpack-loader
React Redux Antdesign Webpack Starter
react + redux + ant design + react-router 4 + webpack 4 starter
Stars: ✭ 44 (-6.38%)
Mutual labels:  webpack, router
Blog.vue
☘ 一个vue的个人博客项目,配合.net core api教程,打造前后端分离
Stars: ✭ 362 (+670.21%)
Mutual labels:  webpack, router
Node Addon Loader
A loader for node native addons
Stars: ✭ 17 (-63.83%)
Mutual labels:  webpack, webpack-loader
Redux Json Router
Declarative, Redux-first routing for React/Redux browser applications.
Stars: ✭ 37 (-21.28%)
Mutual labels:  webpack, router
Vue Template Loader
Vue.js 2.0 template loader for webpack
Stars: ✭ 253 (+438.3%)
Mutual labels:  webpack, webpack-loader
Extract Loader
webpack loader to extract HTML and CSS from the bundle
Stars: ✭ 297 (+531.91%)
Mutual labels:  webpack, webpack-loader
Stylefmt Loader
Webpack-loader. Fixes stylelint issues automatically while bundling with Webpack.
Stars: ✭ 24 (-48.94%)
Mutual labels:  webpack, webpack-loader

Webpack loader for NgModule lazy loading using the angular router

Build Status GitHub version

Installation

npm install ng-router-loader --save-dev

OR

yarn add ng-router-loader --dev

V 2.0.0 BREAKING CHANGES:

Version 2.0.0 introduce support for the import() construct.
import() is not yet implemented in TypeScript. TypeScript does not ignore it but transpile it to something else which breaks the code.

To use the import() construct the loader must run AFTER the typescript transpilation process, this is after the awesome-typescript-loader in the example below.

Running after TS also means all code generators now emit ES5 code.

Webpack 1 users can't use async-import as it's not supported in version 1.
Webpack 2 users can use it as long as they are running on webpack > 2.1.0 beta28

Webpack integration

Add the ng-router-loader to your typescript loaders chain

Webpack 1

loaders: [
  {
    test: /\.ts$/,
    loaders: [
       'ng-router-loader',
      'awesome-typescript-loader'
    ]
  }
]

Webpack 2

module: {
  rules: [
    {
       test: /\.ts$/,
       use: [
       {
           loader: 'ng-router-loader' 
           options: {
            /* ng-router-loader options */
           }
        } ,
         'awesome-typescript-loader'          
       ]
    }
  ]
}

Lazy Loading

Use the loadChildren API with any webpack resolvable path to reference your lazy loaded angular module. Use # as a delimiter and write the NgModule class name.

import { Routes } from '@angular/router';

export const ROUTES: Routes = [
  { path: 'detail', loadChildren: () => '../my-ng-modules/details#DetailModule' },
];

The delimiter is configurable.

Query parameters (details#DetailModule?loader=sync) are added after the delimiter.
This behaviour might change, supporting both pre & after.

Synchronous Loading

For synchronous module loading, add the sync=true as a query string value to your loadChildren string. The module will be included in your bundle and not lazy-loaded.

import { Routes } from '@angular/router';

export const ROUTES: Routes = [
  { path: 'detail', loadChildren: () => '../my-ng-modules/details#DetailModule?loader=sync' },
];

The Synchronous example uses a resource specific loader option, you can also set a global loader option.

Configuration

Please read the documentation

In detph

@angular/router

The @angular/router provides an API for deferred NgModule loading, this is a simple API that accepts a function that returns an NgModule class.

Project structure

├── project-root/
│   ├── app
│   │   ├── app.routes.ts
│   ├── my-ng-modules
│   │   ├── details
│   │   │   ├──index.ts
│   │   │   ├──details.module.ts
│   │   │   ├──details.component.ts

DetailModule is defined in details.module.ts and exported in index.ts

app.routes.ts

import { Routes } from '@angular/router';
import { DetailModule } from '../my-ng-modules/details';

export const ROUTES: Routes = [
  { path: 'detail', loadChildren: () => DetailModule },
];

The @angular/router will not invoke the function until the path is active, this is the how lazy loading is done.

The loader

The example above works just fine but it includes a hard reference to the DetailModule. Having a reference results in adding the file containing the module into the bundle.

To achieve lazy loading we need to write the code in a lazy loading code-style that webpack understand.

ng-router-loader abstracts the complexity and provides an easy approach using a string reference. In the background the loader will translate the string to code.

The string reference is the reference you use when you require or import.
Any string that resolves with require or import can be used and the same rules apply with 1 addition, the string reference requires must provide the name of the NgModule exported.

Using the same example above:

app.routes.ts

import { Routes } from '@angular/router';

export const ROUTES: Routes = [
  { path: 'detail', loadChildren: () => '../my-ng-modules/details#DetailModule' },
];

It's that easy!

A word about the angular-router-loader

The angular-router-loader ("ARL" from now) came out with angular final when AOT was still blurry and not enough information was out there. This made it very limited in it's capabilities, while using it I reached some dead ends that ARL did'nt handle.

Another issue I had is that ARL forced me to structure my app in a certain way which was not webpack oriented. A loader should be transparent to the developer.

I started fixing things and quickly understood that a rewrite is required.

Here are some of the key points:

  • Module resolution
    ARL use the file system to resolve URIs, this makes it impossible to use the goodies webpack resolve provides, such as barrels, aliasing, custom module directories and more, see webpack resolve.
    ng-router-loader uses webpack to resolve modules so everything webpack resolves will work.

  • AOT re-exports
    ARL can't handle re-exported NgModule symbols in AOT mode.
    The example above shows the index.ts file exporting the DetailModule defined in a different file, this is a tricky scenario that requires symbol tracking and it will result in an unknown module import created by ARL ng-router-loader performs a deep metadata search to extract the right import.

  • Custom code generators ng-router-loader code generation is plugin based, you can provide a custom code generator that fits your use case.

  • Typescript based

TODO

  • Smart detection, use AST to detect ROUTE API.

Credits

angular-router-loader Learned a lot reading the code!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].