export-helper
Pretentious name for a tiny nodejs module that edits the last line of a file.
Why ?
It basically turns export = myModule;
into default export myModule;
between tsc compilations (both ways supported)
Use it to compile into the syntax of Old (require()
, NOT require().default
) and a valid es6 module, in a single build command.
Compatibility
node >= 10
Install
npm install --save-dev export-helper
Use
Javascript file helper.js
:
const exportHelper = require("export-helper");
exportHelper({ mode: "es6", path: "testFile.ts" })
.then(res => res);
In terminal:
$ node helper.js
$ npmjs/export-helper: "export = constant;" has successfully be replaced by "export default constant;" (testFile.ts)
Options
This function accepts an option object :
const options = {
mode: "es6",
path: "testFile.ts",
silent: false,
linesToTrim: 1
};
... more examples ?
This is my configuration on the module I made this for.
I have an utility file updateExport.js:
const exportHelper = require("export-helper");
const updateExport = () => {
if (process.argv.slice(2)[0] === "es5") {
exportHelper({ mode: "es5", path: "src/index.ts" }).then((res) => res);
} else if (process.argv.slice(2)[0] === "es6") {
exportHelper({ mode: "es6", path: "src/index.ts" }).then((res) => res);
} else
throw "Oopsie, it seems you forgot to pass either 'es5' or 'es6' as argument !";
};
updateExport();
This is my npm run build
script:
node rebuild.js && node updateExport.js es5 && tsc -p tsconfig-cjs.json && node updateExport.js es6 && tsc -p tsconfig.json
- rebuild.js is a simple utility that wipes the /lib folder before compilation.
node updateExport.js es5
: wrapping my module into a function using process.argv
, I can easily pass arguments to it. Basically it will read the last line of my index file, search for default
and replace it by =
. It is that dumb.tsc -p tsconfig-cjs.json
: I'm calling tsc with this config file- Now that I've compiled a proper cjs version with
module.exports
, I call node updateExport.js es6
to change back the source code. - Finally I call tsc to build the es6 module.