Add node module to ember CLI app

后端 未结 5 1793
孤独总比滥情好
孤独总比滥情好 2020-12-16 17:01

I would like to use this Node.js module https://www.npmjs.com/package/remarkable-regexp in my Ember-CLI application.

How do I make it available to the Ember applicat

5条回答
  •  醉酒成梦
    2020-12-16 17:09

    ember-browserify is a great choice for use in apps, and there is work being done to try to allow Ember CLI to import NPM packages without any extra help at all.

    However, if you're trying to make this work in both addons and apps you can take a slightly different approach, which is to manually modify the broccoli build chain to include your Node package.

    This is a quick example of how this can be done in an addon's index.js file:

    var path = require('path');
    var mergeTrees = require('broccoli-merge-trees');
    var Funnel = require('broccoli-funnel');
    
    module.exports = {
      name: 'my-addon',
    
      treeForVendor: function(tree) {
        var packagePath = path.dirname(require.resolve('node-package'));
        var packageTree = new Funnel(this.treeGenerator(packagePath), {
          srcDir: '/',
          destDir: 'node-package'
        });
        return mergeTrees([tree, packageTree]);
      },
    
      included: function(app) {
        this._super.included(app);
    
        if (app.import) {
          this.importDependencies(app);
        }
      },
    
      importDependencies: function(app) {
        app.import('vendor/node-package/index.js');
      }
    };
    

    A similar technique can be used for standard apps. Again, this method will be replaced soon as the Ember CLI team adds support for Node modules, so try to use it sparingly and keep up to date with Ember CLI!

提交回复
热议问题