ember-cli-node-assets icon indicating copy to clipboard operation
ember-cli-node-assets copied to clipboard

How do you import a npm dependency into an ember file with having it declared globally?

Open ssylvia opened this issue 8 years ago • 1 comments

I'm attempting to use the CUID id generator library. I am able to import the library into the vendor folder with this addon, but everything I try loads the load the library as a global variable.

nodeAssets: {
  cuid: {
    srcDir: 'dist',
    import: ['browser-cuid.js'],
  },
},

It looks like the library uses UMD: https://github.com/ericelliott/cuid/blob/master/dist/browser-cuid.js#L102. Do I need to do anything else to make it work with

import cuid from 'cuid';

const myId = cuid();

instead of

const myId = window.cuid();

ssylvia avatar Jun 02 '17 12:06 ssylvia

Hi @ssylvia,

It looks like the library uses UMD: https://github.com/ericelliott/cuid/blob/master/dist/browser-cuid.js#L102.

That doesn't actually look like standard UMD to me — I'm not sure what module format that app.register call aligns with, but AMD (which is the arm of a UMD setup that would work with Ember's loader.js) would have a define() call somewhere.

Do I need to do anything else to make it work with

import cuid from 'cuid';

const myId = cuid();

To make the import call work rather than relying on the global, you can use ember generate vendor-shim cuid. Based on your snippet, I think the generator output should give you exactly what you need without any tweaking 🙂

dfreeman avatar Jun 02 '17 14:06 dfreeman