Skip to content

Instantly share code, notes, and snippets.

@alevshunov
Created October 2, 2019 14:27
Show Gist options
  • Save alevshunov/e8a96ac821d491328b82e415f9bd6174 to your computer and use it in GitHub Desktop.
Save alevshunov/e8a96ac821d491328b82e415f9bd6174 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
const 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