jspm / generator

JSPM Import Map Generator
Apache License 2.0
167 stars 21 forks source link

Checkout API #92

Open guybedford opened 2 years ago

guybedford commented 2 years ago

Currently import maps generated by JSPM generator are always made comprehensively against the JSPM CDN.

There could be benefit in having a checkout API that enables a way to bring CDN workflows back to the local file system.

Consider an import map has already been generated for the generator, then we might want to perform the following steps:

const fsMap = generator.checkout({
  // if any package has only one import with no deps,
  // don't check out the whole folder and instead rename
  // the module to just be the name of the package itself
  singleFileCheckouts: true,

  // if a package only has one version, skip the version in the package name
  singleVersionNaming: true,

  // if a package only comes from one registry, skip the registry version name
  singleRegistryNaming: true,

  // required option to actually do any work
  checkoutUrls: {
    'https://ga.jspm.io/': './third_party/jspm/',
    'https://unpkg.com/': './third_party/unpkg/',
  }
);

// Output is a map of URLs to local paths
// The import map returned then works against these local paths directly
returning {
  // single file package
  'https://ga.jspm.io/npm:pkg@1.2.3/': {
    singleFile: true, // enabled by "singleFileNaming" option above
    target: './third_party/jspm/mod.js',
    traced: ['mod.js'],
    static: ['package.json', 'app.test.js']
  }

  // directory package
  'https://ga.jspm.io/npm:dep@1.2.3/': {
    singleFile: false,
    target: './third_party/jspm/dep/',
    traced: ['index.js', 'dep.js'],
    static: ['package.json', 'asset.txt']
  }

  // multi-version package (and for its single-file variant)
  'https://ga.jspm.io/npm:multi-version@1.2.3/': {
    singleFile: false,
    target: './third_party/jspm/multi-version@1.2.3/',
    traced: ['index.js', 'dep.js', 'dynamic-import.js'],
    static: ['package.json', 'asset.txt', 'unused.js']
  }
}

The import map is then updated to work against the new file mappings, and the download operation can be completed with a simple fetch loop over the files. Non-JS files could be included in the file map for directory packages along with their associated mappings.