coderaiser/putout

convert-commonjs-to-esm/exports not working

ngugcx opened this issue · 8 comments

ngugcx commented

.putout.json

{
    "rules": {
        "strict-mode/add-missing": "off",
        "package-json/add-type": "off",
        "convert-commonjs-to-esm": "on",
        "remove-console": "off",
        "convert-commonjs-to-esm/require": "on",
        "convert-commonjs-to-esm/commons": "on",
        "convert-commonjs-to-esm/exports": "on",
        "try-catch/sync": "off"
    }
}

lib.js

function test() {
    console.log('test');
}

exports.test = test;

package.json:

{
    "name": "putout-test",
    "version": "1.0.0",
    "type": "module",
    "main": "index.js",
    "license": "MIT"
}

putout lib.js --fix

exports in lib.js is not modified.

putout -v
v32.16.0

This is not really a good idea to use:

exports.test = test;

Since:

exports = test;

Will just overwrite exports variable, not make any exports.

ngugcx commented

One of third party modules I'm using now uses this way to export.
Can putout handle it?

Yes, just landed a new rule nodejs/convert-exports-to-module-exports. Enable it, and it will do the thing.

Is it works for you?

ngugcx commented

I want to convert it to es module export.

Upgrade to v33 and try it, should work for you use case.

ngugcx commented

Tried, lots of plugin not found errors:

No plugin found for a rule: "convert-commonjs-to-esm"
No plugin found for a rule: "convert-commonjs-to-esm/require"

There is no more such plugins (they are merged to @putout/plugin-nodejs), read release notes.
Here is updated config for your use case:

{
    "rules": {
        "strict-mode/add-missing": "off",
        "package-json/add-type": "off",
        "remove-console": "off",
        "nodejs/convert-commonjs-to-esm": "on",
        "try-catch/sync": "off"
    }
}

Is it works for you?