Skip to main content

Command Palette

Search for a command to run...

How to obfuscate production builds of your Angular app

Published
1 min read

Let's look at how to obfuscate production builds of your Angular web app.

EDIT: reword article title to avoid confusion :)

EDIT2: fixed webpack config.

You will need an Angular project v10 or later to continue.

We will use javascript-obfuscator for this tutorial.

Installation:

  • Install custom Angular builder which supports Webpack config and obfuscator packages.
npm i -D @angular-builders/custom-webpack javascript-obfuscator webpack-obfuscator
  • Now, update let's update angular.json to use the above installed Angular builder.

BEFORE:

"architect": {
  "build": {
     "builder": "@angular-devkit/build-angular:browser",
        "options": {
           ...
        }
     }
}

AFTER:

"architect": {
  "build": {
        "builder": "@angular-builders/custom-webpack:browser",
        "options": {
           ...,
            "customWebpackConfig": {
              "path": "./webpack.config.js",
              "mergeRules": {
                "externals": "replace"
              }
        }
     }
}
  • Later, create a webpack.config.js at the root of the project directory (beside angular.json).
// webpack.config.js
const JavaScriptObfuscator = require('webpack-obfuscator');
module.exports = (config, options) => {
  if (config.mode === 'production') {
    config.plugins.push(new JavaScriptObfuscator({
      rotateStringArray: true, // please customizable with options
    }, ['exclude_bundle.js']);
  }
}

More options can be found here.

  • Finally, run a production build with npm run build and compare the output JS files with older ones.

Hope you learned something valuable here. Feel free to @ me on Twitter here

C

i have the follow error

An unhandled exception occurred: Cannot find module 'node:assert'

S

Hi, can you try using a newer version of NodeJS like v16.16.0?

C

Surya Teja Karra i will try, thanks!!

1
S

your webpack.config.js snippet doesn't even compile.

Did you at least try it before writing this blog ??

S

Hey, I've fixed the webpack config. It was missing a } at the end. Give it a try now :)

S

and the trailing ';'

anyway, still not working, gives me this error :

$ ng build --prod --progress=false Options: --version Show version number [boolean] -s, --source A path (relative to the working directory) of the node_modules folder to process. [default: "./node_modules"] ... --help Show help [boolean]

Unknown arguments: use-program-dependencies, useProgramDependencies

An error occurred during the build: Error: NGCC failed. at NgccProcessor.process (C:\Users\466367\AppInternes\cva\CVA-front\src\main\webapp\node_modules\@ngtools\webpack\src\ngcc_processor.js:129:19)

It's ike it's running webpack with unknow arguments...

D

Beware the language barrier! To “obfuscate” something means to make it far less clear or even unclear, which in context is not a good outcome.

Certainly here in the text he context is clear , here obfuscation is as a facet of bundling the code. However looking at the title of the article initially I assumed it to be a warning on writing unclear code.

Usually such titles don’t bother me but this one really sticks out as I believe it needs a small change to ensure what lies within.

It’s a smart article of course no issue there but I felt also it was lacking in intention as the title put me off somewhat 😂

1
S

Hey thanks for informing. I've reworded the title.

D

Surya Teja K

I think you need to try to find an alternate word than obfuscate, in this context it’s very misleading , perhaps the term you are attempting to illustrate is “uglify” this is what usually happens in bundlers

More from this blog

Blog | Surya Teja K

41 posts