This tool hasn't been tested, yet, on a reasonable amount of cases. Please, consider this, when using in your project.
import-export-merger
Merge javascript files with imports/exports into one function. Made to pack code in one file, exculding external dependencies.
Having three files:
/* index.js */ ; ;
/* moduleA.js */ ; const a = 1;
/* moduleB.js */ ; const b = a + 1; ;
Merging files result to a function:
{ var moduleAExports = ; var moduleBExports = ; return ;} { var a = moduleAa; return Object; } { var a = moduleAa; const b = a + 1; var $default = b; return default: $default ; } { const a = 1; return a: a ; } external;
Final code with UMD declaration:
{ if typeof define === "function" && defineamd ; else if typeof module === "object" && moduleexports moduleexports = ; else rootmyLibrary = ; }typeof self !== "undefined" ? self : this { return { var moduleAExports = ; var moduleBExports = ; return ; } { var a = moduleAa; return Object; } { var a = moduleAa; const b = a + 1; var $default = b; return default: $default ; } { 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:
;; const merge = ; const output = ;
To get code from predefined strings:
; const merge = ; const output = ;
Add UMD wrapper:
; const makeUMDBody supplyExternals = ; const merge = ;
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.