Skip to content

Support for multiple "app" folders? #6887

Closed

Description

@akondo06

Hello,
I am currently porting an ember 1.12 app (with grunt as build tool) to ember 1.13.13 ( with ember-cli 2.12.0). I've done most of the files restructure (ES6 modules and stuff). Now the issue I have is that the app has two "app" folders. Difference being that the second one has only the components and routes that should be available just if I go via a different index.html file.
Project Structure:

client/
	app/
		services/
		models/
		pods/
		index.html
	app-admin/
		services/
		models/
		pods/
			components/
			admin/ (basically for administration)
		index.html
	ember-cli-build.js

My solution for this was to have 2 EmberApps in ember-cli-build.js and then chain the trees (and then include the generated "client.js" asset from the first app into the index of the second one).
But I found out I cannot tell EmberApp what folder to look into for the app files.
Or that if ember itself should be included in the resulted "client.js".

I looked into ember-cli/lib/broccoli/ember-app.js and made changes to fit my needs.

function "appOptions" just gives back babel options
var app = new EmberApp(defaults, appOptions());

var appAdmin = new EmberApp(defaults, appOptions({
	baseAppDir: 'app-admin',
	appOnlyJS: true,
	outputPaths: {
		app: {
			js: '/assets/client.admin.js',
			html: 'index.admin.html',
			css: {
			  'app': `/assets/client.admin.css`,
			}
		},
		vendor: {
			js: '/assets/vendor.admin.js'
		}
	}
}));

let dependencies = [
	...,
];

dependencies.forEach(dependency => {
	app.import(dependency);
	appAdmin.import(dependency);
});

return app.toTree(appAdmin.toTree());

Notice the baseAppDir and appOnlyJS options.
I can create a pull request if this is the way .. but maybe there's a more elegant solution?

Activity

  1. akondo06 commented on Mar 22, 2017

    @akondo06
    Author

    After reading up the ember-app class more .. refactored to only one option additionalAppFolders: ['app-admin'] .. instead of the baseAppDir and appOnlyJS. Now I just merge the passed 'app-admin' folder with appTree. Simpler solution but maybe have something similar to outputPaths called inputPaths` so you can pass the project folders as such:

    {
    	inputPaths: {
    		app: ['app', 'app-admin']
    	},
    	outputPaths: { ... }
    }
    
  2. RobbieTheWagner commented on Mar 22, 2017

    @RobbieTheWagner
    Contributor

    @akondo06 are these truly separate apps or could they be merged into one app folder instead? You could use an in-repo-addon or engine to separate them, but unsure if that will work with Ember 1.13.

  3. akondo06 commented on Mar 22, 2017

    @akondo06
    Author

    @rwwagner90 they have to be separated. Reason being that the second one has extra components, services and models that can be viewed only by administrators (accessed by index.admin.html instead of index.html).
    It adds up a bit of security there.

    I already looked at engines but it will take a bit more time to refactor than what I have.
    I will not stop at v1.13.13 but because things get cleaned up in v2.0.0, I have to ged rid of all deprecations before that. After v2, things will go smooth.

    Back to my problem.
    After better understanding EmberApp, I figured that I can just merge the second folder into the app tree before calling app.toTree().

    const mergeTrees = require('ember-cli/lib/broccoli/merge-trees');
    const WatchedDir = require('broccoli-source').WatchedDir;
    .....
    let app = new EmberApp(...);
    let appAdmin = new EmberApp(....);
    let additionalAppFolders = ['app-admin'];
    if(additionalAppFolders.length) {
    	let additionalAppTrees = additionalAppFolders.map(folder => {
    		return new WatchedDir(appAdmin._resolveLocal(folder));
    	});
    	appAdmin.trees.app = mergeTrees([appAdmin.trees.app].concat(additionalAppTrees), {
    		overwrite: true,
    		annotation: 'TreeMerger (app with additional app folders)'
    	});
    }
    ....
    return app.toTree(appAdmin.toTree());
    

    Only downside that I can see now is the vendor.js file (which is the same for both) being built twice.

  4. rwjblue commented on Mar 23, 2017

    @rwjblue
    Member

    That looks pretty close to what I would think also, but don't merge them at the end like that.

    Slight modification:

    let adminAppTrees = [
      new WatchedDir('app'),
      new WatchedDir('app-admin'),
      // whatever other trees/folders here
    ];
    
    let adminApp = mergeTrees(adminAppTrees, {
      overwrite: true,
      annotation: 'TreeMerger (app with additional app folders)'
    });
    
    let app = new EmberApp(defaults);
    let adminApp = new EmberApp(defaults, {
      trees: {
        app: adminApp
      }
    });
    
    let outputTrees = [
      new Funnel(app.toTree(), {
        destDir: 'base-app',
        annotation: 'Funnel: base-app output'
      }),
      new Funnel(adminApp.toTree(), {
        destDir: 'admin-app',
        annotation: 'Funnel: admin-app output'
      })
    ];
    
    return mergeTrees(outputTrees);
  5. rwjblue commented on Mar 23, 2017

    @rwjblue
    Member

    I agree that it is slightly sub-optimal to duplicate the final vendor.js output file, but it doesn't seem like it should be terribly inefficient (we already dedupe addon's treeFor hooks when versions match and whatnot).

    I think the above is a decent solution to the problem, so I'm going to close this. I'm happy to continue the discussion though...

  6. akondo06 commented on Mar 23, 2017

    @akondo06
    Author

    Thanks @rwjblue will give that a shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions