bpb
browserify transform to safely replace process.browser
with true
by working on the Abstract Syntax Tree (AST)
introduction
The browserify/webpack implementation of process
has a special browser
member set to true
, which is not present in
the nodejs/iojs implementation of process
.
// on node processbrowser === undefined // -> true // on browser processbrowser === true // -> true
This can be referenced in isomorphic code that
- adjusts it's behavior depending on which environment it's in
- is fully portable within the nodejs/iojs/browserify/webpack ecosystem, and
- is concise and Don't Repeat Yourself (DRY)
// example #1 { if !processbrowser throw 'animateElement function is for use client-side only!'; ; } // example #2 { return processbrowser ? : ; }
This is where bpb (short for "browserify-processisfy.browserify") comes in.
For optimized build-sizes and or security, use bpb in combination with unreachable-branch-transform (or a good minifier like UglifyJS) to strip out server-side-only code.
// example #1 after bpb { if !true throw 'animateElement function is for use client-side only!'; ; } // example #1 after bpb + unreachable-branch-transform { ; } // example #2 after bpb { return true ? : ; } // example #2 after bpb + unreachable-branch-transform { return ; }
usage
bpb can be used as a browserify transform, a transform stream, or a synchronous function.
options
- ecmaVersion: Must be either 3, 5, or 6. Default is 5.
All options are passed directly to falafel which passes them directly to acorn.
examples
// as a browserify transform var browserify = ; var fs = ; ; // as a transform stream var fs = ; var bpb = ; var unreachable = ; fs ; // as a synchronous function var bpb = ; var unreachable = ; unreachable // -> 'foo(1)'
changelog
0.2.2
- emit errors rather than throw them
0.2.1
- return passthrough for json files
0.2.0
- added es6 support
0.1.1
- added changelog to readme
- include readme in package files
0.1.0
- safe implementation
- advanced tests
0.0.1
- tests
- initial dumb implementation