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
[question] Difference between externals and globals #1169
Comments
I'm under the impression that I think it boils down to how the bundle is actually written and what format you are emitting. |
Thanks for the quick response!
Interesting. So, if my code is (You can see a runnable version of this, albeit produced for a different issue, here.) Can you describe what would be different, in terms of how the bundle's written, in a case where I was using |
The The node-resolve plugin doesn't like failing to resolve module IDs (because it usually indicates a bug, like you forgot to install the package in question), so it will throw an error rather than letting Rollup print a warning. If you use the So, we have our bundle, and we're generating an An aside: it's more common to use the package ID rather than a symbol, so you'd typically import jQuery with import $ from 'jquery'; and use |
Awesome @Rich-Harris, I can now see how they differ. I'm still confused about when you'd use them separately though—doesn't On that latter question, your remark about import $ from 'jquery';
$(document.body).text('hello world'); results in output import $ from 'jquery';
$(document.body).text('hello world'); so there's one case where I guess you could use just Anyhow, it still seems to me that |
@wearhere I'm also confused but maybe an internal module could be exported onto the window by setting it as a global but not external. I mean, maybe it was considered as a future possibility. So I guess what @Rich-Harris is trying to say is that (sorry, I'm just logging it here for my own benefit): globals are assumed to have their field value on the globals: {
name: 'Value',
}, assumes that some other script tag or whatever establishes The resolver needs to be told how to link these globals and that's what the external: ['name'], Then you can reference it like import myName from 'name';
myName(); |
That makes sense to me @cool-Blue! And since my Jan 1 comment we've learned why you might want to use We've started to factor some of our client-side JS as libraries to share between projects. These libraries So when these libraries bundle themselves for distribution, as ES6 modules, they mark Then, the projects that use these libraries get to process these I still don't know when you would want to use |
Isn't
All my imports are relative paths, either ./Foo.js or ../bar/Foo.js. If I use this format, how would I specify the global IDs? Just 'Foo'? Or the full path './Foo.js' or '../bar/Foo.js'? |
@backspaces in this case |
Thanks! Wasn't clear that Rollup wasn't using es6 Module syntax. I know webpack and others do the same. Thanks to your seriously excellent docs I noticed I could have a function for globals so this solved my problems .. thanks:
|
Dear community. I am pulling in an NPM package, and added it to "external". Now Rollup is trying to guess it's
I'm not 100% sure what is meant with this:
|
If you are bundling into entry import x from "some-npm-package";
console.log(x); config ...
output: {
...
globals: {
// map 'some-npm-package' to 'SomeNPMPackage' global variable
"some-npm-package": "SomeNPMPackage"
}
},
external: ["some-npm-package"] Bundle result: (function(x) {
console.log(x);
})(SomeNPMPackage); // the 'SomeNPMPackage' global variable is used as 'x'
Why users would receive the warning? Isn't the warning is generated by rollup? |
@eight04 Thank you, I see that it's only relevant for IIFE builds? (I do get the warning on es and cjs builds as well though.) And for other users to prevent to receive this warning: Yes, it's only on rollup, I meant if other users use Rollup, can I set up the globals: {
// map 'some-npm-package' to 'SomeNPMPackage' global variable
"some-npm-package": "SomeNPMPackage"
} on the package side, so not all users who import it and use rollup on it need to do this themselves? |
That's weird. You can use |
Rollup warns about not providing an globals for external modules for reasons explained here: rollup/rollup#1169 (comment) In developit/microbundle#223 the suggestion is to mark the modules as globals to tell rollup that it's ok. It seems to me that microbundle should also apply globals for scoped packages as it does for non-scoped.
I am loading jQuery from a CDN, as a global (
$
). However for consistency within my project, I wish to writeimport $ from '$'
.I originally thought that I could do this using the
external
option because jQuery is "a module that should remain external to my bundle", no? But Rollup threw an error, saying that I should useglobals
instead. Which is fine, that works and also seems sensible given that option's documentation—but then what are externals for?The text was updated successfully, but these errors were encountered: