How to obfuscate production builds of your Angular app
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.jsonto 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.jsat 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 buildand compare the output JS files with older ones.
Hope you learned something valuable here. Feel free to @ me on Twitter here
Comments
Join the discussioni have the follow error
An unhandled exception occurred: Cannot find module 'node:assert'
Hi, can you try using a newer version of NodeJS like v16.16.0?
Surya Teja Karra i will try, thanks!!
your webpack.config.js snippet doesn't even compile.
Did you at least try it before writing this blog ??
Hey, I've fixed the webpack config. It was missing a } at the end. Give it a try now :)
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...
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 😂
Hey thanks for informing. I've reworded the title.
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