convert module usage to inline expressions
npm install static-moduleconvert module usage to inline expressions
Here's a simplified version of the brfs module
using static-module.
brfs converts fs.readFileSync(file) calls to inline strings with the contents
of file included in-place.
`` js
var staticModule = require('static-module');
var quote = require('quote-stream');
var fs = require('fs');
var sm = staticModule({
fs: {
readFileSync: function (file) {
return fs.createReadStream(file).pipe(quote());
}
}
}, { vars: { __dirname: __dirname + '/brfs' } });
process.stdin.pipe(sm).pipe(process.stdout);
`
input:
``
$ cat brfs/source.js
var fs = require('fs');
var src = fs.readFileSync(__dirname + '/x.txt');
console.log(src);
output:
`
$ node brfs.js < brfs/source.js
var src = "beep boop\n";
console.log(src);
`
` js`
var staticModule = require('static-module')
Return a transform stream sm that transforms javascript source input tomodules
javascript source output with each property in the object expanded in
inline form.
Properties in the modules object can be ordinary values that will be includedopts.vars
directly or functions that will be executed with the statically
evaluated arguments from the source
under an optional set of variables.
Property functions can return streams, in which case their contents will be
piped directly into the source output.
Otherwise, the return values of functions will be inlined into the source in
place as strings.
Use opts.varModules to map whitelisted module names to definitions that can bevar
declared in client code with and will appear in static expressions likeopts.vars.
For example, to make this code with path.join() work:
` js`
var fs = require('fs');
var path = require('path');
var src = fs.readFileSync(path.join(__dirname, 'x.txt'), 'utf8');
console.log(src);
you can do:
` js
var staticModule = require('static-module');
var quote = require('quote-stream');
var fs = require('fs');
var sm = staticModule({
fs: {
readFileSync: function (file) {
return fs.createReadStream(file).pipe(quote());
}
},
varMods: { path: require('path') }
}, { vars: { __dirname: __dirname + '/brfs' } });
process.stdin.pipe(sm).pipe(process.stdout);
`
Use opts.parserOpts to set additional options for the
acorn parser.
Set opts.sourceMap to true to generate a source map and add it as an inlineopts.inputFilename
comment. You can add to configure the original file name
that will be listed in the source map.
With npm do:
```
npm install static-module
MIT