Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eight04/es-iife
Transform ES module into a simple IIFE
https://github.com/eight04/es-iife
Last synced: 4 months ago
JSON representation
Transform ES module into a simple IIFE
- Host: GitHub
- URL: https://github.com/eight04/es-iife
- Owner: eight04
- License: mit
- Created: 2018-08-27T14:51:32.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-02-18T05:25:51.000Z (about 4 years ago)
- Last Synced: 2024-10-11T23:33:22.124Z (4 months ago)
- Language: JavaScript
- Size: 241 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
es-iife
=======[](https://travis-ci.com/eight04/es-iife)
[](https://codecov.io/gh/eight04/es-iife)
[](https://packagephobia.now.sh/result?p=es-iife)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
-----```js
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
```js
transform({
code: String,
parse?: Function,
ast?: Object,
sourcemap?: Boolean = false,
strict?: Boolean = false,
resolveGlobal?: (importPath: String) => globalVariableName: String,
name?: String
}) => {code: String, map?: SourceMap}
```Arguments:
* `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.
* `strict` - add `use strict` directive.
* `resolveGlobal` - a function receiving an import path and return a global variable name.
* `name` - The variable name that exports will be assigned to.Returns:
* `code` - the code after transformed.
* `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.Changelog
---------* 0.2.2 (Feb 18, 2021)
- Add: `strict` option.
* 0.2.1 (Oct 29, 2020)
- Fix: object shorthand issue.
- Bump dependencies. Fix for-loop issue.* 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.