Created
November 29, 2018 19:12
-
-
Save BruceL33t/1d7b9cd2100f3b53ea442bb0669e20a3 to your computer and use it in GitHub Desktop.
Resolve modules which have been added using `npm link` or `yarn link`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
// absolute paths to all symlinked modules inside `nodeModulesPath` | |
// adapted from https://github.com/webpack/webpack/issues/811#issuecomment-405199263 | |
module.exports = function findLinkedModules(nodeModulesPath) { | |
const modules = [] | |
fs.readdirSync(nodeModulesPath).forEach(dirname => { | |
const modulePath = path.resolve(nodeModulesPath, dirname) | |
const stat = fs.lstatSync(modulePath) | |
if (dirname.startsWith('.')) { | |
// not a module or scope, ignore | |
} else if (dirname.startsWith('@')) { | |
// scoped modules | |
modules.push(...findLinkedModules(modulePath)) | |
} else if (stat.isSymbolicLink()) { | |
const realPath = fs.realpathSync(modulePath) | |
const realModulePath = path.resolve(realPath, 'node_modules') | |
modules.push(realModulePath) | |
} | |
}) | |
return modules | |
} | |
module.exports = { | |
resolve: { | |
// work with npm link | |
// see https://github.com/webpack/webpack/issues/985 | |
// see https://github.com/vuejs-templates/webpack/pull/688 | |
symlinks: false, | |
modules: [ | |
// provide absolute path to the main node_modules, | |
// to avoid webpack searching around and getting confused | |
// see https://webpack.js.org/configuration/resolve/#resolve-modules | |
path.resolve('node_modules'), | |
// include linked node_modules as fallback, in case the deps haven't | |
// yet propagated to the main node_modules | |
...findLinkedModules(path.resolve('node_modules')), | |
], | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment