jspm / generator

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

package in import map resolved to incorrect version #157

Closed zachsa closed 2 years ago

zachsa commented 2 years ago

Bug

JSPM Generator is producing an import map with the @arcgis/core library version specified incorrectly.

In package.json (and package-lock.json) the version is specified at 4.24.7. But generating the import map results in importing the library at a lower version than specified (4.23.7). So far I have only noticed this with the @arcgis libraries (and not, for example, the @mui libraries).

On a possibly related note. Trying the ga.jspm.io link for the 4.24.7 version gives me a 404 error:

To Reproduce

In a new directory:

npm init
npm install @arcgis/core@4.24.7
npm install @jspm/generator@1.0.0-beta.34

Create the following files

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8'>
  <script src="index.js" type="module"></script>
</head>
</html>

index.js

import Map from '@arcgis/core/Map'
const x = new Map()

jspm.js

import { Generator } from '@jspm/generator'
import { readFile, writeFile } from 'fs/promises'
import { pathToFileURL } from 'url'
import mkdirp from 'mkdirp'
import { dirname } from 'path'

const DEP = './index.html'
const TARGET = './output.html'

const generator = new Generator({
  mapUrl: pathToFileURL(TARGET),
  env: ['browser', 'production', 'module'],
  defaultProvider: 'jspm',
})

const htmlSource = await readFile(DEP, 'utf-8')
const pins = await generator.addMappings(htmlSource)
const html = await generator.htmlInject(htmlSource, {
  trace: true,
  pins,
  preload: false,
  integrity: false,
  esModuleShims: true,
})

mkdirp.sync(dirname(TARGET))
await writeFile(TARGET, html)

Generate the import map

node jspm.js

The output.html file shows the incorrectly resolved package version

<!DOCTYPE html>
<html>

<head>
  <meta charset='utf-8'>
  <!-- Generated by @jspm/generator - https://github.com/jspm/generator -->
  <script async src="https://ga.jspm.io/npm:es-module-shims@1.5.12/dist/es-module-shims.js"
    crossorigin="anonymous"></script>
  <script type="importmap">
  {
    "scopes": {
      "./": {
        "@arcgis/core/Map": "https://ga.jspm.io/npm:@arcgis/core@4.23.7/Map.js"
      },
      "https://ga.jspm.io/": {
        "@esri/arcgis-html-sanitizer": "https://ga.jspm.io/npm:@esri/arcgis-html-sanitizer@2.9.0/dist/esm/index.js",
        "@esri/calcite-components/dist/components/index.js": "https://ga.jspm.io/npm:@esri/calcite-components@1.0.0-beta.77/dist/components/index.js",
        "@stencil/core/internal/app-data": "https://ga.jspm.io/npm:@stencil/core@2.13.0/internal/app-data/index.js",
        "@stencil/core/internal/client": "https://ga.jspm.io/npm:@stencil/core@2.13.0/internal/client/index.js",
        "focus-trap": "https://ga.jspm.io/npm:focus-trap@6.7.3/dist/focus-trap.esm.js",
        "luxon": "https://ga.jspm.io/npm:luxon@2.3.2/build/cjs-browser/luxon.js",
        "tabbable": "https://ga.jspm.io/npm:tabbable@5.3.3/dist/index.esm.js"
      }
    }
  }
  </script>
  <script src="index.js" type="module"></script>
</head>

</html>
guybedford commented 2 years ago

This should be working now - turns out the build was crashing, and I was able to track down the memory leak!

zachsa commented 2 years ago

Thanks very much!

Was it while building this library? I actually had a realization that the same thing may have been happening in a new version of the maplibre-gl library.

I was going to check this tomorrow

guybedford commented 2 years ago

I may have spoken too soon, it seems to still be held up in the package upload pipeline due to the file count.

zachsa commented 2 years ago

I see that the new version is accessible via the CDN. Does that mean it's working completely?

zachsa commented 2 years ago

Also, ideally import map generation would fail if exact versions can't be matched - or at least there should be a console warning

zachsa commented 2 years ago

Looking at this now - the same thing happens for another library:

npm install maplibre-gl@2.2.0-pre.3

The import map then specifies an older version of this library:

"maplibre-gl": "https://ga.jspm.io/npm:maplibre-gl@2.1.9/dist/maplibre-gl.js",

But in this case the newer library version does appear to be available via the JSPM CDN - https://ga.jspm.io/npm:maplibre-gl@2.2.0-pre.3/dist/maplibre-gl.js

guybedford commented 2 years ago

This build should be working now, and with the override as well.