Skip to content

GTDev87/insert-module-globals

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

insert-module-globals

insert implicit module globals (__filename, __dirname, process, global, and Buffer) into a module bundle stream generated by module-deps

build status

example

var mdeps = require('module-deps');
var bpack = require('browser-pack');
var insert = require('insert-module-globals');

var files = [ __dirname + '/files/main.js' ];
mdeps(files)
    .pipe(insert(files))
    .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 main.js | 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 insert = insertGlobals(files, opts)

Insert globals for an array of entry files. The files array is used to rebase the __filename and __dirname names or you can pass in an opts.basedir explicitly.

You can pass in a custom opts.resolve(id, parent, cb) function. By default browser-resolve is used for the opts.resolve.

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.

Return a through stream insert that expects input from the format written by module-deps. The insert stream will output data that can be fed into browser-pack.

usage

usage: insert-module-globals [files]

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!
    }
}

mdeps(files)
    .pipe(insert(files, {vars: vars}))
    .pipe(bpack({ raw: true }))
    .pipe(process.stdout)

license

MIT

About

insert implicit module globals into a module-deps stream

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.6%
  • Shell 2.4%