node-module-concat
Advanced tools
Comparing version 2.0.0-beta to 2.0.0
@@ -31,3 +31,2 @@ /* node-module-concat | ||
project.pipe(out); | ||
setTimeout(() => project.pause(), 1); | ||
}); | ||
@@ -34,0 +33,0 @@ // Call `cb` if it was provided; otherwise return the Promise |
@@ -66,20 +66,9 @@ const fs = require("fs") | ||
- `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 `__dirname` and `__filename` are replaced properly. If | ||
`__dirname` and `__filename` are not used in your project or your | ||
project dependencies, it is not necessary to provide this path. | ||
- `excludeFiles` - An Array of files that should be excluded from the | ||
project even if they were referenced by a `require(...)`. | ||
- `outputPath` | ||
- `excludeFiles` | ||
- `excludeNodeModules` | ||
- `extensions` | ||
- `compilers` | ||
- `browser` | ||
Note: These `require` statements should probably be wrapped with a | ||
conditional or a try/catch block to prevent uncaught exceptions. | ||
- `excludeNodeModules` - Set to `true` if modules loaded from | ||
`node_modules` folders should be excluded from the project. | ||
- `browser` - Set to `true` when concatenating this project for the | ||
browser. In this case, whenever a required library is loaded from | ||
`node_modules`, the `browser` field in the `package.json` file (if | ||
found) is used to determine which file to actually include in the | ||
project. | ||
See README.md for more details. | ||
@@ -99,2 +88,25 @@ */ | ||
} | ||
// Configure default file extensions | ||
if(!Array.isArray(opts.extensions) ) { | ||
opts.extensions = opts.extensions ? [opts.extensions] : | ||
[".js", ".json"]; | ||
} | ||
// Add ".node" to the list to allow us to find native modules | ||
opts.extensions.push(".node"); | ||
// Configure default compilers | ||
opts.compilers = opts.compilers || {}; | ||
if(typeof opts.compilers[".json"] === "undefined") { | ||
opts.compilers[".json"] = | ||
(src, options) => "module.exports = " + src; | ||
} | ||
/* Use package.json `browser` field instead of `main` if `browser` | ||
option is set */ | ||
if(opts.browser) { | ||
opts.packageFilter = (parsedPkgJson, pkgPath) => { | ||
if(parsedPkgJson.browser) { | ||
parsedPkgJson.main = parsedPkgJson.browser; | ||
} | ||
return parsedPkgJson; | ||
}; | ||
} | ||
// List of files already included in the project or pending inclusion | ||
@@ -110,8 +122,5 @@ this._files = [entryModulePath]; | ||
// Called when we should start/continue processing | ||
/* Called when we should start/continue processing. | ||
We should stop processing whenever `this.push` returns `false`. */ | ||
_read(size) { | ||
this._continueProcessing(); | ||
} | ||
_continueProcessing() { | ||
// Write the project header | ||
@@ -125,4 +134,5 @@ if(!this._headerWritten) { | ||
while(this._fileIndex < this._files.length) { | ||
if(!this._addFile(this._files[this._fileIndex]) ) | ||
if(!this.push(this._addFile(this._files[this._fileIndex])) ) { | ||
return; | ||
} | ||
} | ||
@@ -135,4 +145,5 @@ // Write the project footer | ||
/* Adds the file from the given `filePath` to the project. Returns `true` | ||
if more data can be added to the stream; `false` otherwise. */ | ||
/* Adds the file from the given `filePath` to the project. Returns the | ||
modified module contents (with header/footer added), which should be | ||
added to the stream. */ | ||
_addFile(filePath) { | ||
@@ -144,2 +155,7 @@ try { | ||
this._fileIndex++; | ||
// Compile this file if needed | ||
let compiler = this._options.compilers[path.extname(filePath)]; | ||
if(compiler) { | ||
code = compiler(code, this._options); | ||
} | ||
// Remove some line comments from code | ||
@@ -179,20 +195,7 @@ code = code.replace(/(?:\r\n?|\n)\s*\/\/.*/g, ""); | ||
// Get ready to resolve the module | ||
var resolveOpts = { | ||
"basedir": path.dirname(filePath), | ||
"extensions": ["", ".js", ".json", ".node"] | ||
}; | ||
/* Use package.json `browser` field instead of `main` if | ||
`browser` option is set */ | ||
if(this._options.browser) { | ||
resolveOpts.packageFilter = (parsedPkgJson, pkgPath) => { | ||
if(parsedPkgJson.browser) { | ||
parsedPkgJson.main = parsedPkgJson.browser; | ||
} | ||
return parsedPkgJson; | ||
}; | ||
} | ||
this._options.basedir = path.dirname(filePath); | ||
// Thank you, node-resolve for making this easy! | ||
try { | ||
// Resolve the module path | ||
modulePath = resolve.sync(modulePath, resolveOpts); | ||
modulePath = resolve.sync(modulePath, this._options); | ||
// Do not replace core modules | ||
@@ -211,6 +214,7 @@ if(resolve.isCore(modulePath) ) { | ||
if(index < 0) { | ||
// Not found; add this module to the project | ||
// Not added to project yet; try to add it | ||
if(!this._options.excludeFiles || | ||
this._options.excludeFiles.indexOf(modulePath) < 0) | ||
{ | ||
// Not excluded, so we're good to go! | ||
index = this._files.push(modulePath) - 1; | ||
@@ -244,19 +248,11 @@ } | ||
} | ||
/* Prepend module header and append module footer and write the | ||
included module to the stream */ | ||
return this.push( | ||
// Add file header | ||
FILE_HEADER | ||
/* Return the modified module contents, prepending the module header | ||
and appending the module footer. */ | ||
return FILE_HEADER | ||
.replace(/\$\{id\}/g, this._files.indexOf(filePath) ) | ||
.replace(/\$\{path\}/g, filePath) + | ||
// Add extra header bit if this is a JSON file | ||
(path.extname(filePath) === ".json" ? | ||
"module.exports = " : "") + | ||
// Add modified project file | ||
code + | ||
// Add file footer | ||
FILE_FOOTER | ||
.replace(/\$\{id\}/g, this._files.indexOf(filePath) ) | ||
.replace(/\$\{path\}/g, filePath) | ||
); | ||
.replace(/\$\{path\}/g, filePath); | ||
} catch(err) { | ||
@@ -266,3 +262,4 @@ process.nextTick(() => { | ||
}); | ||
return false; | ||
// Return EOF | ||
return null; | ||
} | ||
@@ -269,0 +266,0 @@ } |
{ | ||
"name": "node-module-concat", | ||
"version": "2.0.0-beta", | ||
"description": "CommonJS module concatenation library", | ||
"version": "2.0.0", | ||
"description": "Lightweight CommonJS module concatenation tool", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
# node-module-concat | ||
CommonJS module concatenation library | ||
Fairly lightweight CommonJS module concatenation tool | ||
## What is it? | ||
This library exposes a single function that concatenates CommonJS modules | ||
within a project. This can be used to obfuscate an entire project into a | ||
single file. It can also be used to write client-side JavaScript code where | ||
This library exposes a single function and stream API that concatenates CommonJS | ||
modules within a project. This can be used to obfuscate an entire project into | ||
a single file. It can also be used to write client-side JavaScript code where | ||
each file is written just like a Node.js module. | ||
## Why? | ||
Because projects like Webpack and Browserify are cool, but they are a little | ||
heavy for my taste. I just wanted something to compile CommonJS modules into a | ||
single JavaScript file. This project has one dependency: | ||
[resolve](https://github.com/substack/node-resolve) | ||
## Install | ||
@@ -19,5 +25,5 @@ | ||
var outputFile = "./project/concatenated.js"; | ||
modConcat("./project/index.js", outputFile, function(err, files) { | ||
modConcat("./project/index.js", outputFile, function(err, stats) { | ||
if(err) throw err; | ||
console.log(files.length + " were combined into " + outputFile); | ||
console.log(stats.files.length + " were combined into " + outputFile); | ||
}); | ||
@@ -36,3 +42,3 @@ ``` | ||
concatenated. This might be an `index.js` file, for example. | ||
- `options` - object to specify any of the following options | ||
- `options` - object to specify any of the following options: | ||
- `outputPath` - the path where the concatenated project file will be | ||
@@ -43,3 +49,3 @@ written. Provide this whenever possible to ensure that instances | ||
project dependencies, it is not necessary to provide this path. This | ||
has no effect when `browser` option is set. | ||
has no effect when the `browser` option is set. | ||
- `excludeFiles` - An Array of files that should be excluded from the | ||
@@ -52,2 +58,40 @@ project even if they were referenced by a `require(...)`. | ||
`node_modules` folders should be excluded from the project. | ||
- `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 `[".js", ".json"]`. | ||
For example, `require("./foo")` will search for: | ||
- `./foo` | ||
- `./foo.js` | ||
- `./foo.json` | ||
in that order, relative to the file containing the require statement. | ||
Another example, `require("./foo.js")` will search for: | ||
- `./foo.js` | ||
- `./foo.js.js` | ||
- `./foo.js.json` | ||
**Note**: ".node" file extensions are considered to be native C/C++ | ||
addons and are always excluded from the build. | ||
- `compilers` - An Object describing how files with certain file extensions | ||
should be compiled to JavaScript before being included in the project. | ||
The example below will allow node-module-concat to handle `require` | ||
statements 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. | ||
```javascript | ||
{ | ||
".coffee": (src, options) => require("coffee-script").compile(src) | ||
} | ||
``` | ||
`options` are 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 `compilers` Object. | ||
**Note**: By default, the file extensions specified in `compilers` are | ||
not added to the `extensions` option, so `require("./foo")` will not | ||
find `./foo.coffee` unless ".coffee" is explicitly added to `extensions` | ||
(see above). | ||
- `browser` - Set to `true` when concatenating this project for the | ||
@@ -58,2 +102,5 @@ browser. In this case, whenever a required library is loaded from | ||
project. | ||
- Any [option supported by resolve.sync] | ||
(https://github.com/substack/node-resolve#resolvesyncid-opts) except | ||
`basedir` and `packageFilter`, which can be overwritten. | ||
- Any [option supported by the Readable class] | ||
@@ -60,0 +107,0 @@ (https://nodejs.org/api/stream.html#stream_new_stream_readable_options) |
@@ -24,1 +24,2 @@ var fs = require("fs") | ||
console.log(cool); | ||
console.log("node-module-concat version:", require("../package.json").version); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
23743
19
1
132
0
420