jspm / generator

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

Tracing dynamic import expressions #137

Open zachsa opened 2 years ago

zachsa commented 2 years ago

Using an index file as an import map entry point, I have found that depending on how I define the string indicating the import that the import map either fails succeeds. For example

This works (i.e. the import map is generated correctly)

// index.importmap.js
import('./.cache/ssr.some-file.js')

This does not work (the dynamic import is ignored)

// index.importmap.js
import(`./.cache/ssr.some-file.js`)

This does not work (the dynamic import is ignored)

// index.importmap.js
const p = './.cache/ssr.some-file.js'
import(p)

This does not work (the dynamic import is ignored)

// index.importmap.js
import('./.cache/ssr.some-file.js' + '')

This seems very odd to me - at a guess I would say the the import expression is hoisted above variable assignment (not sure if this makes sense). In any case, I didn't expect that dynamic imports would be sensitive to the way in which path-strings are defined (not sure if this is JSPM or something else)

The use case is that I would like to be able to pick up when a user adds a new file that should be included in import map generation:

import { readdir } from 'fs/promises'
readdir('./.ssr-webapp')
  .then(files => files.filter(f => f.startsWith('ssr.')))
  .then(entryPoints => entryPoints.forEach(js => import(`./ssr-webapp/${js}`)))