Ember 添加文件到根目录的插件

Ember addon to add files to root directory

我有一个插件需要将一组 JS 文件从他们的 bower 目录复制到 Ember 应用程序的 /dist 根目录(这是用于与服务工作者关联的范围规则)。我想也许我可以使用 treeForApp 挂钩,但虽然我没有收到任何错误,但我也没有得到想要的结果。

index.js是:

const Funnel = require('broccoli-funnel');

module.exports = {
  name: 'ember-upup',
  treeForApp: function(tree) {
    tree = new Funnel(tree, { include: 
      [ 
        'bower_components/upup/dist/upup.min.js',
        'bower_components/upup/dist/upup.sw.min.js' 
      ]});

    return this._super.treeForApp.call(this, tree);
  },

Note: I thought I might be able to solve this problem by simply copying the javascript files as part of index.js postBuild hook but while this DOES put the JS files into the dist folder's root it is not served by ember-cli's ember serve apparently if not pushed through one of it's build pipelines.

Stefan Penner has now pointed out the the dist directory is for developers to look at but serving is actually done within the tmp directory structure ... this explains why my "hack" didn't work.

看来我最初的尝试并不完全遥远。为了让它工作,你需要像这样挂入 treeForPublic 挂钩:

const path = require('path');
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
const JS_FILES = ['upup.min.js', 'upup.sw.min.js'];

module.exports = {
  treeForPublic: function() {
    const upupPath = path.join(this.app.bowerDirectory, 'upup/dist');
    const publicTree = this._super.treeForPublic.apply(this, arguments);
    const trees = [];
    if (publicTree) {
      trees.push(publicTree);
    }
    trees.push(new Funnel(upupPath, {
      include: JS_FILES,
      destDir: '/'
    }));

    return mergeTrees(trees);
  }   
}

希望对您有所帮助。