1 | es-iife
|
2 | =======
|
3 |
|
4 | [![Build Status](https://travis-ci.com/eight04/es-iife.svg?branch=master)](https://travis-ci.com/eight04/es-iife)
|
5 | [![codecov](https://codecov.io/gh/eight04/es-iife/branch/master/graph/badge.svg)](https://codecov.io/gh/eight04/es-iife)
|
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 | transform({
|
61 | code: String,
|
62 | parse?: Function,
|
63 | ast?: Object,
|
64 | sourcemap?: Boolean = false,
|
65 | strict?: Boolean = false,
|
66 |
|
67 | resolveGlobal?: (importPath: String) => globalVariableName: String,
|
68 | name?: String
|
69 | }) => {code: String, map?: SourceMap}
|
70 | ```
|
71 |
|
72 | Arguments:
|
73 |
|
74 | * `code` - the JavaScript source code that would be transformed.
|
75 | * `parse` - a parser function which can parse JavaScript code into ESTree.
|
76 | * `ast` - AST object. If undefined then use `parse(code)`.
|
77 | * `sourcemap` - if true then generate the sourcemap.
|
78 | * `strict` - add `use strict` directive.
|
79 | * `resolveGlobal` - a function receiving an import path and return a global variable name.
|
80 | * `name` - The variable name that exports will be assigned to.
|
81 |
|
82 | Returns:
|
83 |
|
84 | * `code` - the code after transformed.
|
85 | * `map` - 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.
|
86 |
|
87 | Changelog
|
88 | ---------
|
89 |
|
90 | * 0.2.2 (Feb 18, 2021)
|
91 |
|
92 | - Add: `strict` option.
|
93 |
|
94 | * 0.2.1 (Oct 29, 2020)
|
95 |
|
96 | - Fix: object shorthand issue.
|
97 | - Bump dependencies. Fix for-loop issue.
|
98 |
|
99 | * 0.2.0 (Aug 14, 2019)
|
100 |
|
101 | - Change: define default export with `var`.
|
102 |
|
103 | * 0.1.1 (Aug 28, 2018)
|
104 |
|
105 | - Fix: export from statements.
|
106 |
|
107 | * 0.1.0 (Aug 28, 2018)
|
108 |
|
109 | - Initial release.
|