JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1741
  • Score
    100M100P100Q114201F
  • License MIT

Transform ES module into a simple IIFE.

Package Exports

  • es-iife

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (es-iife) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

es-iife

Build Status codecov install size

Transform ES module into a simple IIFE.

Features

  • import statements are resolved to global variables.
  • export statements are exported as a global variable.

There are more samples under test/cases folder.

Usage

const {parse} = require("acorn");
const {transform} = require("es-iife");
const code = `
import foo from "./foo.js";
const main = (value) => return foo(value);
export default main;
`;
const result = transform({
  code,
  parse,
  name: "doFoo",
  resolveGlobal: (name) => {
    if (name === "./foo.js") {
      return "FOO";
    }
  }
})
console.log(result.code);
/* ->
var doFoo = (function () {

const main = (value) => return FOO(value);

return main;
})();
*/

API reference

This module exports following members.

  • transform: A function which can convert ES module synax into an IIFE.

transform

const result = transform({
  code: String,
  parse?: Function,
  ast?: Object,
  sourcemap?: Boolean,
  resolveGlobal?: (importPath: String) => globalVariableName: String,
  name?: String
});

code - the JavaScript source code that would be transformed.

parse - a parser function which can parse JavaScript code into ESTree.

ast - AST object. If undefined then use parse(code).

sourcemap - if true then generate the sourcemap.

The result object has following members:

  • code: string. The result JavaScript code.
  • map?: object. The source map object generated by magicString.generateMap. If sourcemap is false then the map is null.

Changelog

  • 0.2.0 (Aug 14, 2019)

    • Change: define default export with var.
  • 0.1.1 (Aug 28, 2018)

    • Fix: export from statements.
  • 0.1.0 (Aug 28, 2018)

    • Initial release.