Giter VIP home page Giter VIP logo

babel-plugin-transform-modules-iife's Introduction

babel-plugin-transform-modules-iife

This plugin transforms ES2015 modules to an IIFE format; reading and writing from global namespaces.

It can be used if you are only targeting browser globals, and you don't want to deal with the complexity and overhead introduced by the @babel/plugin-tansform-modules-umd plugin. The additional CommonJS and AMD logic in the UMD format can cause problems on occasion.

While the inspiration for this plugin was taken from the UMD transform plugin, the output more closely resembles that of the iife format from rollup.

Install

Using npm:

npm install --save-dev babel-plugin-transform-modules-iife

or using yarn:

yarn add babel-plugin-transform-modules-iife --dev

Example

In:

import { foo } from './foo.js';

export const bar = foo;

Out:

this.Dog = this.Dog || {};
this.Dog.Cat = this.Dog.Cat || {};

(function (_exports, _foo) {
  "use strict";
  const bar = _foo.foo;
  _exports.bar = bar;
})(this.Dog.Cat, this.Foo.Bar.dummyRootFoo);

Usage

The plugin can be used in a normal Babel workflow like:

import babel from '@babel/core';
babel.transformFile('input.mjs', {
    plugins: [['babel-plugin-transform-modules-iife', pluginOptions]]
});

...where pluginOptions is an object with the following properties:

importNamespace

A dot-separated string that specifies the global object path containing the definitions of the objects that are imported.

exportNamespace

A dot-separated string that specifies the global object path that will be assigned the exports from the module.

importRelativePath

A path string that will be applied to the module specifier paths so the generated import variable names can be controlled to some degree.

This does not need to be an actual path on disk. It is simply used to help normalize the global variable names for the imported modules.

For example, with the following options:

{
    importNameSpace: 'Dog.Cat',
    importRelativePath: '/dummy/root'
}

, a statement like import { foo } from '../bar/baz.js'; would result in the import definition to be found in the global variable Dog.Cat.dummyBarBaz.

This follows the algorithm:

  • The import specifier is combined with the importRelativePath parameter using path.resolve(specifier, relativePath).
  • That absolute path is stripped of any path separators and converted to camelCase.
  • The resulting variable name is appended to the global object path specified by the importNamespace parameter.

babel-plugin-transform-modules-iife's People

Contributors

aaronmars avatar

Watchers

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