Is there a way to re-export addon components automatically?

Hi, is there a way to re-export all the addon components/services/… automatically from the app folder?

We have quite a few addons and it is very annoying to declare those re-exports all the time. We would like to migrate our addons from pods layout to classic and this would require us to move all of the re-exports again.

Thanks for any help.

The only way I’m aware of is to actually move the files from addon/ into app/ thus sidestepping the need for re-exports entirely. Though I wouldn’t recommend that. It’s possible you could also write some sort of broccoli or other build plugin that would automatically re-export all of your components but that might be more trouble than it’s worth. And lastly of course you could just write a script to do the bulk of the migration for you. That’s all I can think of off the top of my head but maybe someone else has more ideas.

It requires very little code to do this automatically as a broccoli transform. I tested this and it works. The addon I tested it against uses typescript, so it has some extra bits to deal with that, you can skip some steps if everything is JS only.

Delete your app folder and put this into your addon’s index.js:

treeForApp() {
    // add this as a dependency (not devDependency) of your addon
    let stew = require('broccoli-stew');

    // grab all js and ts files from the addon directory
    let addonFiles = stew.find(this.treeGenerator('addon'), '**/*.+(js|ts)');

    // rewrite them into reexports
    let reexports = stew.map(addonFiles, (content, relativePath) => {
      let pathWithoutExtension = relativePath.replace(/\.[tj]s$/, '');
      return `export { default } from "${this.name}/${pathWithoutExtension}";`;
    });

    // rename any that were TS into JS, because we shouldn't use typescript in
    // our app tree.
    return stew.rename(reexports, '.ts', '.js');
  },

The downside of this approach is that you aren’t controlling which things get reexported. If you have private modules inside your addon, they all get reexported, unless you update this code to filter them out.

3 Likes