2015-07-27 12 views
14

Stavo usando ember-browserify per trovare i moduli npm nelle mie app ember-cli, ma per qualche motivo non funziona con i componenti aggiuntivi di ember-cli.Come trovare il modulo npm all'interno del componente add-on di ember-cli

Quindi la mia domanda è: c'è un altro modo per importare i moduli npm in un addon di ember-cli?

Edit:

Così ho potuto non importare il modulo NPM, ma ho trovato che il modulo specifico ho voluto importare è anche una componente Bower, così ho installato come così e importati tramite il index.js come quindi:

included: function(app) { 
    this._super.included(app); 

    app.import('bower_components/dropzone/dist/dropzone.js'); 
} 

e che funzionava. Fare questo con node_modules non era possibile. Fa schifo che sia così difficile importare i moduli npm su un add-on di embone-cli.

risposta

4

ember-fetch esegue questa operazione. Il codice è un po 'complessa:

treeForVendor: function(tree) { 

    // Get the path of whatwg-fetch in node modules 
    var fetchPath = require.resolve('whatwg-fetch'); 

    // use utility function to expand it into a pattern 
    var expandedFetchPath = expand(fetchPath); 

    // rename all the files in a tree containing files matching the pattern 
    var fetch = rename(find(expandedFetchPath), function(path) { 
    return 'whatwg-fetch/fetch.js' 
    }); 

    // apply a template to each file in the tree and merge the trees 
    return mergeTrees([ 
    new Template(fetch, templatePath, function variables(content) { 
     return { 
     moduleBody: content 
     }; 
    }) 
    ]); 
}, 

included: function(app) { 
    this.app = app; 
    this._super.included(app); 

    // import the tree created above in treeForVendor 
    app.import('vendor/whatwg-fetch/fetch.js', { 
    exports: { 
     default: [ 
     'default', 
     'Headers', 
     'Request', 
     'Response' 
     ] 
    } 
    }); 
} 

Tratto da https://github.com/stefanpenner/ember-fetch/blob/master/index.js

Spero che questo aiuti.

+1

Im non seguire ... Potrebbe spiegare meglio? – FutoRicky

+0

Come ho già detto, il codice è un po 'complesso. Aggiungerò alcuni commenti da seguire, ma ci vorrà del lavoro per adattarsi. – Gaurav

+0

Quindi, fondamentalmente installo ember-fetch e il modulo nodo viene importato? O devo installarlo e quindi aggiungere questo codice al mio index.js del mio addon? – FutoRicky

1

Nell'addoc, aggiungere l'importazione alla versione app/ dell'oggetto (che in genere esporta solo l'oggetto).

Nelle app che utilizzano l'addon, installare sia ember-browserify che il modulo npm.

Ad esempio, nel app/models/user.js in un addon:

import TimezoneDetect from 'npm:jstimezonedetect'; 

export { default } from 'common/models/user'; 

Vedi https://github.com/ef4/ember-browserify#using-ember-browserify-in-addons