[SOLVED] How to write file to disk from a package then run it in the client?


Is it possible to make a Meteor package for anyone to use that can write a JS file to disk and for that file to be sent to clients like any other pre-existing file would be? So for example, the file would get written to disk on server startup before any clients can connect so that finally the code that is eventually sent to the client would include the file that was written because that file exists in a location whose files are sent to the client (i.e. in a client folder).

SOLUTION: Use api.addFiles to add a temporary or dummy file to a package (for client or server). Then use Package.registerBuildPlugin to register a custom plugin that will handle the specific file(s). When building is complete, that file goes to the client or server depending on what you specified with api.addFiles.


How is this different than just including the file in the package normally? It really sounds like you’re just describing the default behavior of you would normally include JS files in a package.


@nkrisc Yeah, except I want to write it to disk from a package, and the content of the JS file is not known before hand (otherwise I would totally just put it there like you said :smiley: ). I should probably mention that I want to write it to disk before the port for the app is opened.

I know I can make a server-side package that will write a file, but I think out of the box that will happen after the server port is already open? That would cause the server to restart which would make it happen all over again.

I suppose my question is: How do I hook into Meteor so that I can write a file before the app’s port is opened and before Meteor has compiled the server and client codes so that by the time the build process starts my written file will be there and will get included in the build?


Out of curiosity, what are you trying to write to this JS file such that it has to be implemented this way? What’s the use case?


@nkrisc What I want to do is use something like Webpack or Browserify to build bundles. The bundles would be loaded by Meteor like any other JS file you’d place in the app.


Moved this post (the solution) to the first post.


If you haven’t made a package to use browserify yet, then please try my Meteor package which integrates browserify into Meteor: cosmos:browserify.


@elidoran I’ve started one using Webpack!