Skip to content

Commit

Permalink
Update minifier.js to remove server and development code
Browse files Browse the repository at this point in the history
A very simple change inspired by https://github.com/ssrwpo/uglifyjs2 to remove server (`Meteor.isServer`) and development code (`Meteor.isDevelopment`) easily (and speeds up client-side execution by removing `Meteor.isClient` and `Meteor.isProduction`)

We do this by search-replacing first to get simple var names (UGLIFYJS_FALSE and UGLIFYJS_TRUE) for `terser` to replace during conditional compilation

Notes: 
1. In addition to reducing JS load, removing server-side code is desirable for security purposes (and protecting server-side code as well)
2. Tested in production and works great
  • Loading branch information
ramezrafla committed Jul 5, 2018
1 parent b8074a8 commit 9fffeac
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/minifier-js/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ meteorJsMinify = function (source) {
terser = terser || Npm.require("terser");

try {
source = source
.replace(/Meteor\.isServer|Meteor\.isDevelopment|process\.env\.NODE_DEBUG/g, 'UGLYFYJS_FALSE')
.replace(/Meteor\.isClient|Meteor\.isProduction/g, 'UGLYFYJS_TRUE');
var terserResult = terser.minify(source, {
compress: {
drop_debugger: false,
unused: false,
dead_code: true,
global_defs: {
"process.env.NODE_ENV": NODE_ENV
"process.env.NODE_ENV": NODE_ENV,
UGLYFYJS_FALSE: false,
UGLYFYJS_TRUE: true
}
},
mangle: {
Expand Down

1 comment on commit 9fffeac

@ramezrafla
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention that we set ‘unused’ to true

Please sign in to comment.