ember-cli-node-assets
ember-cli-node-assets copied to clipboard
How do you import a npm dependency into an ember file with having it declared globally?
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();
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 🙂