While this code below won't execute on client it will however be included in final build.
if (Meteor.isServer) {
console.log('blah');
}
Of course this code can be moved to server source but in some cases it's much easier to do conditional check. For example:
Meteor.methods({
greet: function () {
if (Meteor.isServer) {
console.log('greet()');
}
return 'hello';
},
});
Right now if you don't want to ship server code to client you need to split this method into 3 files.
// lib/greet_impl.js
greet_impl = function () {
return 'hello';
}
// server/methods.js
Meteor.methods({
greet: function () {
console.log('greet()');
return greet_impl();
}
});
// client/methods.js
Meteor.methods({
greet: function () {
return greet_impl();
}
});
I believe that this could be achieved by applying these changes to https://github.com/meteor/meteor/blob/devel/packages/minifier-js/minifier.js#L8:
var uglifyResult = uglify.minify(source, {
compress: {
drop_debugger: false,
unused: false,
- dead_code: false
+ dead_code: true,
+ global_defs: {
+ 'Meteor.isServer': false,
+ }
}
});
Not sure if babel.minify
has similar option.
I'm planning to write a blog post about this technique soon, but one approach here is to make a copy of the minifier-js
package in your local packages
folder, then modify it however you see fit. Eliminating dead code is an attractive optimization, but it has far-reaching consequences, so you're encouraged to take that plunge even if Meteor can't (yet) justify toggling that option in the standard minifier-js
package.
Good point. If build plugins were configurable (as described in #7829) this could be optional and disabled by default.
To help provide a more clear separation between feature requests and bugs, and to help clean up the feature request backlog, Meteor feature requests are now being managed under the https://github.com/meteor/meteor-feature-requests repository.
This feature request will be closed here, but anyone interested in migrating this feature request to the new repository (to make sure it stays active), can click here to start the feature request migration process. This manual migration process is intended to help identify which of the older feature requests are still considered to be of value to the community. Thanks!
Most helpful comment
I'm planning to write a blog post about this technique soon, but one approach here is to make a copy of the
minifier-js
package in your localpackages
folder, then modify it however you see fit. Eliminating dead code is an attractive optimization, but it has far-reaching consequences, so you're encouraged to take that plunge even if Meteor can't (yet) justify toggling that option in the standardminifier-js
package.