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

Add script type="module" support #84

Open
asos-tomp opened this issue Feb 12, 2019 · 4 comments
Open

Add script type="module" support #84

asos-tomp opened this issue Feb 12, 2019 · 4 comments

Comments

@asos-tomp
Copy link

asos-tomp commented Feb 12, 2019

https://developers.google.com/web/fundamentals/primers/modules

Accept args.module = true for a particular script to add type="module". It would be up to consumers to do a feature detect:

'noModule' in document.createElement('script')

...to decide which script to request in loadJs, but using the same identifier for both that and a traditional script.

@amorey
Copy link
Member

amorey commented Feb 13, 2019

You should be able to do this using the before callback:

loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
  success: function() {},
  error: function(pathsNotFound) {},
  before: function(path, scriptEl) {
    /* called for each script node before being embedded */
    if (path === '/path/to/foo.js') scriptEl.type = "module";
  }
});

Let me know if that helps.

@asos-tomp
Copy link
Author

I hadn't considered that hook, thanks!

Would still be nice to have this embedded into the loader, however. I'm trying to keep the amount of code written to the page as small as possible, and the above is relatively verbose.

@toddw
Copy link
Contributor

toddw commented Feb 20, 2019

You can compress that down to something like:

before: function(p, l) { p == ‘/path’ && l.type= “module” }

If you are only targeting modern browsers you can make it even smaller:

before: (p, l) => p == ‘/path’ && l.type= “module”

In order to make that even smaller, it would probably require more code in loadjs. If you found a way somehow to make it smaller it’s probably not going to be super benefitial.

@asos-tomp
Copy link
Author

asos-tomp commented Feb 27, 2019

Yep, makes sense. I guess I'm just thinking if you have a lot of scripts to load, and each needs to add the before: code, it might be nice to have a native option (like async), and hopefully only a very small addition to loadjs itself, i.e.

module = args.module
...
e.type = module ? "module" : undefined

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

3 participants