stackblitz / core

Online IDE powered by Visual Studio Code ⚡️
https://stackblitz.com
MIT License
10.29k stars 911 forks source link

Docs or tutorials how to use external resources\dependencies and import them #540

Open alexeyrv opened 6 years ago

alexeyrv commented 6 years ago

Hello team. I need some help with StrackBlitz and external javascript libs (current proj is Angular6\TS projects). Cant find any docs or blog posts how to use external dependencies. Here is StackBlitz editorail link to my own fork StackBlitz

I saw you talk about some possibilities how import js. Can you explain how import .js libs from the external deps? Similar links or simple filenames dont work.

  1. // try external dependenceis with same url - doesnt work
    import pdfjsLib from 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.489/build/pdf.js';
    console.log( "pdfjsLib: ", pdfjsLib );
    pdfjsLib.GlobalWorkerOptions.workerSrc =  'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.489/build/pdf.worker.js';

    Error: Can't find module: https://cdndelivr.net/npm/pdfjs-dist/build/pdf.js (@2.0.489)

  2. Also I try add to src/assets/ files and add it from component.

    import pdfjsLib from '../../assets/pdf';
    console.log( "pdfjsLib: ",pdfjsLib );
    pdfjsLib.GlobalWorkerOptions.workerSrc = '../../assets/pdf.worker.js';

    Error - "Can't find module: //stackblitz.com/tmp/appfiles/assets/pdf (https:)"

If I dont use file extension it tries get .ts and its clear. But how to use js libs I need out of NPM storage? For example my own libs.

Thanks in advance. Alex

eestein commented 5 years ago

I came here to ask the same thing... Any updates on this? We're developing a local project, won't be publicly available initially, and we'd like to use stackblitz as a playground area for the developers. So they could run and test the components. The library is not publicly available and we couldn't find how to work with that scenario.

Has anyone figured this out yet? I noticed the question is a tad bit old.

Kind regards,

Nyalothas commented 5 years ago

Just posting to see if there are any updates on this. I want to load some dependencies that are not on npm. 🤒

eestein commented 5 years ago

@Nyalothas the solution I implemented was:

  1. Generate an UMD minified file of my built library (let's call it xyz.js)
  2. Import that module into my app.module:
import { XyzModule } from '../../xyz';

It's working fine. Not ideal, I agree, but it works.

KooiInc commented 2 years ago

Not necessarily the best solution, but here's an idea.