Search code examples
javascriptecmascript-6deprecation-warning

ES modules: deprecate export


As a library mantainer, I want to deprecate an old export in favour of a new one.

In order to keep backward compatibility I'd like to temporary keep both exports and warn those users who are still using the old one.

Before

function foo(){}
export {foo as oldExport}

After

function foo(){}
export {foo as newExport}
export {foo as oldExport} // When user import it I'd like to fire a `console.warn`

The only solutions I came up with consist of using external libraries or wrapping the exported function in another deprecated function. Which I don't consider optimal for a small library like the one I'm working on.

Is there any other approach I have overlooked?


Solution

  • There is no official way to do that in JavaScript (as far as I know).

    One way that at least works in IntelliJ IDEA and probably most other IDEs is the one below. It will mark any import and any usage of oldExport as deprecated (by default formatted with a strike-through).

    function foo() {}
    export {foo as newExport}
    
    /**
     * @deprecated
     */
    const oldExport = foo;
    export {oldExport}
    

    See http://usejsdoc.org/tags-deprecated.html

    If a log message is absolutely necessary (I would not do that), then you have to do it by yourself.