-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Custom output formats? #269
Copy link
Copy link
Closed
Labels
Description
I had to do something a lil’ dirty in d3-bundler. I call bundle.generate with iife format and then mangle the resulting code:
function patch(code) {
if (code.slice(0, 37) !== "(function (exports) { 'use strict';\n\n") throw new Error("unexpected output");
if (outro = code.slice(-26) !== "\n})((this.exports = {}));\n") throw new Error("unexpected output");
try { var version = require(path.resolve(entry, "../package.json")).version; } catch (ignore) {};
return "!function(global, exports) {\n"
+ " \"use strict\";\n"
+ "\n"
+ code.slice(37, -26)
+ (version ? " exports.version = " + JSON.stringify(version) + ";\n" : "")
+ "\n"
+ " if (typeof define === \"function\" && define.amd) global." + argv.name + " = exports, define(\"" + argv.name + "\", [], function() { return exports; });\n"
+ " else if (typeof module === \"object\" && module.exports) module.exports = exports;\n"
+ " else global." + argv.name + " = exports;\n"
+ "}(this, {});\n";
}I do this because I have “special” requirements for my UMD module, as D3 is a library:
- I want to generate a named AMD module, not an anonymous one.
- I want to export a global even if AMD is available (but not with CommonJS).
- I want to automatically pull the version number from the package.json.
My hack’s not too bad since the iife format is pretty simple, but do you think this is the right way to do it?
Reactions are currently unavailable