Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

export-helper

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

export-helper

Nodejs utility to help compile TS modules into sweet es5 & es6 exports

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by75%
Maintainers
1
Weekly downloads
 
Created
Source

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.
code style: prettier Known Vulnerabilities

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", // needed. Available options: "es5", "es6"
  path: "testFile.ts", // needed. Path to file (./ is optionnal)
  silent: false, // default to false; set to true to remove the log
  linesToTrim: 1 /* default to 1.
    You usually don't need this, but in case your IDE insert 
    an extra blank line between your module export and EOF, 
    incrementing that value should do the trick.
   */
};

... 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.

Keywords

FAQs

Package last updated on 24 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc