babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

How to isolate node_modules?

Open avlonder opened this issue 4 years ago • 1 comments

How do I avoid applying the path mapping to the files inside node_modules? If, by coincidence, you pick an alias that resembles one of the imports used inside a third-party package, this can lead to unexpected behavior. I would like to constrain the path mapping only to the files inside src.

avlonder avatar Feb 07 '21 14:02 avlonder

This would be really helpful and would save me from having to rename all my references in my code. For others looking for temporary solution, you could try this:

const {resolvePath} = require('babel-plugin-module-resolver');

module.exports = function (api) {
    return {
        plugins: [['module-resolver', {
                    alias: {...},
                    resolvePath(sourcePath, currentFile, opts) {
                        if (currentFile.indexOf(/node_modules/) > -1) {
                            return undefined;
                        }

                        return resolvePath(sourcePath, currentFile, opts);
                    }
        }]]
    };
};

guikubivan avatar Apr 30 '24 23:04 guikubivan