3

What is the best practice to organise angular(1.5) components/modules when working with webpack and ES6?

Should every component, directives and services be exported as a separate module or a function and then in the main app registered as the main app module dependencies, directives or services?

So far its the only approach I saw in webpack tutorials. But in pure js angular examples every component, directive or service registers by itself to the main app module (from their own file where they are defined)

I wonder what is the most common approach to organise all the services components and other bits of application.

  1. With Webpack (app.js would have a very long list of components)

    //app.js import myDirective from './myDirective.js'; angular.module('app').directive('myDirective', myDirective) //myDirective.js export default function myDirective(){} 
  2. Without webpack (but not used in any of examples with webpack)

    //app.js angular.module('app', []); //myDirective.js angular.module('app').directive('myDirective', function myDirective(){ }) 
6
  • I would move your scripts into the body instead of the head. Here's a good post on why. Also I don't see that you've bound your app to the page anywhere. Here's a link to the angular docs for bootstrap Once you've bound your app to the page you should at least get some errors in the console to look at. Commented Oct 16, 2016 at 20:03
  • 1
    Most flexible approach is one Angular module per unit (directive/controller/service whatever), one file per Angular module. Having a single app module smells bad and also bad for testing. In the example above it will throw an exception anyway, because angular.module('app', []) wasn't defined beforehand. Commented Oct 16, 2016 at 20:28
  • @estus so if you define a service angular.module('app.myService', function(){}) do you whenever you use a dependency injection you use a module dependency for 'app.myService' as well? If it is a separate module per each file I would expect you want to have all services as dependencies as well? Isn't it a bit too much? Commented Oct 16, 2016 at 20:51
  • 1
    It is about maintaining a hierarchy of dependencies. I.e. app loads app.foo module, and app.foo module loads app.foo.component and app.foo.service module. Depending on the design some levels of nesting can be flattened. But in some cases they may be kept, e.g. app.foo may have run or config blocks which you may want to avoid when testing nested modules. It is a bit cumbersome but it pays off in the large scale. There's no such thing as 'best practice' but this one can be considered a good one. Commented Oct 16, 2016 at 22:07
  • I dont like the idea that i have to have a module dependency pretty much every time i use dependency injection (api etc) but i guess its the only option to make code modular and not coupled with the whole application. Maybe if IDE would auto import module on specifying injected dependency it wouldnt be a problem at all:) like class/anotation is autoimported in java when you type it in eclipse or intellij Commented Oct 16, 2016 at 22:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.