tleunen / babel-plugin-module-resolver

Custom module resolver plugin for Babel
MIT License
3.45k stars 204 forks source link

How to isolate node_modules? #423

Open avlonder opened 3 years ago

avlonder commented 3 years ago

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.

guikubivan commented 4 months ago

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);
                    }
        }]]
    };
};