Skip to content

How to build for <script type="module"> #15

@oskarrough

Description

@oskarrough

Hi, if your library depends on an external package, how can you build for <script type="module">? Bonus points if you can make it work as CJS, too.

Here's our library

// my-library.js
import fetch from 'isomorphic-unfetch'
export default function (url) {
  // do stuff
  return fetch(url)
}

Using rollup es format, can this work?

<script type="module">
  import myLibrary from './dist/my-library.es.js'
  myLibrary(42)
</script>

Right now you it warns you

Uncaught TypeError: Failed to resolve module specifier "isomorphic-unfetch". Relative references must start with either "/", "./", or "../".`

If you change the library's import to something like ./node_modules/isomorphic-unfetch/es-version.js it'll work locally but of course break the built version. What to do? What is this rabbit hole?

Any help would be great appreciated! Thank you.

Possibly related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions