Fairly lightweight CommonJS module concatenation tool
What is it?
npm install node-module-concat
var modConcat = ;var outputFile = "./project/concatenated.js";;
var modConcat = require("node-module-concat");
var stream = new modConcat.ModuleConcatStream(entryModulePath [, options])
Constructs a Readable Stream of the concatenated project.
entryModulePath- the path to the entry point of the project to be concatenated. This might be an
index.jsfile, for example.
options- object to specify any of the following options:
outputPath- the path where the concatenated project file will be written. Provide this whenever possible to ensure that instances of
__filenameare replaced properly. If
__filenameare not used in your project or your project dependencies, it is not necessary to provide this path. This has no effect when the
browseroption is set.
excludeFiles- An Array of files that should be excluded from the project even if they were referenced by a
requirestatements should probably be wrapped with a conditional or a try/catch block to prevent uncaught exceptions.
excludeNodeModules- (boolean or Array) Set to
trueif all modules loaded from
node_modulesfolders should be excluded from the project. Alternatively, set to an Array of module names to be excluded from the project.
require("foobar")will not be replaced if
excludeNodeModulesis set to an Array containing
excludeNodeModulesis set to
extensions- An Array of extensions that will be appended to the required module path to search for the module in the file system. Defaults to
require("./foo")will search for:
./foo.jsonin that order, relative to the file containing the require statement.
require("./foo.js")will search for:
Note: ".node" file extensions are considered to be native C/C++ addons and are always excluded from the build.
requirestatements pointing to *.coffee files (i.e.
require("./foo.coffee")). These modules are compiled using the coffee-script compiler before they are included in the project.
optionsare passed along to the compiler function, as shown above.
Note: By default, ".json" files are prepended with
module.exports =. This behavior can be overwritten by explicitly specifying the ".json" key in the
Note: By default, the file extensions specified in
compilersare not added to the
require("./foo")will not find
./foo.coffeeunless ".coffee" is explicitly added to
browser- Set to
truewhen concatenating this project for the browser. In this case, whenever a required library is loaded from
browserfield in the
package.jsonfile (if found) is used to determine which file to actually include in the project.
allowUnresolvedModules- Set to
trueto prevent unresolved modules from throwing an Error; instead, the
require(...)expression will not be replaced, and the unresolved module will be added to
stats.unresolvedModules(see below). Defaults to
Any option supported by resolve.sync except
packageFilter, which can be overwritten.
Returns an Object containing statistics about the files included in the project. This object is available after the 'end' event is fired and there is no more data to consume. Properties include:
files- An Array of files included in the project
addonsExcluded- An Array of files excluded from the project because they are native C/C++ add-ons.
unresolvedModules- An Array of modules that could not be included in the project because they could not be found. Each element in the Array is an Object containing these properties:
parent- the full path of the file containing the require expression
module- the name or path to the module that could not be found
modConcat(entryModule, outputPath, [options, cb])
Helper function that constructs a new
ModuleConcatStream (see above) with
the following options and pipes the concatenated project to the
entryModule- the path to the entry point of the project to be concatenated. This might be an
index.jsfile, for example.
outputFile- the path where the concatenated project file will be written.
cb- Callback of the form
cb(err, stats). If no callback is provided, a Promise is returned instead, which resolves to the
statsObject returned by
require()statements don't work (i.e.
require("./" + variable))
require.resolvecalls are not modified
require.cachestatements are not modified