0

I'm developing a node.js plugin in TypeScript.

The requirements are to export it as module.exports directly, e.g. when compiled, the index.js should look like this:

module.exports = require('./src/plugin.js').default;

My plugin.ts looks like this:

import {Options} from './types';

export default function myPlugin(options?: Partial<Options>): any {
  // ...
}

I can achieve the desired result by writing my index.ts like this:

import myPlugin from './src/plugin';

export = myPlugin;

However, if I do this, the Options type would not be exported in type definitions.

And, it's not possible to use export = module construct with another exports for types.

I want the consumer to be able to import both the plugin function and the types for it:

import myPlugin = require('@acme/my-plugin');

import {Options} from '@acme/my-plugin';

Is it possible for the consumer to be able to import types from my module as well as it's main symbol?

Slava Fomin II
  • 21,036
  • 19
  • 98
  • 176
  • 1
    What about `export {myPlugin as default, Options}`? Then your customer could do `import myPlugin from "@acme/my-plugin"` and `import {Options} from '@acme/my-plugin'` – barbsan Feb 01 '19 at 12:34
  • @barbsan `export default symbol` is different from `export = symbol`. In the first case you will get `module.exports = { default: symbol }` as output. – Slava Fomin II Feb 01 '19 at 13:59
  • I know it's different, that's why I wrote it as comment - it would allow your customers to import both plugin and options but with `require` they'll have to use `require("yourmodule").default`, – barbsan Feb 01 '19 at 15:06
  • Thanks @barbsan, but as I said in the question: `The requirements are to export it as module.exports directly`. It's a rule of the plugins ecosystem. – Slava Fomin II Feb 01 '19 at 15:12

0 Answers0