cyrilletuzi / angular-async-local-storage

Efficient client-side storage for Angular: simple API + performance + Observables + validation
MIT License
676 stars 68 forks source link

AOT Compiling in Angular 6 causes the LocalStorage Module to go missing #41

Closed darthmolen closed 6 years ago

darthmolen commented 6 years ago

Angular 6 app. Works in dev mode. However, we were getting errors in the console when deploying our app using v6 of this component. Enabled source maps to figure out what was going on and found out that the library is missing after the application is AOT compiled.

image

When aot compiling for angular, the library goes missing.

In Dev Mode:

image

After AOT compiled.

image

I'm including our package.json and angular.json

ClientApp.zip

cyrilletuzi commented 6 years ago

Thanks for reporting this issue and for the details. I can't reproduce it yet with a classic build, I'll investigate more.

A few questions :

cyrilletuzi commented 6 years ago

Tested the second scenario (only in a lazy-loaded module), it works.

@darthmolen Waiting for you to test with a full reinstallation, as installations problems happen often, to not investigate hours for nothing.

By the way, do you use npm or yarn?

darthmolen commented 6 years ago

I'll delete my node_modules and do a new npm install and let you know. I did do an upgrade from 5 to 6.

I use both npm and yarn. They get different results. Sometimes better, sometimes worse depending.

alignsoft commented 6 years ago

If you’re upgrading from 5 to 6, did you remove the Import?

Then, for versions 4 & 5 only, include the LocalStorageModule in your app root module (just once, do NOT re-import it in your submodules). Since version 6, this step is no longer required and LocalStorageModule is removed.

import { LocalStorageModule } from '@ngx-pwa/local-storage'; @NgModule({ imports: [ BrowserModule, LocalStorageModule, ... ] ... }) export class AppModule {}

Best, :Steve

Stephen R. Smith

Alignsoft 447 Nelson Ave, Burlington, Ontario, Canada, L7S 1N3 P : 905.464.4440

ssmith@alignsoft.com http://www.alignsoft.com

On Jul 25, 2018, at 3:28 PM, Steven Molen notifications@github.com wrote:

I'll delete my node_modules and do a new npm install and let you know. I did do an upgrade from 5 to 6.

I use both npm and yarn. They get different results. Sometimes better, sometimes worse depending.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

darthmolen commented 6 years ago

I deleted my node_modules, did a yarn install --force and then an npm install for grins and giggles. Still works in non-prod mode, but --prod, the module no longer exists in the sourcemap, and comes in as null in the constructor. It's like it doesn't exist to ng build and webpack. I went ahead and zipped up the source so you can see for yourself, there's nothing sensitive in there. https://www.dropbox.com/s/woii6ungmtujj4c/PF.Server.WebUI.zip?dl=0

cyrilletuzi commented 6 years ago

Thanks for the files. You can now delete them for safety.

Found the issue: it's because your way of defining constructors is quite strange:

constructor()
constructor(private localStorage?: LocalStorage) {}

Just do the normal way:

constructor(private localStorage: LocalStorage) {}

and then it works.

I suppose what happens is that tree-shaking happening in prod mode doesn't understand your way of defining constructor parameters, as they are normally handled by Angular dependency injection, and then thinks the param is not used or not defined and so deletes it for performance.

Closing as it's not related to the lib, but I would be interested to know if there is a reason for this way of defining your constructor?

darthmolen commented 6 years ago

I'll work through changing that then. The reason the constructor was that way was that I wanted to use it as a standard business object as well for when dependency injection wasn't involved and I simply needed a clone for serialization or other work.

cyrilletuzi commented 6 years ago

Thanks for the info.

darthmolen commented 6 years ago

@cyrilletuzi For informational purposes, looks like they might add that feature to angular :) (nullable types still being respected by the transpiler) https://github.com/angular/angular/issues/25151