You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But the script-loader is loading the script from the home domain. Shouldn't it be possible to load stuff from another domain (given that the CORS-header are set correctly)?
Is it possible to use a named-AMD-module like 'jquery' or a chunk as an "external" chunk ?
I have 2+ webpack runtimes on one page that require jquery - so, i can not use the CommonsChunkPlugin, right?
Since webpack is a module bundler, would it somehow be possible to give one chunk a url of an external chunk or amd module?
@ThomasDeutsch If you want to share chunks or common modules between multiple entry points, you need to use the CommonsChunkPlugin and compile all in a single webpack pass. Do this instead of multiple webpack runtimes. Check this.
I wrote this simple loader, external-webpack-loader, to solve this problem.
Is there any reason this is not the appropriate solution? I just found webpack today, so let me know if I'm not thinking correctly. Thanks!
Ah! I see the light. $script is the way to go since it takes care of downloading and evaluating the external script at runtime and keeps up with what you've already downloaded so that it won't download the same script twice. Makes sense. I think I had it in my head that webpack could do that, but it is a different task altogether.
I was just looking for something like this but for fonts, not scripts. It doesn't seem common fonts are stored in npm, so rather than maintaining my own variants I was hoping I could convince webpack to download them.
@m59peacemaker - It looks like you removed your loader project, do you still have the source?
Thanks @nkbt , but if I use externals, I'll have to take care of my own dependencies (which is a major reason to start using Webpack to begin with).
This is actually what I did (use externals), but not the point I'm trying to make.
Since bundling npm installed packages is way nicer and more Webpacky, Isn't Webpack encouraging bloated bundles by not giving a good remote CDN option for 3rd party libs???
A half-lazy developer would just pick the easy big-bundle way. I supposed many did.
For example, I've looked at several react-webpack tutorials. All of them encourage 'npm install react' rather than the ugly external.
Reacted by ryancwalsh, Paul Bernhardt, Nick Zavaritsky and warriorg
I understand where you are going here, but the thing is - CDN is an app concern, not bundler. Besides, there is no such thing as good remote CDN option for 3rd party libs except maybe the recent unpkg (npmcdn). But for any reasonably loaded prod it makes sense to buy/setup your own CDN.
I personally cannot come up with any reasonable answer to this. If you do - PR to docs would be a good point to start the discussion.
PS: mastering your tools is always a difficult process that takes heaps of time and errors. If it is possible to reduce this time - awesome
I haven't thoroughly tested it yet, but you can use something like this to load an external script:
const_scriptCache=newMap();/** * Load an external script. * * @param {string} url Absolute URL of script to load * @param {string=} name Name of global variable that the script is expected to define * @return {Promise} */exportfunctionloadScript(url,name){letpromise;if(_scriptCache.has(url)){// TODO: normalize URLpromise=_scriptCache.get(url);}else{promise=newPromise((resolve,reject)=>{letscript=document.createElement('script');script.onerror=event=>reject(newError(`Failed to load '${url}'`));script.onload=resolve;script.async=true;script.src=url;if(document.currentScript){document.currentScript.parentNode.insertBefore(script,document.currentScript);}else{(document.head||document.getElementsByTagName('head')[0]).appendChild(script);}});_scriptCache.set(url,promise);}returnpromise.then(()=>{if(global[name]){returnglobal[name];}else{thrownewError(`"${name}" was not created by "${url}"`);}});}
The name parameter is to ensure the API you're loading actually created the global variable you were expecting (e.g. Stripe creates window.Stripe) and it avoids some IDE warnings ("Stripe is undefined").
Reacted by Mathieu M-Gosselin, Seva Arkhangelskiy, Alexandros Katechis, Amir Sharif, Franco Gilio, Chuck, shymean, Vitalii Komenda, Jugal S, Anjaneya Sivan and 5 more
Reacted by Razvan Predescu, Zack Jackson, Marek Sierociński and Max
I want to know how a Webpack website application (with multiple entry points) can manage lots of different dependencies ... while also taking advantage of the fact that many people have jQuery and other popular libraries cached in their browsers because they've already visited sites that pulled them from CDNs
You can do this the same way you always would. Add
To the top of your page, and then use $ or window.$ in your bundle.
Why would I serve my own copies of popular libraries (e.g. bundled into the rest of my JS)? That seems like a waste of browser payload.
It's your choice. If you trust your CDN to always be available, don't bundle it. I personally put all my vendor/3rd party libraries in a CommonsChunk and perma-cache it. Our users only have to download this bundle once the first time they visit the site. Since it's a web-app, 99% of our users are recurring. If most of your users are new visitors, you might prefer a CDN.
But I also don't yet understand @sokra 's #150 (comment) recommending scriptJs.
Why can't Webpack manage them CDN dependencies?
Because it's not webpack's job to do that for you. Its purpose is to bundle local files, not manage CDNs. It's relatively easy to add a <script src="..."> to the top of your page, use scriptjs, or create a helper function like I did. There's no need to bake that into webpack.
What would that even look like? Supporting require('https://...') would lock up your browser while the script downloads because require is synchronous. require.ensure or import() are async and support could be added there, but it's just as trivial to create your own myExternalRequire.
Yes, most of my site's visitors are new and not recurring. Maybe I'm in the minority and starting to use Webpack was the wrong choice for me.
I'm surprised more Webpack fans aren't asking for this functionality. To me, managing dependencies is important, regardless of whether the code is hosted all together locally or if some of it sits on a CDN.
I'm not sure how to create my own myExternalRequire function to do what we're describing.
[I'm impressed that I even got Webpack working at all—it took me more than a full day to migrate my site over (to the Webpack way). The docs are super sparse and seem like they were written as reminders to the creators of Webpack rather than to introduce new developers to it.]
@ryancwalsh I posted my implementation just a few comments up. Webpack is difficult to get started with, I'll grant you that, but it's also quite flexible. I'm sure the maintainers wouldn't mind some help on the documentation side once you get it all figured out. There's lots of blogs/tutorials on the subject if you find the official documentation too sparse.
I just want my library code to be wrapped by webpackJsonp function (without all that webpack bootstrap) and then in a parent project i want to have import("path/to/external/library.js") which doesn't throw any "Module not found" errors.
However it seems like I am no longer able to import url's now.
I tried to figure out how to disable the warning only for certain imports, like if the expression started with certain letters like "url", or if I had provided a string literal that said "http", using the remaining properties. I couldn't figure it out unfortunately. If anyone else knows how, would be happy to know!
Ok, I found how to import a webpack bundle from another project. The file I load wraps everything inside a define()-call, which I just had to shim to get whatever what inside it.
I've written a loader that allows you to expose modules from one webpack build to others via a global defined namespace, this lets you define which modules you want to share between the webpack builds and allows you to share common modules while maintaining the bundles intact.
A possible workaround may be using __non_webpack_require__ that is is the original require function which exists in the environment before webpack comes in. See #1021 (comment)
In short: you can write a tiny script and load it with val loader to make webpack read the latest content from the CDN on every build and include it in your bundle, so you can treat it like any other normal dependency without having to vendor it into your repo.
Reacted by Peter Mellett, Ian Hunter and Pouya Sanooei
Activity
jhnns commentedon Jan 20, 2014
Mhmm... loading a script from another domain is definitely not a webpack task, since webpack is basically a bundler.
But maybe the script-loader should detect domain patterns? It's a bit complicated because the domain pattern should not conflict with a file path.
However, this discussion should be done in the script-loader's repo
sokra commentedon Jan 20, 2014
webpack is a module bundler not a javascript loader. It package files from local disk and don't load files from the web (except its own chunks).
Use a javascript loader, i. e. script.js
jhnns commentedon Jan 20, 2014
But the script-loader is loading the script from the home domain. Shouldn't it be possible to load stuff from another domain (given that the CORS-header are set correctly)?
sokra commentedon Jan 20, 2014
The script-loader doesn't really add a
<script>tag. It just evaluate some javascript in the global context.jhnns commentedon Jan 20, 2014
Ah ok...
kaste commentedon Jan 20, 2014
I just would have thought that using one of the loaders (script-loader seemed to be a good guess), it would work.
(From the user perspective it looks like, webpack (CLI, server) is a bundler and webpack-web is basically a loader.)
ThomasDeutsch commentedon Jan 23, 2014
Is it possible to use a named-AMD-module like 'jquery' or a chunk as an "external" chunk ?
I have 2+ webpack runtimes on one page that require jquery - so, i can not use the CommonsChunkPlugin, right?
Since webpack is a module bundler, would it somehow be possible to give one chunk a url of an external chunk or amd module?
sokra commentedon Jan 24, 2014
@ThomasDeutsch If you want to share chunks or common modules between multiple entry points, you need to use the CommonsChunkPlugin and compile all in a single webpack pass. Do this instead of multiple webpack runtimes. Check this.
m59peacemaker commentedon Nov 26, 2014
I wrote this simple loader, external-webpack-loader, to solve this problem.
Is there any reason this is not the appropriate solution? I just found webpack today, so let me know if I'm not thinking correctly. Thanks!
jhnns commentedon Dec 1, 2014
Sure, you can do that (and there are probably use-cases for it). But usually it defeats the purpose of a package manager...
m59peacemaker commentedon Dec 1, 2014
Ah! I see the light.
$scriptis the way to go since it takes care of downloading and evaluating the external script at runtime and keeps up with what you've already downloaded so that it won't download the same script twice. Makes sense. I think I had it in my head that webpack could do that, but it is a different task altogether.jbcpollak commentedon Dec 9, 2015
I was just looking for something like this but for fonts, not scripts. It doesn't seem common fonts are stored in npm, so rather than maintaining my own variants I was hoping I could convince webpack to download them.
@m59peacemaker - It looks like you removed your loader project, do you still have the source?
m59peacemaker commentedon Dec 9, 2015
@jbcpollak I haven't used webpack in a long time, so I don't know how fonts should work. My loader was bad and wrong, so I removed it.
simkessy commentedon Dec 22, 2015
Would it be possible to do something similar but with CSS. I'm assuming $scriptjs would only work with js files.
16 remaining items
shachar-i commentedon Oct 30, 2016
Thanks @nkbt , but if I use externals, I'll have to take care of my own dependencies (which is a major reason to start using Webpack to begin with).
This is actually what I did (use externals), but not the point I'm trying to make.
Since bundling npm installed packages is way nicer and more Webpacky, Isn't Webpack encouraging bloated bundles by not giving a good remote CDN option for 3rd party libs???
A half-lazy developer would just pick the easy big-bundle way. I supposed many did.
For example, I've looked at several react-webpack tutorials. All of them encourage 'npm install react' rather than the ugly external.
nkbt commentedon Oct 30, 2016
I understand where you are going here, but the thing is - CDN is an app concern, not bundler. Besides, there is no such thing as
good remote CDN option for 3rd party libsexcept maybe the recentunpkg(npmcdn). But for any reasonably loaded prod it makes sense to buy/setup your own CDN.I personally cannot come up with any reasonable answer to this. If you do - PR to docs would be a good point to start the discussion.
PS: mastering your tools is always a difficult process that takes heaps of time and errors. If it is possible to reduce this time - awesome
mnpenner commentedon Jan 29, 2017
I haven't thoroughly tested it yet, but you can use something like this to load an external script:
Usage:
The
nameparameter is to ensure the API you're loading actually created the global variable you were expecting (e.g. Stripe createswindow.Stripe) and it avoids some IDE warnings ("Stripe is undefined").ryancwalsh commentedon Mar 19, 2017
I totally agree with @shachar-i 's points above at #150 (comment).
I have spent 8 hours trying to learn Webpack today, and the docs do not clarify:
I want to know how a Webpack website application (with multiple entry points) can manage lots of different dependencies (which I thought was the main motivation for Webpack) while also taking advantage of the fact that many people have jQuery and other popular libraries cached in their browsers because they've already visited sites that pulled them from CDNs, such as https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js.
Why would I serve my own copies of popular libraries (e.g. bundled into the rest of my JS)? That seems like a waste of browser payload.
But I also don't yet understand @sokra 's #150 (comment) recommending scriptJs.
Why can't Webpack manage them CDN dependencies?
What am I misunderstanding?
Thanks, everyone!
mnpenner commentedon Mar 24, 2017
@ryancwalsh
You can do this the same way you always would. Add
To the top of your page, and then use
$orwindow.$in your bundle.It's your choice. If you trust your CDN to always be available, don't bundle it. I personally put all my vendor/3rd party libraries in a CommonsChunk and perma-cache it. Our users only have to download this bundle once the first time they visit the site. Since it's a web-app, 99% of our users are recurring. If most of your users are new visitors, you might prefer a CDN.
Because it's not webpack's job to do that for you. Its purpose is to bundle local files, not manage CDNs. It's relatively easy to add a
<script src="...">to the top of your page, usescriptjs, or create a helper function like I did. There's no need to bake that into webpack.What would that even look like? Supporting
require('https://...')would lock up your browser while the script downloads becauserequireis synchronous.require.ensureorimport()are async and support could be added there, but it's just as trivial to create your ownmyExternalRequire.ryancwalsh commentedon Mar 24, 2017
@mnpenner Thanks for the response!! ☺
Yes, most of my site's visitors are new and not recurring. Maybe I'm in the minority and starting to use Webpack was the wrong choice for me.
I'm surprised more Webpack fans aren't asking for this functionality. To me, managing dependencies is important, regardless of whether the code is hosted all together locally or if some of it sits on a CDN.
I'm not sure how to create my own
myExternalRequirefunction to do what we're describing.[I'm impressed that I even got Webpack working at all—it took me more than a full day to migrate my site over (to the Webpack way). The docs are super sparse and seem like they were written as reminders to the creators of Webpack rather than to introduce new developers to it.]
mnpenner commentedon Mar 24, 2017
@ryancwalsh I posted my implementation just a few comments up. Webpack is difficult to get started with, I'll grant you that, but it's also quite flexible. I'm sure the maintainers wouldn't mind some help on the documentation side once you get it all figured out. There's lots of blogs/tutorials on the subject if you find the official documentation too sparse.
ryancwalsh commentedon Mar 24, 2017
@mnpenner Sweet--I don't know how I missed that. I'll check that out. Thanks!
bizm commentedon Apr 26, 2017
Is it possible to bundle library as a module for dynamic import (https://webpack.js.org/guides/code-splitting-async/#dynamic-import-import-)?
I just want my library code to be wrapped by webpackJsonp function (without all that webpack bootstrap) and then in a parent project i want to have import("path/to/external/library.js") which doesn't throw any "Module not found" errors.
wbern commentedon Jun 14, 2017
I want to be able to use System.import() to import some external url's that I only want resolved during runtime.
After scouring this page a bit: https://github.com/webpack/docs/wiki/configuration#automatically-created-contexts-defaults-modulexxxcontextxxx
However it seems like I am no longer able to import url's now.
I tried to figure out how to disable the warning only for certain imports, like if the expression started with certain letters like "url", or if I had provided a string literal that said "http", using the remaining properties. I couldn't figure it out unfortunately. If anyone else knows how, would be happy to know!
wbern commentedon Jun 14, 2017
Ok, I found how to import a webpack bundle from another project. The file I load wraps everything inside a
define()-call, which I just had to shim to get whatever what inside it.Edit: Turns out it was not that simple, but changing library to
umdmade things easier in the unmanaged bundle.MrFrankel commentedon Feb 25, 2018
I've written a loader that allows you to expose modules from one webpack build to others via a global defined namespace, this lets you define which modules you want to share between the webpack builds and allows you to share common modules while maintaining the bundles intact.
https://github.com/MrFrankel/share-loader
yvele commentedon Nov 27, 2018
A possible workaround may be using
__non_webpack_require__that is is the originalrequirefunction which exists in the environment before webpack comes in. See #1021 (comment)josiahruddell commentedon Feb 6, 2019
I posted a workaround here: https://stackoverflow.com/a/54559637/39013
This solution uses webpack's
noParseconfiguration to make a module that exports the nativerequirefunction to bundled code.pimterry commentedon Feb 7, 2019
It looks like there's a few different cases being discussed here, but if you're trying to require an external file and pull it in at build time (e.g. pull it from a CDN and include it in your bundle), I wrote a blog post on how to do that using val loader: https://httptoolkit.tech/blog/bundling-remote-scripts-with-webpack/
In short: you can write a tiny script and load it with val loader to make webpack read the latest content from the CDN on every build and include it in your bundle, so you can treat it like any other normal dependency without having to vendor it into your repo.