jspm / generator

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

Html Generation #95

Closed guybedford closed 2 years ago

guybedford commented 2 years ago

This adds a new generator.htmlGenerate(htmlString) method, which takes an HTML string as input, parses the HTML, detects all module imports, runs the generator over them, then updates the HTML to update an existing import map or inject a new import map which includes the necessary mappings for execution.

Injection options can be provided of the form { preload: boolean, integrity: boolean, whitespace: boolean, esModuleShims: boolean | string, htmlUrl: string }. The defaults are no preloading an integrity, but whitesace and es module shims being injected.

ES Module shims will automatically be looked up to the latest version for the given provider.

The full end to end example looks like:

import { Generator } from '@jspm/generator';

const generator = new Generator({
  mapUrl: new URL('./local/page.html', import.meta.url),
  env: ['production', 'browser']
});

const outputHtml = await generator.htmlGenerate(`
  <!doctype html>
  <script type="module">import 'react'</script>
`);

where the outputHtml will contain the generated import map and ES module shims, taking into account indentation and whitespace.