Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instanceof a class in angular library not working #47823

Open
malvandi opened this issue Oct 20, 2022 · 3 comments
Open

instanceof a class in angular library not working #47823

malvandi opened this issue Oct 20, 2022 · 3 comments
Labels
area: docs Anything related to doc content, incl guides, API reference, tutorial, web content docsarea: library-or-schematics Use only with "area: docs". Applies to doc about creating and using libraries or schematics. good first issue An issue that is suitable for first-time contributors; often a documentation issue.
Milestone

Comments

@malvandi
Copy link

malvandi commented Oct 20, 2022

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

I have an angular project angular-library-project(link). I create an angular library with command ng g library angular-library. Now I create the class AngularLibrary. The structure as follows:
angular-library-project

I add ol dependency to both projects and used ol/View class as follow:
AngularLibrary class:

import View from 'ol/View';

export class AngularLibrary {

  constructor(view: View) {
    console.log('In Library: ', view instanceof View);
  }
}

And I used AngularLibrary in app.component.ts file as follows:

import {Component, OnInit} from '@angular/core';
import {AngularLibrary} from '../../projects/angular-library/src/lib/angular-library';
import View from 'ol/View';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'angular-library-project';


  ngOnInit(): void {

    const view = new View({
      zoom: 5,
      center: [0, 0]
    });

    console.log('In Component: ', view instanceof View);

    new AngularLibrary(view)
  }
}

The console log is the opposite of what I expected. It's as follows:
result

What's the problem?

Please provide a link to a minimal reproduction of the bug

https://github.com/malvandi/angular-library-project

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.2.6
Node: 16.17.0
Package Manager: npm 8.19.2
OS: win32 x64

Angular:
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1402.6 (cli-only)
@angular-devkit/core         14.2.6 (cli-only)
@angular-devkit/schematics   14.2.6 (cli-only)
@schematics/angular          14.2.6 (cli-only)

Anything else?

No response

@ngbot ngbot bot added this to the needsTriage milestone Oct 20, 2022
@JoostK
Copy link
Member

JoostK commented Oct 20, 2022

You're observing the effect of how Node package management/resolution works here, not something specific to Angular libraries.

The library has its own package.json, which results in a dedicated node_modules directory within projects/angular-library/ which duplicates the ol dependency from the root node_modules folder. Since the source file angular-library.ts imports ol, the Node module resolution logic will look in the nearest node_modules tree to find the ol dependency from the library itself, which is a distinct copy from the one imported by the app (which came from the root node_modules). This results in multiple copies of those modules being bundles, causing duplicate code and resulting in instanceof mismatches.

In a workspace with multiple projects/libraries it becomes beneficial to use npm/Yarn workspaces, where you then install dependencies from the root while still considering the package.json files of nested projects. This allows the package manager to hoist nested dependencies into the root node_modules directory, provided that the versions allow for it. This hoisting process deduplicates the modules, resolving the problem.

@JoostK
Copy link
Member

JoostK commented Oct 20, 2022

tldr; this is not a bug, but I don't know to what extent the documentation currently covers anything like this and whether this warrants additional docs (since it is just the effect of Node module resolution, not specific to Angular).

@JoostK JoostK added area: docs Anything related to doc content, incl guides, API reference, tutorial, web content docsarea: library-or-schematics Use only with "area: docs". Applies to doc about creating and using libraries or schematics. and removed area: compiler labels Oct 20, 2022
@alan-agius4
Copy link
Contributor

alan-agius4 commented Oct 21, 2022

We might want to improve the peer dependencies section and explain why peer dependencies should be used.

@alan-agius4 alan-agius4 added the good first issue An issue that is suitable for first-time contributors; often a documentation issue. label Oct 28, 2022
Labels
area: docs Anything related to doc content, incl guides, API reference, tutorial, web content docsarea: library-or-schematics Use only with "area: docs". Applies to doc about creating and using libraries or schematics. good first issue An issue that is suitable for first-time contributors; often a documentation issue.
Projects
None yet
Development

No branches or pull requests

4 participants