{"action":"created","issue":{"url":"https://api.github.com/repos/webpack/webpack/issues/2031","repository_url":"https://api.github.com/repos/webpack/webpack","labels_url":"https://api.github.com/repos/webpack/webpack/issues/2031/labels{/name}","comments_url":"https://api.github.com/repos/webpack/webpack/issues/2031/comments","events_url":"https://api.github.com/repos/webpack/webpack/issues/2031/events","html_url":"https://github.com/webpack/webpack/issues/2031","id":133042067,"number":2031,"title":"How to exclude node_modules but one","user":{"login":"borm","id":4027842,"avatar_url":"https://avatars3.githubusercontent.com/u/4027842?v=4","gravatar_id":"","url":"https://api.github.com/users/borm","html_url":"https://github.com/borm","followers_url":"https://api.github.com/users/borm/followers","following_url":"https://api.github.com/users/borm/following{/other_user}","gists_url":"https://api.github.com/users/borm/gists{/gist_id}","starred_url":"https://api.github.com/users/borm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/borm/subscriptions","organizations_url":"https://api.github.com/users/borm/orgs","repos_url":"https://api.github.com/users/borm/repos","events_url":"https://api.github.com/users/borm/events{/privacy}","received_events_url":"https://api.github.com/users/borm/received_events","type":"User","site_admin":false},"labels":[{"id":1089824,"url":"https://api.github.com/repos/webpack/webpack/labels/question","name":"question","color":"cc317c","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2016-02-11T17:55:09Z","updated_at":"2017-07-25T00:04:33Z","closed_at":"2016-08-12T18:15:40Z","body":"Subj,\nas example\nI create some module in another folder ( /projects/MY_MODULE )\nMY_MODULE not compiled, source code\nthen run npm link\nafter go to my project and run npm link MY_MODULE\nCompile my project and have error two copies React\n\ni`am try\n\n```\n{\n        test: /\\.js$/,\n        exclude: [/node_modules/],\n        include: [/node_modules\\/MY_MODULE/]\n      },\n```\n"},"comment":{"url":"https://api.github.com/repos/webpack/webpack/issues/comments/317589620","html_url":"https://github.com/webpack/webpack/issues/2031#issuecomment-317589620","issue_url":"https://api.github.com/repos/webpack/webpack/issues/2031","id":317589620,"user":{"login":"jh3141","id":6211701,"avatar_url":"https://avatars2.githubusercontent.com/u/6211701?v=4","gravatar_id":"","url":"https://api.github.com/users/jh3141","html_url":"https://github.com/jh3141","followers_url":"https://api.github.com/users/jh3141/followers","following_url":"https://api.github.com/users/jh3141/following{/other_user}","gists_url":"https://api.github.com/users/jh3141/gists{/gist_id}","starred_url":"https://api.github.com/users/jh3141/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jh3141/subscriptions","organizations_url":"https://api.github.com/users/jh3141/orgs","repos_url":"https://api.github.com/users/jh3141/repos","events_url":"https://api.github.com/users/jh3141/events{/privacy}","received_events_url":"https://api.github.com/users/jh3141/received_events","type":"User","site_admin":false},"created_at":"2017-07-25T00:04:33Z","updated_at":"2017-07-25T00:04:33Z","body":"Building on @nowells suggestion above and incorporating the comment from @lxjwlt about Windows paths being different, I decided to make a function to build the necessary regexps automatically with the correct path separator:\r\n\r\n```\r\nfunction excludeNodeModulesExcept (modules)\r\n{\r\n    var pathSep = path.sep;\r\n    if (pathSep == '\\\\') // must be quoted for use in a regexp:\r\n        pathSep = '\\\\\\\\';\r\n    var moduleRegExps = modules.map (function (modName) { return new RegExp(\"node_modules\" + pathSep + modules)})\r\n\r\n    return function (modulePath) {\r\n        if (/node_modules/.test(modulePath)) {\r\n            for (var i = 0; i < moduleRegExps.length; i ++)\r\n                if (moduleRegExps[i].test(modulePath)) return false;\r\n            return true;\r\n        }\r\n        return false;\r\n    };\r\n}\r\n```\r\n\r\nUsage is to put the above function in your preamble, and then call it to generate the \"exclude\" value, e.g.:\r\n\r\n```\r\nmodule.exports = {\r\n  module: {\r\n      rules: [\r\n          {\r\n              test: /\\.js$/,\r\n              loader: 'babel-loader',\r\n              options: { presets: ['es2017', 'es2016', 'es2015' ] },\r\n              exclude: excludeNodeModulesExcept([\"lru_map\"])\r\n          },\r\n```\r\n\r\nYou can add however many modules you need to exclude from exclusion to the list, although note that the test is O(n) in the number of modules, so if you have a *lot* of exclusions to process it may be worth finding a better way."}}
{"actor":{"display_login":"jh3141"}}