1 | es-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 |
|
8 | Transform ES module into a simple IIFE.
|
9 |
|
10 | Features
|
11 | --------
|
12 |
|
13 | * `import` statements are resolved to global variables.
|
14 | * `export` statements are exported as a global variable.
|
15 |
|
16 | There are more samples under `test/cases` folder.
|
17 |
|
18 | Usage
|
19 | -----
|
20 |
|
21 | ```js
|
22 | const {parse} = require("acorn");
|
23 | const {transform} = require("es-iife");
|
24 | const code = `
|
25 | import foo from "./foo.js";
|
26 | const main = (value) => return foo(value);
|
27 | export default main;
|
28 | `;
|
29 | const result = transform({
|
30 | code,
|
31 | parse,
|
32 | name: "doFoo",
|
33 | resolveGlobal: (name) => {
|
34 | if (name === "./foo.js") {
|
35 | return "FOO";
|
36 | }
|
37 | }
|
38 | })
|
39 | console.log(result.code);
|
40 | /* ->
|
41 | var doFoo = (function () {
|
42 |
|
43 | const main = (value) => return FOO(value);
|
44 |
|
45 | return main;
|
46 | })();
|
47 | */
|
48 | ```
|
49 |
|
50 | API reference
|
51 | -------------
|
52 |
|
53 | This module exports following members.
|
54 |
|
55 | * `transform`: A function which can convert ES module synax into an IIFE.
|
56 |
|
57 | ### transform
|
58 |
|
59 | ```js
|
60 | const 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 |
|
78 | The `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 |
|
83 | Changelog
|
84 | ---------
|
85 |
|
86 | * 0.1.0 (Aug 28, 2018)
|
87 |
|
88 | - Initial release.
|