Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oskarrough opened this issue Feb 18, 2018 · 3 comments
Open

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

oskarrough opened this issue Feb 18, 2018 · 3 comments

Comments

@oskarrough
Copy link

oskarrough commented Feb 18, 2018

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

@georgedoescode
Copy link

Hey 🙂 I'm kinda new to rollup but also ran into this problem. The way I got around it is to use @rollup/plugin-node-resolve as a plugin when bundling modules for use in the browser, so that dependancies are all bundled up and no resolving is needed client-side. Something like:

// rollup.config.js
{
      input: 'src/main.js',
      output: {
          file: pkg.module,
          format: 'esm'
      },
      plugins: [
          resolve(),
      ]
},

Whereas if I was building out an es module to be used in Webpack / whatever I wouldn't bundle the dependancies and let the build tool handle it.

There might be a better way of handling this so if anyone has a better option give me a shout :)

Cheers!

@theawless
Copy link

@oskarrough it's not possible right now to change where the identifier points to.
In the future, we could probably use import-maps.

@damirsaleschoice
Copy link

For me, it was a matter of changing:
from:

<script src="js/bootstrap.js" type="module"></script>

to:

<script type="module" src="js/bootstrap.js"></script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants