1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/root-module-relative-path.js
Fin Hopkins 8a5250985f Changes hard-coded module aliases to be relative (#1164)
Without this, modules built with Babel or Webpack would have hard-coded absolute paths
all the way back to the root of the filesystem. This prevented compilation and running
on different machines or even from different directories on the same machine.

With this change, paths are hard-coded to the top-most node_madules directory found,
which should make them portable relative to the app.

Fixes #1160
2017-02-16 07:37:29 +05:30

25 lines
895 B
JavaScript

// Next.js needs to use module.resolve to generate paths to modules it includes,
// but those paths need to be relative to something so that they're portable
// across directories and machines.
//
// This function returns paths relative to the top-level 'node_modules'
// directory found in the path. If none is found, returns the complete path.
const RELATIVE_START = 'node_modules/'
// Pass in the module's `require` object since it's module-specific.
export default (moduleRequire) => (path) => {
// package.json removed because babel-runtime is resolved as
// "babel-runtime/package"
const absolutePath = moduleRequire.resolve(path)
.replace(/[\\/]package\.json$/, '')
const relativeStartIndex = absolutePath.indexOf(RELATIVE_START)
if (relativeStartIndex === -1) {
return absolutePath
}
return absolutePath.substring(relativeStartIndex + RELATIVE_START.length)
}