2

I am about to write a common Angular module which can be used as a plug and play module in my application.

What I want

I want to put the module definition and all its factory, directive, constants in a single js file, example:

angular.module('commonModule', [])
.factory(...)
.directive(...)
.constant(...)

Why because

Whenever a developer wants to use my module he/she just need to call a single js file and just need to inject my module in their module.

What is my problem

I have read the John papa's Angular 1 good practice style guide, he told that it is good to define a single component in a file, but here I am doing the opposite so what should I do?

Arpit Kumar
  • 2,179
  • 5
  • 28
  • 53
  • Are you publishing this as a package? The one file reccomendation is for when you have some sort of builder like webpack or gulp etc. if this is to be included in another project then yea. Concatenate. – Phix Jan 13 '17 at 06:44
  • Keep them in separate files as per the style guide when you are developing the module. Have you thought about maybe bundling the module in a single file? – Prinay Panday Jan 13 '17 at 06:45
  • @_phix I am not going to publish the module it will be used for a single project only. And I am not using any builder we are simply going to compress the code using gulp. – Arpit Kumar Jan 13 '17 at 06:48
  • As in minify all the js files into one file? If that's the case then they will only have to inject the module and be free to use any of it – Prinay Panday Jan 13 '17 at 06:50
  • if you have a little code you can put everything in the same file. but if you have like 500 lines+ per controller and a lot of controller better to make a file per controller, a file for service , etc... it will be easier to find a function – AlainIb Jan 13 '17 at 06:51

1 Answers1

1

Separate. Just so and not otherwise.

Keeping components separately is one of the best practices in programming world. No matter if you have few lines of code at the moment, later code will increase. Doing so from the beginning you cultivate a literate programming discipline. Bad practice to come up with a bunch of exceptions in the rules, like "if I have few lines of code I can write all components of my app in one single file". It makes rules and possibility to follow them more complicated.

Whenever a developer wants to use my module he/she just need to call a single js file and just need to inject my module in their module.

Correct. For consumer it is very convenient to include just one single file in his/her project. But it is bad for code maintenance. So just use any JS bundler to concatenate all dependencies in one result file so consumers can just inject it in their module.

GProst
  • 9,229
  • 3
  • 25
  • 47