Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Make unused public methods private in services
Domenik Reitzner
Domenik Reitzner

Posted on

     

Make unused public methods private in services

We have finally reached the end of the line (maybe 😋).

Building upon the shoulders of our past code

We have already invested a lot of time and afford into streamlining our code bundle.

In this final puzzle piece we will look at all our services and see if we find any methods and members that we only use privately.

Gather all the used services methods

This is pretty similar to searching for used methods in templates from the last article. Instead of looking at hmlt files we will search in all of our js files.

Also the regex (at least in my case) changed a bit.
I now search for all strings that start with a big letter followed with at least one small letter. The whole thing needs to be prefixed with " " (inside none class syntax files), "_" (normal class service injection), or "!" (negated none class syntax).

constmatches=string.match(/[ |_|!][A-Z]+[a-z]+[a-zA-Z]*\.[a-zA-Z0-9_-]+/gm);
Enter fullscreen modeExit fullscreen mode

Replacing that and adapting it to use a different Set as before, we end up with:

conststringsFromJs=newSet();consttGatherFromJs=()=>{returngulp.src(packagePath+'/**/*.js',{base:'./'}).pipe(through.obj((file,_,cb)=>{conststring=file.contents.toString();constmatches=string.match(/[ |_|!][A-Z]+[a-z]+[a-zA-Z]*\.[a-zA-Z0-9_-]+/gm);if(matches)matches.forEach((match)=>stringsFromJs.add(match.substring(1)));cb();}));};
Enter fullscreen modeExit fullscreen mode

Oh wait, we can reuse or existing function

As we already loop over all our js files (tFindUnusedPublic) we can just add our service check.

consttFindUnusedPublic=()=>{conststringToSearchT=[...stringsFromTemplate].join('');conststringToSearchJs=[...stringsFromJs].join('');// get public from js filesreturngulp.src(packagePath+'/**/*.js',{base:'./'}).pipe(through.obj((file,_,cb)=>{// ... some stuff we already do// components and directicesletbaseName=string.match(/controllerAs: '(.*)'/);if(baseName){/*...*/}// servicesletserviceName=string.match(/\.service\('(.*)'/);if(serviceName){serviceName=serviceName[1];constuniquePublic=getPublic(string);string=searchInString(uniquePublic,serviceName,stringToSearchJs,string,false);}// ... more some stuff we already do})).pipe(gulp.dest('./'));};
Enter fullscreen modeExit fullscreen mode

So with this little addition we now also check our services for unused methods and members.

Ideally our compiled code base should be smaller now (I saved about 100kB) and our users should be a bit more happy.

I hope that you where also able to squeeze a little more performance out of your angularjs project.

All the best and enjoy frontending 😉

happy coding🎉

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Christian, husband, father of four, web-enthusiast, Sveltian, musician and blogger.
  • Location
    Vienna
  • Education
    School for mechatronics with emphasis on robotics
  • Work
    Senior Frontend Developer @ woom
  • Joined

More fromDomenik Reitzner

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp