Detecting "Meteor Packaging" (To the End of "meteor publish; npm publish" Just Working)

When packaging stuff for Node (CommonJS) and other schemes, there is some indicator of what the environment is (see example code from HighlightJS below).

Is there some way to find the global object from within a Meteor package so I can have essentially the same code base for a npm module (has an extra package.json relative to a Meteor package), and for a Meteor package (has an extra package.js relative to a npm package)?

i.e.: I want to be able to do meteor publish; npm publish; and have things all work out. But what I have below is sketchy.

(function(root, name, factory) {
	var thingie = factory();

	if (typeof module === "object" && module.exports) {
		// Node or CommonJS
		module.exports = thingie;
	} else if (typeof define === "function" && define.amd) {
		// AMD
		define([], function() {
			return thingie;
		});
	} else {
		// The Else Condition


		// Find the global object for export to both the browser and web workers.
		var globalObject = (typeof window === 'object') && window ||
			(typeof self === 'object') && self;

		var thingie = factory(_);
		root[name] = thingie;
		if (!!globalObject) {
			globalObject[name] = thingie;
		}

		// Poor Meteor
		SomeModule = thingie;
	}
}(this, 'SomeModule', someFactory));

I don’t know for sure, but in Meteor 1.3 the above should just work, since Meteor will support the CommonJS format. @benjamn is this correct? Will we have some documentation about writing packages using modules?

There’s a reliable global variable defined in the meteor package. If you’re using the modules package in 1.3, though, this code will probably fall into the // Node or CommonJS block. To make that work, make sure your package.js file calls api.mainModule("main.js") instead of api.addFiles("main.js").

1 Like

Also, in Meteor 1.3, you might be able to publish to NPM only and just use npm install in your Meteor app!

1 Like

Would you mind humouring me… And this is a fairly serious question since I’ve practically got no education on Node.

What is this?

When this file is used on the client (e.g. web browser), this is the browsers window object. When this file is used on the server (Node), this is Node’s global or GLOBAL (alias) object (so like the browsers window object but for the server).

1 Like