If I have a method that needs to run some different code on the server and the client, I can use a Meteor.isServer block to identify the server only parts, this is nice because I dont have to repeat the boiler plate code:
export const updateText = new ValidatedMethod({
name: 'todos.updateText',
validate: new SimpleSchema({
todoId: { type: String },
newText: { type: String }
}).validator(),
run({ todoId, newText }) {
if(Meteor.isServer) {
// some super cool stuff
}
}
});
But if my server side code depends on packages that are not available on the client, how can I make sure those import process only on the server side? I want to do something like this - leaving my boiler plate and simulation code in a shared file, then have a second file with the server specific code, from which I could do all my imports:
export const updateText = new ValidatedMethod({
name: 'todos.updateText',
validate: new SimpleSchema({
todoId: { type: String },
newText: { type: String }
}).validator(),
run({ todoId, newText }) {
if(Meteor.isServer) {
import {updateText} from './server/methods';
updateText();
}
}
});
It seems to work, but is that the way the import is supposed to work? What is the best practice?