• Angular 6 HMR 热加载配置


    什么是 HMR

    ​ HMR 是hot module replacement 的简称,直译:热模块替换,如果不开启HMR模式,angular项目在模块更改的时候会从根节点开始刷新,开启HMR模式以后,只会刷新有修改的地方,开发效率在某种意义上可以提高

    配置步骤

    配置前提:你已经创建了一个angular6项目,安装hmr依赖

    npm install --save-dev @angularclass/hmr

    1)在 src/environments 目录下添加 environment.hmr.ts 配置文件,文件内容为:

    export const environment = {
    production: false,
    hmr: true
    };

    2)然后分别在 environment.prod.ts 和 environment.ts 添加 hmr:false 配置项

    3)配置 src/tsconfig.app.json 文件 ,内容为:

    "compilerOptions": {
    ...
    "types": ["node"]
    }

    4)配置 src/hmr.ts 文件内容如下 :

    import { NgModuleRef, ApplicationRef } from "@angular/core";
    import { createNewHosts } from "@angularclass/hmr";
    
    export const hmrBootstrap = (
    module: any,
    bootstrap: () => Promise<NgModuleRef<any>>
    ) => {
    let ngModule: NgModuleRef<any>;
    module.hot.accept();
    bootstrap().then(mod => (ngModule = mod));
    module.hot.dispose(() => {
    const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef);
    const elements = appRef.components.map(c => c.location.nativeElement);
    const makeVisible = createNewHosts(elements);
    ngModule.destroy();
    makeVisible();
    });
    };

    5)更新 src/main.ts 文件内容如下

    import { enableProdMode } from "@angular/core";
    import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
    
    import { AppModule } from "./app/app.module";
    import { environment } from "./environments/environment";
    
    import { hmrBootstrap } from "./hmr";
    
    if (environment.production) {
    enableProdMode();
    }
    
    const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);
    
    if (environment.hmr) {
    if (module["hot"]) {
    hmrBootstrap(module, bootstrap);
    } else {
    console.error("HMR is not enabled for webpack-dev-server!");
    console.log("Are you using the --hmr flag for ng serve?");
    }
    } else {
    bootstrap().catch(err => console.log(err));
    }

    6)配置 angular.json 文件 :

    "build": {
    "builder": "@angular-devkit/build-angular:browser",
    "options": {
    "outputPath": "dist/${project-name}",
    "index": "src/index.html",
    "main": "src/main.ts",
    "polyfills": "src/polyfills.ts",
    "tsConfig": "src/tsconfig.app.json",
    "assets": ["src/favicon.ico", "src/assets"],
    "styles": ["src/styles.css"],
    "scripts": []
    },
    "configurations": {
    
    
    
    "hmr": {
    "fileReplacements": [
    {
    "replace": "src/environments/environment.ts",
    "with": "src/environments/environment.hmr.ts"
    }
    ]
    },
    
    
    
    "production": {
    "fileReplacements": [
    {
    "replace": "src/environments/environment.ts",
    "with": "src/environments/environment.prod.ts"
    }
    ],
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true
    }
    }
    },
    ...
    "serve": {
    "builder": "@angular-devkit/build-angular:dev-server",
    "options": {
    "browserTarget": "${project-name}:build"
    },
    "configurations": {
    "production": {
    "browserTarget": "${project-name}:build:production"
    },
    
    
    
    "hmr": {
    "browserTarget": "${project-name}:build:hmr",
    "hmr": true
    }
    
    
    
    }
    },

    7)启动方式:两种

    第一种:

    ng serve --configuration hmr

    第二种:

    ng run ${project-name}:serve:hmr

    8)为方便一般添加在package.json的scripts里:

    "scripts": {
    ...
    "hmr": "ng serve --configuration hmr --open" 
    }
  • 相关阅读:
    232. Implement Queue using Stacks
    231. Power of Two
    n&(n-1)位运算的妙用
    230. Kth Smallest Element in a BST
    关于UNIX的exec函数
    Go语言的各种Print函数
    Go语言的接口interface、struct和组合、继承
    Go语言知识点笔记
    Ubuntu自定义终端窗口位置
    Python的类变量和成员变量、类静态方法和类成员方法
  • 原文地址:https://www.cnblogs.com/sghy/p/9814487.html
Copyright © 2020-2023  润新知