UNPKG

2.16 kBMarkdownView Raw
1es-iife
2=======
3
4[![Build Status](https://travis-ci.org/eight04/es-iife.svg?branch=master)](https://travis-ci.org/eight04/es-iife)
5[![Coverage Status](https://coveralls.io/repos/github/eight04/es-iife/badge.svg?branch=master)](https://coveralls.io/github/eight04/es-iife?branch=master)
6[![install size](https://packagephobia.now.sh/badge?p=es-iife)](https://packagephobia.now.sh/result?p=es-iife)
7
8Transform ES module into a simple IIFE.
9
10Features
11--------
12
13* `import` statements are resolved to global variables.
14* `export` statements are exported as a global variable.
15
16There are more samples under `test/cases` folder.
17
18Usage
19-----
20
21```js
22const {parse} = require("acorn");
23const {transform} = require("es-iife");
24const code = `
25import foo from "./foo.js";
26const main = (value) => return foo(value);
27export default main;
28`;
29const result = transform({
30 code,
31 parse,
32 name: "doFoo",
33 resolveGlobal: (name) => {
34 if (name === "./foo.js") {
35 return "FOO";
36 }
37 }
38})
39console.log(result.code);
40/* ->
41var doFoo = (function () {
42
43const main = (value) => return FOO(value);
44
45return main;
46})();
47*/
48```
49
50API reference
51-------------
52
53This module exports following members.
54
55* `transform`: A function which can convert ES module synax into an IIFE.
56
57### transform
58
59```js
60const result = transform({
61 code: String,
62 parse?: Function,
63 ast?: Object,
64 sourcemap?: Boolean,
65 resolveGlobal?: (importPath: String) => globalVariableName: String,
66 name?: String
67});
68```
69
70`code` - the JavaScript source code that would be transformed.
71
72`parse` - a parser function which can parse JavaScript code into ESTree.
73
74`ast` - AST object. If undefined then use `parse(code)`.
75
76`sourcemap` - if true then generate the sourcemap.
77
78The `result` object has following members:
79
80* `code`: string. The result JavaScript code.
81* `map?`: object. The source map object generated by [`magicString.generateMap`](https://github.com/Rich-Harris/magic-string#sgeneratemap-options-). If `sourcemap` is false then the map is null.
82
83Changelog
84---------
85
86* 0.1.0 (Aug 28, 2018)
87
88 - Initial release.