r/angularjs • • Jun 02 '17

What a Large AngularJS Application Looks Like at Scale

https://papers.landinglion.com/actually-building-a-large-angularjs-app-at-scale-f26ef1f3f3b1
44 Upvotes

29 comments sorted by

16

u/karathos Jun 02 '17

you should highly consider migrating to 1.5-1.6. this would enable you to do 'newer' angularjs development using components over directives/loose controllers.

you don't have to necessarily rewrite everything, but doing newer work in the latest version means less burden should you choose to move to Angular over AngularJS in the future.

4

u/hunterbrennick Jun 02 '17

Thanks for the feedback. It's definitely something we have considered and still consider. From your personal experience, what are the pros/cons between directives and components?

4

u/karathos Jun 02 '17

I cannot speak for how your app is structured and thus I can't really give a one-size-fits-all solution.

back when I worked on an angular 1.x app, most of my directives were really written like components to begin with. it was a glorified template with inputs that would be bound to scope, and most bindings were as simple as changing the = to < for one-way binding.

it might be a good idea to go over todd motto's es2015 angular style guide and see what parts might work for your app and your team. be forewarned, it uses a lot of the newer features in the language, so if you haven't followed up on newer javascript language specs for the past 3-4 years then a lot of it might be foreign to you.

2

u/hunterbrennick Jun 02 '17

Hmm, the "supported properties" for components look identical to directive properties. Are components essentially syntax sugar on top of directives?

6

u/karathos Jun 02 '17 edited Jun 02 '17

pretty much yes. they have isolate scopes, automatically use controllerAs syntax ($ctrl), and theres no more link function. i'm taking a wild guess here, but due to the sheer number of directives you have, you're probably writing a lot of "UI directives" or "template directives" as todd calls them, which is a pretty standard thing to do pre-ng1.5. these should ideally be components. it conveys the intent of the code a lot better.

from todd's style guide on directives:

The usage of these should be carefully considered now that .component() exists. Directives should not declare templates and controllers anymore, or receive data through bindings. Directives should be used solely for decorating the DOM. By this, it means extending existing HTML - created with .component().

0

u/hunterbrennick Jun 03 '17

We use controllerAs all the time in angular 1.3.x. and I like how I explicitly declare it (not a huge fan of implicit things happening behind the scenes). I also like being able to take advantage of preLink and postLink directive functions - post when I need. Can be used to speed up directive compile and init time.

3

u/karathos Jun 03 '17

postLink still exists as a lifecycle hook ($postLink). depending on what you were doing in prelink, you might be able to swing it in $onInit instead.

one thing to keep in mind is that components aren't a full replacement for directives. directives are now meant to be manipulating existing DOM, so if you need certain linking functions then it makes sense to keep them as directives. components are meant to replace template-driven directives.

i'd be interested to read about what kind of optimizations you are doing in pre/post-link. maybe an idea for a new article ;)

2

u/[deleted] Jun 03 '17 edited Jun 03 '17

Pretty much. Directives that use templates are essentially components in 1.5.x-1.6.x. in my experience, the syntactic sugar is less important than the conceptual changes that come with it. Using controllerAs style props instead of relying on scope as a super service helps eliminate side effects. If at all possible, avoid using $scope to hold state. It leads to tightly coupled, brittle code, despite appearing to be decoupled. This also helps lead you to idempotent functions that are much more easily tested.

Edit: didn't read the other comment until I spoke my mind. It's a better answer that echoes my feelings.

0

u/hunterbrennick Jun 03 '17

We use controllerAs all the time in angular 1.3.x. I like explicitly declaring it.

3

u/ell0bo Jun 02 '17

Components are the nouns, directives are the adjectives. It's really the way I try to explain it. When you combine components with webpack, it really make organizing the site much simpler

2

u/MattBlumTheNuProject Jun 03 '17

For sure move to ES6 with 1.5+. We're writing a very large app in 1.6.3 right now and the code is clean. Very few "what the fuck are we doing here" moments.

2

u/ThunderTherapist Jun 03 '17

Don't components have controllers?

4

u/CarpetFibers Jun 03 '17

How are you delivering the app? Is everything bundled up (i.e., Webpack) or are you doing any kind of lazy loading? We recently abandoned Angular 1.5 in favor of Angular 4 because of the built-in lazy loading, among other reasons. I'd be curious what performance optimizations you've made to account for the sheer size and breadth of your application.

3

u/[deleted] Jun 03 '17

He mentions the build tools in the article but it looks like grunt mainly. Another article on that coming up though

3

u/hunterbrennick Jun 03 '17

We use npm and bower for package managers. We use Grunt tasks (in Gruntfile.js) and Yeoman scaffolding (in our index.template.html file) for building and deploying. We have 7 Grunt build tasks ('local', 'exp', 'dev', 'test', 'stage', 'demo', and 'prod') and 5 grunt deployment tasks ('dev', 'exp', 'prod', 'lower', and 'all'). Like /u/intrepid_i mentioned, I will get into more detail here in a further article. We have iterated a lot on our AngularJS web app build/deployment process over the last 5 years.

1

u/highmastdon Oct 10 '17

kind of lazy loading

He's asking how you deliver the code to the browser. Is it lazy loading on features, modules or otherwise to make sure the client doesn't need to download 5 MB on start, and has to wait for the browser to parse and compile.

8

u/[deleted] Jun 02 '17

As someone with also a large Angular app on the wild I'm so happy to see other still on Angular 1.x , I use 1.5 and I love it!

5

u/eggnaramoose Jun 02 '17

It's refreshing to see that not only are there companies using AngularJS at scale, but also are giving back to Angular community. Great post. Keep up the good work :)

2

u/highmastdon Oct 10 '17

[x-post r/webdev:] So I'm reading this:

“Organizing by feature” makes it difficult to reuse html, directives, services, and factories across feature directories [...] we have found it takes a longer time to locate a specific JavaScript file in a directory that has dozens of html and JavaScript files versus one with only JavaScript files.

Looking at the file structure he proposes, there's not much difference between the "Organizing by feature" version. Especially when he's arguing that the other structure makes it difficult to reuse html, directives, [etc] it seems that he missed the point of "Organizing by Feature". You should not put reusable parts in the "feature" directories. That's what the shared directory that is for. And it has its own directory structure, based on the feature a directive, service, or factory delivers.

I'm really not seeing how you get 1000s of files in one directory when you're separating by feature. It's all about the granularity and the naming of features, that determines in which directory they land.

Of course, if you have "home", "login" and "application" as your features, then you end up having a next to empty "home" directory and a completely overflowing "application" directory. And then trying to reuse components in the different directories becomes pretty hard when you don't put stuff in an orderly fashion in the "shared" directory

0

u/[deleted] Jun 02 '17

[deleted]

6

u/hunterbrennick Jun 02 '17

No concerns currently. To us the split from 1 to 2+ is similar to what happened in the Python community with 2.x and 3.x.

Angular1 works perfect for us currently and we don't see how we would benefit from switching to 2. I think a large motivation for Angular2+ is to make the learning curve easier for people starting out. The Angular1's learning curve is known to be steep.

2

u/pr0fess0rx29 Jun 03 '17

I would recommend sticking with AngularJS 1 for your system. Angular 2/4 really is a totally different framework. They should really have given it another name. We have several systems running in angular 1 and now a few running in angular 2 and we would never even dream about converting the angular 1 apps to angular 2.

2

u/pr0fess0rx29 Jun 03 '17

I should also mention though that we did do a major overhaul on our angularjs 1 apps after discovering John Papa's angularjs coding guidelines. Most likely you've already seen it, but if not I would recommend it.

https://github.com/johnpapa/angular-styleguide

2

u/hunterbrennick Jun 03 '17

Yup! We certainly have drawn influence from john papa's guidelines :)

8

u/karathos Jun 02 '17

why the 1.x community is much larger.

because Angular (2.0) hasn't been around as long as AngularJS, there's significantly less resources when you compare the two. you'll find more developers who know ng1 over ng2, more knowledge on stack overflow, more tutorials, more examples, more third party libraries...etc.

0

u/[deleted] Jun 02 '17

[deleted]

3

u/karathos Jun 02 '17 edited Jun 02 '17

I imagine if the adoption rates continue, given enough time, there will be more ng2 resources and you'll see more teams choose ng2 over ng1.

8

u/[deleted] Jun 02 '17 edited Jun 03 '17

[removed] — view removed comment

1

u/hunterbrennick Jun 02 '17

Fair. Different strokes for different folks... I am in the process of writing another article that will go into more detail about our file naming and organization patterns. I think that might provide further clarity on the subject, our patterns might align closer than you think.

The file naming and organization patterns we use now, like I said in the article, is the result of countless iteration (we too tried styles in a totally separate folder tree). It is also strongly related to how we index files within our application. We try to always opt for the pattern that promotes the fastest indexing.

2

u/Switche Jun 03 '17

I can second this, although I also know the pain of "by feature" making its own mess. By feature isn't quite structured enough on its own, and many devs I've worked with struggle to find files in the hierarchy without find by filename. It's less friction, but enough that it can slow you down and slowly diverge in predictable structure.

I still much prefer this. It would be nice to have a plug-in or something that loads an entire feature on the index.js load or something.

On the topic, in case it helps anyone, or if anyone has better advice, we use browserify to bundle, and eventually the "relative path hell" of imports was too much to bear, so we started adding each module path to the "files" option in our bundle task, so we can just import by name, or if we just want to import a single dep from the module, a relative path from the module: import 'myModule/myFactory';