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

ts-use-exports

v1.1.2

Published

A TypeScript custom transformer that redirects function reference to the exports object.

Downloads

10

Readme

ts-use-exports

CircleCI npm

A TypeScript custom transformer that redirects function reference to the exports object.

When targeting CommonJS as the module target, the TypeScript compiler attach all exported functions to the module.exports object. However, any reference to those functions are not modified, causing unexpected results when one tries to stub these functions in unit-tests. Consider this module:

/** foobar.ts */

export function foo() {
  return "foo";
}

export function bar() {
  return foo() + "bar";
}

which transpiles to:

/** foobar.js */

function foo() {
  return "foo";
}
exports.foo = foo;
function bar() {
  return foo() + "bar";
}
exports.bar = bar;

Let's unit test this module. If foo is a function that triggers network activity or some other operation that we do not wish to exercise, it is common to stub it with a fake function.

/** test_foobar.js */

assert.strictEqual(foobar.bar(), "foobar"); // Pass

// Use SinonJS to stub out the foo function
sinon.replace(foobar, 'foo', sinon.fake.returns("bar"));

assert.strictEqual(foobar.bar(), "barbar"); // AssertionError! Actual: foobar

Sinon works by overwriting the reference to a value on an object. The second assert failed because the foo() call in bar is referencing the said function directly rather than through the module.exports object. This package provides a custom TypeScript transformer that emits the proper function call (ie exports.foo()) so that function stubbing works as expected.

Usage

First install the package

yarn add ts-use-exports
# or
npm install ts-use-exports

Depending on your toolchain, see one of the example setups below on using the custom transformer.

ts-node

ts-node by default does not provide a program instance to custom transformers. We will use ttypescript (install with yarn add ttypescript) to work around this limitation.

/** tsconfig.json */
{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "ts-use-exports" }
    ]
  }
}

Then invoke the ts-node binary with the ttypescipt compiler

ts-node --compiler ttypescript

Alternatively, if you use ts-node programmatically,

require('ts-node').register({
  project: 'tsconfig.json',
  compiler: 'ttypescript',
});

Webpack (with ts-loader / awesome-typescript-loader)

/** webpack.config.js */
const tsUseExports = require('ts-use-exports');

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader', // or 'awesome-typescript-loader'
        options: {
          getCustomTransformers: program => ({
            before: [tsUseExports(program)],
          }),
        },
      },
    ],
  },
};

Note

  • The module target must be CommonJS (either set explicity or implied by ES3/ES5 target).

  • The transformer currently does not produce the correct output if you alias a function name. See issue #3.