npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

import-export-merger

v0.1.0

Published

Concatenate javascript files with imports/exports.

Downloads

4

Readme

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.