šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

babel-compat-loader

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-compat-loader

A node loader to import modules compiled by Babel a if they were native ESM.

0.1.1
latest
Source
npm
Version published
Weekly downloads
2
-33.33%
Maintainers
1
Weekly downloads
Ā 
Created
Source

babel-compat-loader

A node loader to import modules compiled by Babel a if they were native ESM.

Installation

npm install --save babel-compat-loader

or

yarn add babel-compat-loader

Usage

node --experimental-modules --loader babel-compat-loader ./your-file.mjs

What is this loader needed for?

ECMAScript modules transpiled by Babel have a "problem": they are not ECMAScript modules anymore. They become CommonJS modules, but this creates an interoperability problem with ECMAScript modules in NodeJS.

Consider this exaple:

// index.mjs
import { val } from "./dependency";
console.log(val);

// dependency.mjs
export const val = 3;

When this program is run using node --experimental-modules index.mjs it logs 3, as expected.

When both the files are transpiled by Babel it still logs 3, because Babel knows how to handle named exports defined in transpiled modules:

// index.js
var _dependency = require("./dependency");

console.log(_dependency.val);

// dependency.js
Object.defineProperty(exports, "__esModule", { value: true });

var val = exports.val = 2;

When only the dependency is transpiled by Babel the program breaks:

// index.mjs
import { val } from "./dependency";
console.log(val);

// dependency.mjs
Object.defineProperty(exports, "__esModule", { value: true });

var val = exports.val = 2;
import { val } from "./dependency";
         ^^^
SyntaxError: The requested module './dependency' does not provide an export named 'val'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)

The problem is that, since JavaScript engines need to know which variables are exported by a module before executing it, CommonJS modules only export a default value which represents the module.exports object.

This loader fixes this problem, by analyzing the imported CommonJS modules before executing them.

FAQs

Package last updated on 01 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts