Coder Social home page Coder Social logo

static-module's Introduction

static-module

convert module usage to inline expressions

example

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.

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);

methods

var staticModule = require('static-module')

var sm = staticModule(modules, opts={})

Return a transform stream sm that transforms javascript source input to javascript source output with each property in the modules object expanded in inline form.

Properties in the modules object can be ordinary values that will be included directly or functions that will be executed with the statically evaluated arguments from the source under an optional set of opts.vars 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 be declared in client code with var and will appear in static expressions like opts.vars.

For example, to make this code with path.join() work:

var fs = require('fs');
var path = require('path');
var src = fs.readFileSync(path.join(__dirname, 'x.txt'), 'utf8');
console.log(src);

you can do:

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 inline comment. You can add opts.inputFilename to configure the original file name that will be listed in the source map.

install

With npm do:

npm install static-module

license

MIT

static-module's People

Contributors

aknuds1 avatar devongovett avatar eush77 avatar felthy avatar forbeslindesay avatar goto-bus-stop avatar hubdotcom avatar hughsk avatar jagonzalr avatar jakearchibald avatar jimmywarting avatar lachenmayer avatar lguzzon avatar mattdesl avatar mondeja avatar mscdex avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.