Giter Site home page Giter Site logo

ytiurin / import-export-merger Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 0.0 1.57 MB

Merge javascript files with imports/exports into one function.

Home Page: https://ytiurin.github.io/import-export-merger/

License: ISC License

JavaScript 98.45% CSS 1.55%
build-tool javascript web compiler javascript-compiler javascript-modules umd export-merger cli module-merger

import-export-merger's Introduction

This tool has a significant design drawback. It doesn't support an import of variables by reference. Still, functions importing works fine.

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. This is an alternative to code bundling for small libraries.

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,
  makeUMDBody
);

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.

import-export-merger's People

Contributors

ytiurin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.