Giter VIP home page Giter VIP logo

esm-polyfills's Introduction

Build

esm-polyfills

ES module format is becoming a norm these days with some of the popular modules published as only ES modules(example got, ky). It also comes with a lot of useful features like Top-level await, etc.

But, there are a few features that CommonJS supports but ESM doesnt like these. It becomes a pain to migrate each of these features to a ESM compatible feature and its a lot of manual work. This polyfill tries to support those commonly used and feasible features in ES modules.

Also, i have a detailed it in my blog here.

Available polyfills

  • __filename
  • __dirname
  • require
  • require.resolve

Installation

Using npm:

$ npm install --save @subbu963/esm-polyfills

Using yarn:

$ yarn add @subbu963/esm-polyfills

Usage

$ node -r @subbu963/esm-polyfills <your-script>.mjs

Then in your <your-script>.mjs file, globals like __filename, __dirname, require and require.resolve will be automatically available:

console.log('__filename', __filename);
console.log('__dirname', __dirname);
console.log(require('lodash'), require.resolve('lodash'), require('../package.json'));

If you dont want to auto polyfill, then you can programmatically import them and use it in your code like this:

$ ESM_POLYFILLS_GLOBAL=false node <your-script>.mjs

Then in your <your-script>.mjs file, do:

import { getFileName, getDirName, getRequire as _require_ } from '@subbu963/esm-polyfills';

const __filename = getFileName();
const __dirname = getDirName();

console.log('__filename', __filename);
console.log('__dirname', __dirname);
console.log(_require_('lodash'), _require_.resolve('lodash'));

Comparision with other libraries

There are other libraries which serve the same purpose like:

But these packages arent actually polyfills. They require you to manually import those libraries and use __dirname, __filename, etc from them. My polyfill aims to get away with this manual process and use those commonjs functionality directly in a ES module like you are in a commonjs module.

esm-polyfills's People

Contributors

subbu963 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  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.