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

Can't require absolute URLs, e.g. from CDN #2421

Closed
dandv opened this issue Dec 16, 2018 · 8 comments
Closed

Can't require absolute URLs, e.g. from CDN #2421

dandv opened this issue Dec 16, 2018 · 8 comments

Comments

@dandv
Copy link
Contributor

dandv commented Dec 16, 2018

🐛 bug report

Even after pulling the fix for #2358,

require('https://...cdn_path.../module.min.js') still fails with:

Failed to install https

What am I doing wrong? Failing to load CDN libraries seems like a blatant omission.

🎛 Configuration (.babelrc, package.json, cli command)

Zero config.

🤔 Expected Behavior

Server launches.

😯 Current Behavior

$ node ./parcel-bundler/bin/cli.js index.html
Server running at http://localhost:1234 
yarn add v1.12.3
error Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
🚨  /home/dandv/parcel/packages/core/index.js:1:19: Failed to install https:.
> 1 | window.$ = require('https://code.jquery.com/jquery-3.3.1.slim.min.js');
    |                   ^
  2 | 

🔦 Context

I'm trying to require from CDN a library I don't want to host myself.

💻 Code Sample

index.html:

<html>
<body>
<script src="index.js"></script>
</body>
</html>

index.js:

window.$ = require('https://code.jquery.com/jquery-3.3.1.slim.min.js');

🌍 Your Environment

Software Version(s)
Parcel commit 7a540fc
Node 11.3.0
npm/Yarn 6.5.0
Operating System Ubuntu Linux 16.04
@devongovett
Copy link
Member

The require function is used to load common js libraries, e.g. local files or node_modules. Loading urls is not supported.

@dandv
Copy link
Contributor Author

dandv commented Dec 17, 2018

Thanks for clarifying that about require, but import 'https://...' doesn't work either. Just replace index.js with:

import jquery from 'https://code.jquery.com/jquery-3.3.1.slim.min.js';
console.log(jquery);

@devongovett
Copy link
Member

yeah we currently don't support that since it would require a runtime to actually load and execute the remote module.

@dandv
Copy link
Contributor Author

dandv commented Dec 17, 2018

Is the only way to import modules from URLs via dynamic imports? I've submitted a docs PR to mention that (related - #2422).

@devongovett
Copy link
Member

Well kinda. That might work for browsers that support dynamic import syntax, but not older ones. Parcel will just ignore dynamic imports of urls, so it won’t work in browsers that don’t support it.

@dandv
Copy link
Contributor Author

dandv commented Dec 17, 2018

Right, I should've specified that in the PR. Another problem is that when the library host doesn't set CORS headers, dynamic imports won't work. While I agree this is an issue that should be solved by the library host, are there plans for Parcel to offer an alternative loading method? I've used this successfully:

function requireJs(url, module) {
  const script = document.createElement('script');
  return new Promise(resolve => {
    script.src = url;
    script.onload = resolve;
    if (module) script.type = 'module';
    document.head.appendChild(script);
  });
}

@dandv
Copy link
Contributor Author

dandv commented Aug 24, 2019

Any updates on supporting imports from absolute URLs?

🚨  .../index.mjs:9:23: Failed to install https:.
   7 |  */
   8 | 
>  9 | import Highcharts from 'https://code.highcharts.com/js/es-modules/masters/highcharts.src.js';
     |                       ^

@fregante
Copy link
Contributor

fregante commented Jul 16, 2022

Any updates on supporting imports from absolute URLs?

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