import-export-merger

0.1.0 • Public • Published

This tool hasn't been tested, yet, on a reasonable amount of cases. Please, consider this, when using in your project.

import-export-merger

Try a web tool

Merge javascript files with imports/exports into one function. Made to pack code in one file, exculding external dependencies.

Having three files:

/* index.js */
 
import { a } from "./moduleA";
 
export * from "./moduleB";
/* moduleA.js */
 
import "external";
 
export const a = 1;
/* moduleB.js */
 
import { a } from "./moduleA";
 
const b = () => a + 1;
 
export default b;

Merging files result to a function:

(function (indexFactory, moduleBFactory, moduleAFactory, external) {
  var moduleAExports = moduleAFactory(external);
  var moduleBExports = moduleBFactory(moduleAExports);
  return indexFactory(moduleAExports, moduleBExports);
})(
  function indexFactory(moduleA, moduleB) {
    var a = moduleA.a;
 
    return Object.assign({}, moduleB);
  },
  function moduleBFactory(moduleA) {
    var a = moduleA.a;
    const b = () => a + 1;
 
    var $default = b;
    return { default: $default };
  },
  function moduleAFactory(external) {
    const a = 1;
    return { a: a };
  },
  external
);

Final code with UMD declaration:

(function (root, factory) {
  if (typeof define === "function" && define.amd) {
    define(["external"], factory);
  } else if (typeof module === "object" && module.exports) {
    module.exports = factory(require("external"));
  } else {
    root.myLibrary = factory(root.external);
  }
})(typeof self !== "undefined" ? self : this, function (external) {
  return (function (indexFactory, moduleBFactory, moduleAFactory, external) {
    var moduleAExports = moduleAFactory(external);
    var moduleBExports = moduleBFactory(moduleAExports);
    return indexFactory(moduleAExports, moduleBExports);
  })(
    function indexFactory(moduleA, moduleB) {
      var a = moduleA.a;
 
      return Object.assign({}, moduleB);
    },
    function moduleBFactory(moduleA) {
      var a = moduleA.a;
      const b = () => a + 1;
 
      var $default = b;
      return { default: $default };
    },
    function moduleAFactory(external) {
      const a = 1;
      return { a: a };
    },
    external
  );
});

UMD is enabled by default.

Install

npm install import-export-merger

Use as CLI

Basic usage:

npx iemerger src

Looking for ./src/index.js in current working directory and output result to <STDIN>

Output to file:

npx iemerger src --output my-library.js

Use CLI in combination with other tools.

Format output with prettier:

npx iemerger src | npx prettier --parser=babel > my-library.js

Process merged code with with babel:

npx iemerger src | npx babel -f my-library.js -o my-library.js

Minify result with uglifyjs:

npx iemerger src | npx uglifyjs -o my-library.js

Combine tools:

npx iemerger src | npx babel -f my-library.js | npx uglifyjs -o my-library.js

Use with Node

The library API consists of functions, that are composed with pipe.

To read file and return a function code:

import { compileModule, makeModule, map, pipe } from "import-export-merger";
import { readFiles } from "import-export-merger/fs";
 
const merge = pipe(readFiles, map(fileToModule), compileModule, makeModule);
 
const output = merge("./src/index.js");

To get code from predefined strings:

import { rawToModule, compileModule, makeModule } from "import-export-merger";
 
const merge = pipe(rawToModule, compileModule, makeModule);
 
const output = merge([
  { body: 'import { myFunction } from "./moduleB";', filepath: "./moduleA" },
  { body: "export function myFunction() {}", filepath: "./moduleB" },
]);

Add UMD wrapper:

import { makeUMD, pickExternals, supply } from "import-export-merger";
 
const [makeUMDBody, supplyExternals] = supply(
  makeUMD,
  pickExternals,
  "myLibrary"
);
 
const merge = pipe(
  // ...
  compileModule,
  supplyExternals,
  makeModule,
  makeUMD
);

How it works?

All imports and exports are located with regular expressions and replaced with ES5 compatible code.

Where can it be used?

It can be used in an automation flow, when building small NPM modules. It doesn't include external dependencies or non-javascript resources into the build, thus, working faster.

Dynamic imports

Dynamic imports are not supported.

Comments

All commentary code is cut out from the source, to prevent bad syntax extraction.

Readme

Keywords

none

Package Sidebar

Install

npm i import-export-merger

Weekly Downloads

1

Version

0.1.0

License

ISC

Unpacked Size

82.8 kB

Total Files

54

Last publish

Collaborators

  • ytiurin