美文网首页Angular勇攀高峰
Angular6 配置 hmr (热加载)

Angular6 配置 hmr (热加载)

作者: niccky | 来源:发表于2018-08-24 23:31 被阅读0次

前置条件

  • 先安装项目依赖包 yarn ( 推荐使用 yarn 包管理工具 )
  • 安装 hmr 依赖包 yarn add @angularclass/hmr --dev

1. 添加 environment.hmr.ts 配置文件

src/app/environments 目录下添加 environment.hmr.ts,内容如下:

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

同时,修改 environment.prod.tsenvironment.ts 文件为如下内容:

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

2. 添加 hmr.ts 文件

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();
  });
};

3. 修改 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));
}

4. 配置 angular.json 文件

修改根目录下的 angular.json 文件

"build": {
  "configurations": {
    ...
    "hmr": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.hmr.ts"
        }
      ]
    }
  }
},
...
"serve": {
  "configurations": {
    ...
    "hmr": {
      "hmr": true,
      "browserTarget": "<project-name>:build:hmr"
    }
  }
}

5. 配置 tsconfig.json 文件

这个选项对高版本的 typescript 才有用

"types": ["node"]

旧版本的 typescript 使用ts的声明文件,如:hot.d.ts

declare interface NodeModule {
  hot: any;
}
declare var module: NodeModule;

如果以上两个都没配置,启动应用编译的时候会报错:

ERROR in src/main.ts(16,7): error TS2304: Cannot find name 'module'.
src/main.ts(17,18): error TS2304: Cannot find name 'module'.

6. 启动应用

方式一:
ng serve --configuration hmr

方式二:
ng run ng6:serve:hmr

方式三:

"scripts": {
  ...
  "hmr": "ng serve --configuration hmr"
}

npm run hmr

有的时候启动应用编译的时候会卡在如下位置:

95%emitting LicenseWebpackPlugin

解决方案:

  • 使用 yarn 重新安装依赖包

如果使用 yarn 安装依赖包出现如下信息

info There appears to be trouble with your network connection. Retrying...

解决方案:

  • 需配置 registry
  • 操作 yarn config set registry https://registry.npm.taobao.org/,之后再次执行 yarn 安装依赖包

天之骄子 2018.8.24 星期五 深圳

Angular 修仙QQ群号【648681235】

相关文章

  • Angular6 配置 hmr (热加载)

    前置条件 先安装项目依赖包 yarn ( 推荐使用 yarn 包管理工具 ) 安装 hmr 依赖包 yarn ad...

  • 我对“热加载”的理解

    热加载又名“模块热替换(HMR - Hot Module Replacement)”。是一种不需要重新加载页面,就...

  • Parcel —— 零配置的模块打包工具

    目录 Parcel概述Parcel的背景Parcel的特点 快速上手HMR —— 模块热替换零配置自动安装依赖支持...

  • webpack之热更新/替换

    模块热替换(HMR)什么是模块热替换HMR(Hot Module Replacement),在应用程序运行过程中,...

  • webpack-HMR自动化更新配置

    HMR 就是在devserver重配置了 hot:true (热模块更新) 问题:遇到的坑,当你有很多文件的时候,...

  • 使用webpack和React实现热替换

    模块热替换(HMR) 概念 在应用程序运行过程中替换、添加或删除模块,而无需重新加载整个页面 也是用在开发环境中加...

  • webpcak HMR原理

    模块热替换(hot module replacement) 模块热替换(HMR - Hot Module Repl...

  • 大白话讲webpack热更新

    webpack热更新,也称热替换,英文缩写HMR(Hot Module Replacement)。ok,介绍完定义...

  • 【webpack】HMR 热替换

    概述 之前呢我们已经简单了解了webpack-dev-server的一些基本用法和特性,那他主要就是为我们使用we...

  • webpack HMR热更新

    1. 介绍 HMR(Hot Module Replacement):模块热替换,也叫模块热更新,解决自动刷新导致...

网友评论

    本文标题:Angular6 配置 hmr (热加载)

    本文链接:https://www.haomeiwen.com/subject/cacwiftx.html