0.3.0 • Published 5 years ago

babel-plugin-nodejs-module-shim v0.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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

OptionDefaultsDescription
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 = {};