duojs / duo

A next-generation package manager for the front-end
3.42k stars 118 forks source link

Request: Option to flatten build files without Common.JS-like functionality #497

Closed Alhadis closed 8 years ago

Alhadis commented 8 years ago

Apologies in advance if there's something I'm missing, but there doesn't appear to be a way of stripping the Common.JS-ish module/require from compiled JavaScript source.

I can see why this wouldn't be easy to remove, given the build process, but it'd be nice if there were an option to flatten/concatenate them as follows:

/** Development */
var polyfill = require("Alhadis/DOMTokenList-Polyfill");
var cssParser = require("Alhadis/CSSQuery");

Which, given the CLI option, could expand to:

/** Built file / Production */
var polyfill = (function(){
    /* ... contents of "token-list.js" get dropped in here */
});
var cssParser = (function(){
    /** Result of whatever CSSQuery would return */
});

I was hoping that require calls could be avoided client-side altogether, and that they were only an intermediary during development. If they can, please enlighten me; it'd be a lovely feature to have. =)

dominicbarnes commented 8 years ago

All the require implementations are never made public or global. Instead, the entry script you provide is where all the bootstrapping occurs.

However, you can create a UMD script with the --standalone <name> flag, which creates a script that can be consumed by CommonJS, AMD or even a plain browser. Similar is the --global <name> flag, which creates a script that exports to the specified global variable. (but doesn't add the CommonJS/AMD definitions)

$ duo polyfill.js --standalone polyfill
# or
$ duo polyfill.js --global polyfill

You can only export 1 thing per file this way, so you will need to run duo for each file you wish to export this way.

Is that what you mean? It sounds like you might also mean that you don't want any require calls throughout your entire codebase. This will not be possible with duo, the closest you will have is what I mentioned above.

Alhadis commented 8 years ago

Yeah, you're right, I'd rather eliminate the require clutter altogether, but if it's not possible, I guess I might resort to post-processing. I'm assuming it's possible through a plugin?