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

What's the right way to include inline JS from remote sources? #1658

Open
IonicaBizau opened this issue Nov 30, 2016 · 9 comments
Open

What's the right way to include inline JS from remote sources? #1658

IonicaBizau opened this issue Nov 30, 2016 · 9 comments

Comments

@IonicaBizau
Copy link

IonicaBizau commented Nov 30, 2016

How can I include a remote script in the bundle? Similar to the inline @import from less.

@chinedufn
Copy link

You might

  • use browserify-shim to expose your script. Without knowing what you're looking to do I'd guess that this could be the answer that you're looking for.

  • create a browserify transform to download your script during build time

    • Wouldn't recommend this unless it was necessary for some reason

@IonicaBizau
Copy link
Author

@chinedufn The first link is quite useful!

Tho, what I need is to download the script if it's remote. So, by doing b.add('https://example.com/foo.js');, foo.js should appear in the bundle and be executed. It won't be required by any script.

@chinedufn
Copy link

chinedufn commented Dec 5, 2016

@IonicaBizau ahh gotcha

So sorry if I'm misunderstanding, but are there any issues with downloading the script and then adding it to the bundle?

i.e.:

require('xhr').get('https://example.com/foo.js', function (err, resp) {
  if (err) { throw(err) }
  var myScript = resp.body
  b.add(myScript)
})

@IonicaBizau
Copy link
Author

@chinedufn Oh, so, can we simply add a JavaScript code snippet? I guessed that works only for paths.

I'll try it.

@IonicaBizau
Copy link
Author

Thanks! 🍰

@chinedufn
Copy link

Gah I think it might only work with paths as you said. @terinjokes do you know if there's a reason for this?

@IonicaBizau in the meantime maybe just fs.writeFileSync and then b.add that file?

@IonicaBizau
Copy link
Author

@chinedufn Correct, but I was thinking there is a transform for that, something similar to this: https://github.com/dsdenes/browserify-cdnjs

While that looks good for cdnjs, I'd like to have one for any url. In my case, I don't really need to require the resources, but just to get them executed on the page.

@chinedufn
Copy link

@IonicaBizau ah cool, gotcha gotcha

I just searched "browserify load script" on node-modules.com and came across bromote

Might that be of any use?

@MellowMelon
Copy link
Collaborator

Browserify allows you to feed in streams when you call methods like add. From front readme:

If an entry file is a stream, its contents will be used. You should pass opts.basedir when using streaming files so that relative requires can be resolved.

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

No branches or pull requests

3 participants