enten / angular-universal

Angular Universal 100% powered by Angular CLI
153 stars 36 forks source link

[HMR] Error: Cannot find module '@angular/material/toolbar.ngfactory' #17

Closed bobsingor closed 5 years ago

bobsingor commented 6 years ago

I am running npm run dev

When I edit a file i get the error: [HMR] Cannot apply update. [HMR] Error: Cannot find module '@angular/material/toolbar.ngfactory'

After this I need to manually restart the server on serverside changes i'm making.

Hope someone can help me out.

Thanks

enten commented 6 years ago

UPDATE: check next comment


I investigated this issue and unfortunately, I don't find a maintenable way to achieve HMR with @angular/material.

To summarize this issue: hmr doesn't work with angular-devkit server builder.

The issue triggered when webpack/hot/poll.js try to require hmr update from @angular/material/*.ngFactory.

It's due to design of @angular/material package exports,

I found a solution by include @material/material components in tsconfig.server.ts:

{
  "extends": "./tsconfig.app.json",
  "compilerOptions": {
    "outDir": "../dist/out-tsc/app/server",
    "module": "commonjs"
  },
  "angularCompilerOptions": {
    "entryModule": "./app/app.server.module#AppServerModule"
  },
+  "include": [
+    "./**/*",
+    "../node_modules/@angular/material/button*",
+    "../node_modules/@angular/material/card*",
+    "../node_modules/@angular/material/toolbar*"
  ]
}

And bundle all dependencies in server build (in angular.json):

"server": {
  "builder": "@angular-devkit/build-angular:server",
  "options": {
    "outputPath": "dist/app/server",
    "main": "src/main.server.ts",
    "tsConfig": "src/tsconfig.server.json",
+    "bundleDependencies": "all"
  },
  "configurations": {
    "production": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }
      ]
    }
  }
},

And finally replace webpack/hot/poll.js entry (in udk dev container) by a custom one which will use HMR Management API to not apply changes from modules @angular/material/*.ngfactory.

I found another solution based on typescript compiler paths option by resolving @angular/material/*.ngFactory to a proxy ngFactory which resolve the exists one in runtime.

May we can share this issue with angular/material team.

enten commented 5 years ago

@bobsingor I have good news sir!

I updated angular-universal branch material with angular/material (and angular/cdk) v7.1.0 and I checked if your issue still exists: I'm happy to tell you that this is no longer the case.

Now, when you edit a file (which related to material module) you will see that kind of logs below in your browser console.

Can you try to update your versions (angular v7.1.0 and udk v1.0.0) and give a feedback if that works for you too?

[HMR] bundle rebuilding
[HMR] bundle 'browser' rebuilt in 7049ms
[HMR] Checking for updates on the server...
[HMR] bundle 'server' rebuilt in 8224ms
[HMR] Updated modules:
[HMR]  - ./src/app/welcome/welcome.component.ts
[HMR]  - ./src/app/welcome/welcome-routing.module.ts
[HMR]  - ./src/app/welcome/welcome.module.ts
[HMR]  - ./src/$$_lazy_route_resource lazy namespace object
[HMR]  - ./node_modules/@angular/core/fesm5/core.js
[HMR]  - ./src/main.ts
[HMR]  - ./node_modules/@angular/platform-browser-dynamic/fesm5/platform-browser-dynamic.js
[HMR]  - ./node_modules/@angular/common/fesm5/common.js
[HMR]  - ./node_modules/@angular/platform-browser/fesm5/platform-browser.js
[HMR]  - ./src/app/app.browser.module.ts
[HMR]  - ./node_modules/@angular/platform-browser/fesm5/animations.js
[HMR]  - ./node_modules/@angular/animations/fesm5/browser.js
[HMR]  - ./src/app/app.module.ts
[HMR]  - ./src/app/app-routing.module.ts
[HMR]  - ./node_modules/@angular/router/fesm5/router.js
[HMR]  - ./src/app/app.component.ts
[HMR]  - ./src/app/not-found.component.ts
[HMR]  - ./node_modules/@nguniversal/express-engine/fesm5/tokens.js
[HMR]  - ./src/app/material.module.ts
[HMR]  - ./node_modules/@angular/material/esm5/material.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/a11y.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/platform.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/observers.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/autocomplete.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/core.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/bidi.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/scrolling.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/collections.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/overlay.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/portal.es5.js
[HMR]  - ./node_modules/@angular/forms/fesm5/forms.js
[HMR]  - ./node_modules/@angular/material/esm5/form-field.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/badge.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/bottom-sheet.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/layout.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/button.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/button-toggle.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/card.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/checkbox.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/chips.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/datepicker.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/dialog.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/text-field.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/input.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/divider.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/expansion.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/accordion.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/grid-list.es5.js
[HMR]  - ./node_modules/@angular/common/fesm5/http.js
[HMR]  - ./node_modules/@angular/material/esm5/icon.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/list.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/menu.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/paginator.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/select.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/tooltip.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/progress-bar.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/progress-spinner.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/radio.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/sidenav.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/slide-toggle.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/slider.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/snack-bar.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/sort.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/stepper.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/stepper.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/table.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/table.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/tabs.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/toolbar.es5.js
[HMR]  - ./node_modules/@angular/material/esm5/tree.es5.js
[HMR]  - ./node_modules/@angular/cdk/esm5/tree.es5.js
[HMR] App is up to date.
enten commented 5 years ago

HMR with angular material v8.0.0-rc.2 still works on browser and server sides.