coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement 💪 with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
705 stars 40 forks source link

convert-commonjs-to-esm/exports not working #191

Closed ngugcx closed 11 months ago

ngugcx commented 11 months ago

.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
coderaiser commented 11 months ago

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 11 months ago

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

coderaiser commented 11 months ago

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

coderaiser commented 11 months ago

Is it works for you?

ngugcx commented 11 months ago

I want to convert it to es module export.

coderaiser commented 11 months ago

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

ngugcx commented 11 months ago

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"
coderaiser commented 11 months ago

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?