Skip to content

automatic module resolution for JPSM packages #6012

Description

I'm trying to use angular2 as a JSPM package.
When I'm doing something like
import {bootstrap, Component, Input} from 'angular2/angular2';
in my code then typescript complains that it can't find the definitions and I don't get any code completions.

The workaround is to install angular2 also as a npm package and set moduleResolution to node. However this seems not optimal, because now I have two dependencies whose versions need to be aligned.

I guess tsc could resolve the modules for jspm/systemjs similar to how it already can do it for node. E.g. look up package.json, config.js and use the pathes from there the locate to type definitions inside the jspm_packages folder.

Not really sure if this is covered by #5039 and #5728.

Activity

  1. mhegazy commented on Dec 9, 2015

    @mhegazy
    Contributor

    JSPM resolution does not seem to be a simple task to do statically. #5039 should allow you to add to the configuration an override for resolving angular2, would that be sufficient?

  2. Matthias247 commented on Dec 9, 2015

    @Matthias247
    Author

    I'm not sure if it would work.
    From my projects root directory the path to angular2 is jspm_packages/npm/[email protected]/angular.(js|d.ts)
    If i would set a rootdir to jspm_packages/npm/ then import 'angular2/angular2' would stil not work because the version number is inserted by JSPM.
    Setting the rootdir to include the version number would require only importing angular2. Then typescript would probably be happy - but SystemJS would no longer find the module.

  3. Matthias247 commented on Dec 9, 2015

    @Matthias247
    Author

    Where do you see problems with static resolution?
    JSPM creates a SystemJS config file which contains all the necessary information:

    System.config({
      baseURL: "/",
      defaultJSExtensions: true,
      transpiler: "none",
      paths: {
        "github:*": "jspm_packages/github/*",
        "npm:*": "jspm_packages/npm/*"
      },
      map: {
        "angular2": "npm:[email protected]",
      ...
      }
    });
    

    This means if we can locate this file and parse it we know that import 'angular2/x' points to jspm_packages/npm/[email protected]/x.

  4. vladima commented on Dec 9, 2015

    @vladima
    Contributor

    the difference between node module resolution and JSPM is that node rules are exactly specified whereis JSPM resolution logic can only be reconstructed from the implementation (and implementation has several augmentations during the last year). I can see TypeScript having support for JSPM module resolution after it will be stabilized and specified otherwise there is a risk that TypeScript will always be one step behind chasing down SystemJS and this will be an endless source of issues with the same origin "TypeScript cannot find something that SystemJS can".

  5. unional commented on Dec 12, 2015

    @unional
    Contributor

    Would it be possible to specify a resolution plugin and the plugin implementation is managed by jspm or someone closer on that end?

    I have the same need (as everyone using typescript with jspm should).

  6. vladima commented on Dec 13, 2015

    @vladima
    Contributor

    currently module resolution is customizable if you use compiler API, we don't support loading user defined resolvers when using command line compiler. This is something that we have in our backlog but the size of this workitem is quite large (it should just work not only in command line compiler but also in all editors we officially support)

  7. unional commented on Dec 13, 2015

    @unional
    Contributor

    Indeed. From what you are saying, is it correct that in order to have editor support (webStorm, atom VS Code etc) resolving jspm packages correctly, we need to either:

    1. Complete the command line compiler backlog item, or
    2. Editors can use the compiler API to implement the proper resolution
      Is 2 possible?

    Thanks,
    Uni

  8. vladima commented on Dec 13, 2015

    @vladima
    Contributor

    In theory it is possible however keep in mind module resolution in editor and in compiler should be consistent otherwise we'll get quite annoying behavior when compiler cannot find some module that editor can discover just fine and vice versa.

  9. unional commented on Dec 13, 2015

    @unional
    Contributor

    Agree. Currently that is exactly what annoys me as systemjs/jspm can discover and run application correctly but the editor and tsc command line compiler cannot.

  10. dsebastien commented on Dec 18, 2015

    @dsebastien

    +1, this is really pushing me to consider letting go of JSPM :(

  11. strabu commented on Jan 4, 2016

    @strabu

    +1, Agree, modules from jspm_packages should be resolved by TSC and the editors.
    Otherwise JSPM and it's bundling-workflow would be dead for Typescript-users.

  12. giovannicandido commented on Jan 5, 2016

    @giovannicandido

    To put my two cents on the discussion:
    http://stackoverflow.com/questions/33702567/how-to-import-external-npm-module-with-typescript-declarations-using-jspm/34407887#34407887

    My work around is to create definitions files for the jspm library with a top level declaration module for instance:

    File notify.d.ts

    declare module 'common/notify' {
    export declare class Notify {
    }
    }
    

    File common.d.ts

    declare class Common {
    }
    

    File components/ca.d.ts

    declare module 'common/components/ca' {
      export declare class Bla {
      }
    }
    

    That way a import for 'common/components/ca' is resolved

    import Bla from 'common/components/ca'
    

    Problems:

    1. Manual edit declaration files, there is no way to generate the files automatically in this shape
    2. common could be override in the Systemjs system. The developer must install the library in the form jspm install common=github:/bla/bla if there is a name clash with other library, no luck here.

    I wish the javascript module community had put the pride beside and had learned something with old dog java, and have made that thing less cumbersome. Java import's are way more stable and predictable, not ideal, and for sure not the best, but work very good with external libraries.

    The javascript module standards forget distribution and package as a crucial design

  13. added
    SuggestionAn idea for TypeScript
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Jan 7, 2016
  14. frederikschubert commented on Jan 8, 2016

    @frederikschubert

    I would love to see this feature implemented as its the only thing that needs a workaround in our Angular 2 setup (apart from the hopefully soon released jspm 0.17).

  15. 71 remaining items

  16. blakeembrey commented on Nov 28, 2016

    @blakeembrey
    Contributor

    Mohamed Hegazy (@mhegazy) No worries, thanks. I just thought you had misread from your comment being explicitly about @types packages. There's a small amount of overhead of maintenance with the duplication approach and duplicating, I had assumed, was already the only approach possible. The only thing not possible, I believe, with the duplication approach is that JSPM supports module aliases. I don't think there's an easy way to do that with NPM.

  17. mhegazy commented on Nov 28, 2016

    @mhegazy
    Contributor

    would setting paths help with that?

  18. blakeembrey commented on Nov 28, 2016

    @blakeembrey
    Contributor

    Good point, sounds like that should work by combining npm install (for the package) and TypeScript paths (for the alias). I believe this wouldn't remove the original module which doesn't exist, but sounds usable0. Sorry, I don't use JSPM myself, just find myself answering the questions a lot so that knowledge is helpful. Cheers 👍

    Side note: The handbook font sizing really shouldn't use ems. They compound and have giant text for list items. I'll find the right place to log this.

  19. aluanhaddad commented on Nov 28, 2016

    @aluanhaddad
    Contributor

    Paths work for aliasing certain modules but they are difficult to maintain and only a partial solution as they fail on constructs like

    /// <reference types="dependency" />

    Because we can redirect the directory structure to point at the packages folder but cannot alias the actual reference because it's an Types (@types) reference.

    That said, the real difficulty is in maintaining all the versionsed paths. It would be helpful if we could specify wildcards in "paths" to match prefixes instead of entire names.
    For example it would be great if

    {
      "paths": {
        "moment" : [
          "jspm_packages/npm/moment*/index"
        ]
      }
    }

    could be used to match "jspm_packages/npm/ [email protected]/index".

    It's only a solution because it doesn't deal with the transitive dependency issue that jspm solves but it would go a long way in making a lot of scenarios easier.

  20. damiandennis commented on Feb 5, 2017

    @damiandennis

    Adding this to my devDependencies in package.json worked for me. Only problem is you have to be specific on the package names :(

    "@angular/common": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/compiler": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/core": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/forms": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/http": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/platform-browser": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/platform-browser-dynamic": "file:jspm_packages/npm/@angular/[email protected]",
        "@angular/router": "file:jspm_packages/npm/@angular/[email protected]",

    It would work similar for any Type (@type) npm libraries as well I am fairly sure.

  21. nahuel commented on Mar 6, 2017

    @nahuel

    You can use https://github.com/charto/cbuild to create a SystemJS bundle from node_modules/
    There is also work here to make SystemJS use only node_modules/: https://github.com/alexisvincent/systemjs-tools/blob/master/docs/features.md#node_modules-package-resolution-beta
    I think the best option long term is to not use jspm_packages/ at all, but make SystemJS compatible with node_modules/.

  22. atrauzzi commented on Mar 7, 2017

    @atrauzzi

    Yeah, definitely agreeing with this. Ecosystem needs to come together and I would love for JSPM & SystemJS to play nicely.

  23. aluanhaddad commented on Mar 7, 2017

    @aluanhaddad
    Contributor

    Alexander Trauzzi (@atrauzzi) I really want to see this. Is there something in JSPM specifically that you feel is out of balance here? From my point of view, the versioned folders approach has a lot of advantages. The primary disadvantage is in fact the impedance mismatch between JSPM and TypeScript but I would appreciate any thoughts you might have on things that could be improved on the JSPM side as they pertain to this issue.

  24. atrauzzi commented on Mar 7, 2017

    @atrauzzi

    The biggest problem is going to be that JSPM isn't the same final structure as nodejs and that has loader implications. Implications that most tooling has now come to agree are the one true way.

    In the past, anything that tries to compete with a community recognized standard has typically lost. Quite soundly. If we look at why and how in this specific case, we have to understand that both JSPM and TypeScript are subsets of the nodejs community overall.

    But yeah, I entirely agree, JSPM got a lot of things right. But we have yarn now, and we've got node_modules, and it almost seems like the book is closed on the whole topic...for now?

  25. mythz commented on Mar 10, 2017

    @mythz

    When it works JSPM is great so it would be a shame if JSPM remained unsupported. It appears the latest jspm@beta has broken integration with TypeScript again and after wasting some time permutating through different configurations I've had to pin it to an older version of jspm@beta.

    It would be nice to see some clarification from the TypeScript team if JSPM is going to be a supported configuration and if there are plans to provide native support and get some much needed Q/A love? or if the TypeScript team are focusing on a different supported configuration and what the recommended configuration should be? i.e. npm/webpack?

    It would help in deciding which approach to adopt that's well supported and will continue to work in future.

  26. unional commented on Mar 10, 2017

    @unional
    Contributor

    Demis Bellot (@mythz) FYI it is probably due to this: systemjs/systemjs#1587
    And we are at a fork here on how to get TS to work with JS moving "backward" (interop with CommonJS).

  27. mqudsi commented on Apr 20, 2018

    @mqudsi

    Has the situation changed in the past year? I see the proposal for resolution plugins was closed, and the preference is to simply work with SystemJS "out-of-the-box". Is there another issue I can track or that blocks this?

  28. jakeNiemiec commented on Apr 20, 2018

    @jakeNiemiec

    Mahmoud Al-Qudsi (@mqudsi) you have this coming down the pipeline: https://github.com/nodejs/modules

  29. RyanCavanaugh commented on Jun 24, 2021

    @RyanCavanaugh
    Member

    JSPM seems to have gone quietly into that good night (Google Trends graph below), so I don't think this is likely to ever be a priority for us now.

    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions