babel-plugin-nodejs-module-shim

0.3.0 • Public • Published

NPM version Build Status Coverage Status

babel-plugin-nodejs-module-shim

A babel plugin to shim Node.js build-in modules and global objects.

Example

function processs(__filename){
  const process = {
    a:1
  }
  return process.a;
}
 
if (process.env.NODE_ENV === 'TEST') {
 
}
 
      ↓ ↓ ↓ ↓ ↓ ↓
      
var process = require("<CWD>/process/browser.js");
 
function processs(__filename) {
  const process = {
    a: 1
  };
  return process.a;
}
 
if (process.env.NODE_ENV === 'TEST') {}

Notice

Polyfills for Node.js core libraries from node-libs-browser are used if available.

By default, this plugin will polyfill each library if there is a known polyfill.

The following modules child_processclusterdgramdnsfsmodulenetreadlinerepltls are not supported polyfill.

But you could use empty option as below mentioned to provide an empty object.

Install

npm install --save babel-plugin-nodejs-module-shim

Usage

Via .babelrc or babel-loader.

{
  "plugins": [["nodejs-module-shim"]]
}

Options

Option Defaults Description
prefix '' prefix + shimModulePath. See example below.
empty [] provide an empty object for the module that is not supported by polyfill. See example below.

More examples

["nodejs-module-shim", { prefix: 'PREFIX' }]

process;
 
      ↓ ↓ ↓ ↓ ↓ ↓
    
var process = require("PREFIX<CWD>/process/browser.js");
 
process;
 

["nodejs-module-shim", { emprty: ['fs'] }]

var fs = require('fs');
 
    ↓ ↓ ↓ ↓ ↓ ↓
  
var fs = {};
 

Readme

Keywords

Package Sidebar

Install

npm i babel-plugin-nodejs-module-shim

Weekly Downloads

0

Version

0.3.0

License

MIT

Unpacked Size

289 kB

Total Files

40

Last publish

Collaborators

  • pigcan