jspm / generator

JSPM Import Map Generator
Apache License 2.0
160 stars 20 forks source link

Question: Usage of provider:nodemodules #294

Closed NakajimaTakuya closed 1 year ago

NakajimaTakuya commented 1 year ago

Hi Thanks for the great package. I have one question.

I have confirmed that specifying nodemodules in the deliver option generates import-maps that resolve the path to node_modules.

Is this an option that is targeted for the front-end or a feature that is intended to use import-maps on node.js?

If it is for the front end, I assume it is for node.js since node_modules needs to be in the public directory. Am I right?

Bubblyworld commented 1 year ago

Hey,

In theory you can use a nodemodules import map in any runtime that supports import maps, like the browser or Deno (or even node.js! but then you need to use an experimental loader like this one). You just have to make sure of two things:

  1. Your dependencies are compatible with the runtime. For instance, if you have a CommonJS dependency then it'll probably only work in node.js.
  2. The node_modules folder is accessible by the runtime. This means node_modules will need to be in the public dir if you're targeting the browser, yeah.

If you want to customise what the generator treats as the root of your public directory, you can change the rootUrl option: https://jspm.org/docs/generator/stable/interfaces/GeneratorOptions.html#rootUrl

NakajimaTakuya commented 1 year ago

I see! Thanks. One last question: is it common to treat node_modules as a public directory? I've seen some that spit out web_modules exclusively, like snowpack, but this is the first time I've heard of making node_modules public. :flushed:

Bubblyworld commented 1 year ago

It's pretty uncommon! The intended use is for local development - if your project uses ESM dependencies then you can just npm install them and jspm install --provider nodemodules -o index.html to start using them locally, offline. For production stuff you should definitely be using a CDN though (jspm install --provider jspm.io or any of the other providers).

NakajimaTakuya commented 1 year ago

@Bubblyworld Thank you, that made it very clear.:heart: