Skip to content

Instantly share code, notes, and snippets.

@BruceL33t
Created November 29, 2018 19:12
Show Gist options
  • Save BruceL33t/1d7b9cd2100f3b53ea442bb0669e20a3 to your computer and use it in GitHub Desktop.
Save BruceL33t/1d7b9cd2100f3b53ea442bb0669e20a3 to your computer and use it in GitHub Desktop.
Resolve modules which have been added using `npm link` or `yarn link`
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