Giter Site home page Giter Site logo

insert-module-globals's Introduction

insert-module-globals

insert implicit module globals (__filename, __dirname, process, global, and Buffer) as a browserify-style transform

build status

example

var mdeps = require('module-deps');
var bpack = require('browser-pack');
var insert = require('insert-module-globals');
function inserter (file) {
    return insert(file, { basedir: __dirname + '/files' });
}

var files = [ __dirname + '/files/main.js' ];
mdeps(files, { transform: inserter })
    .pipe(bpack({ raw: true }))
    .pipe(process.stdout)
;
$ node example/insert.js | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}

or use the command-line scripts:

$ module-deps main.js | insert-module-globals | browser-pack | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}

or use insert-module-globals as a transform:

$ module-deps main.js --transform insert-module-globals | browser-pack | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}

methods

var insertGlobals = require('insert-module-globals')

var inserter = insertGlobals(file, opts)

Return a transform stream inserter for the filename file that will accept a javascript file as input and will output the file with a closure around the contents as necessary to define extra builtins.

When opts.always is truthy, wrap every file with all the global variables without parsing. This is handy because parsing the scope can take a long time, so you can prioritize fast builds over saving bytes in the final output.

Use opts.vars to override the default inserted variables, or set opts.vars[name] to undefined to override an undefined variable that has already been set by default.

If opts.debug is true, an inline source map will be generated to compensate for the extra lines.

events

inserter.on('global', function (name) {})

When a global is detected, the inserter stream emits a 'global' event.

usage

usage: insert-module-globals {basedir}

install

With npm, to get the library do:

npm install insert-module-globals

and to get the bin script do:

npm install -g insert-module-globals

insert custom globals.

insert-module-globals can also insert arbitary globals into files. Pass in an object of functions as the vars option.

var vars = {
    process: function (row, basedir) {
        return {
            id: "path/to/custom_process.js",
            source: customProcessContent
        }
    },
    Buffer: function (row, basedir) {
        return {
            id: 'path/to/custom_buffer.js',
            source: customProcessContent,
            //suffix is optional
            //it's used to extract the value from the module.
            //it becomes: require(...).Buffer in this case.
            suffix: '.Buffer'
        }
    },
    Math: function () {
        //if you return a string,
        //it's simply set as the value.
        return '{}'
        //^ any attempt to use Math[x] will throw!
    }
}

function inserter (file) {
    return insert(file, { vars: vars });
}
mdeps(files, { transform: inserter })
    .pipe(bpack({ raw: true }))
    .pipe(process.stdout)

license

MIT

insert-module-globals's People

Contributors

chadhietala avatar dominictarr avatar feross avatar jscissr avatar maxogden avatar sverrejoh avatar x-oss-byte avatar yuchi avatar zertosh avatar

Watchers

 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.