You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.3 KiB

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var path = require('path');
Cache modules based on filename rather than ID This is ever so slightly less efficient than caching based on ID, since the filename has to be looked up before we can check the cache. However, it's the most minimal approach possible to get this change in place. Since require() is a blocking startup-time operation anyway, a bit of slowness is not a huge problem. A test involving require.paths modification and absolute loading. Here's the gist of it. Files: /p1/foo.js /p2/foo.js 1. Add "/p1" to require.paths. 2. foo1 = require("foo") 3. assert foo1 === require("/p1/foo") (fail) 4. Remove /p1 from require.paths. 5. Add /p2 to require.paths. 6. foo2 = require("foo") 7. assert foo1 !== foo2 (fail) 8. assert foo2 === require("/p2/foo") (fail) It's an edge case, but it affects how dependencies are mapped by npm. If your module requires foo-1.2.3, and my module requires foo-2.3.4, then you should expect to have require("foo") give you foo-1.2.3, and I should expect require("foo") to give me foo-2.3.4. However, with module ID based caching, if your code loads *first*, then your "foo" is THE "foo", so I'll get your version instead of mine. It hasn't yet been a problem, but only because there are so few modules, and everyone pretty much uses the latest version all the time. But as things start to get to the 1.x and 2.x versions, it'll be an issue, I'm sure. Dependency hell isn't fun, so this is a way to avoid it before it strikes.
14 years ago
require.paths.unshift(path.join(__dirname, '../p2'));
Cache modules based on filename rather than ID This is ever so slightly less efficient than caching based on ID, since the filename has to be looked up before we can check the cache. However, it's the most minimal approach possible to get this change in place. Since require() is a blocking startup-time operation anyway, a bit of slowness is not a huge problem. A test involving require.paths modification and absolute loading. Here's the gist of it. Files: /p1/foo.js /p2/foo.js 1. Add "/p1" to require.paths. 2. foo1 = require("foo") 3. assert foo1 === require("/p1/foo") (fail) 4. Remove /p1 from require.paths. 5. Add /p2 to require.paths. 6. foo2 = require("foo") 7. assert foo1 !== foo2 (fail) 8. assert foo2 === require("/p2/foo") (fail) It's an edge case, but it affects how dependencies are mapped by npm. If your module requires foo-1.2.3, and my module requires foo-2.3.4, then you should expect to have require("foo") give you foo-1.2.3, and I should expect require("foo") to give me foo-2.3.4. However, with module ID based caching, if your code loads *first*, then your "foo" is THE "foo", so I'll get your version instead of mine. It hasn't yet been a problem, but only because there are so few modules, and everyone pretty much uses the latest version all the time. But as things start to get to the 1.x and 2.x versions, it'll be an issue, I'm sure. Dependency hell isn't fun, so this is a way to avoid it before it strikes.
14 years ago
exports.foo = require('foo');
Cache modules based on filename rather than ID This is ever so slightly less efficient than caching based on ID, since the filename has to be looked up before we can check the cache. However, it's the most minimal approach possible to get this change in place. Since require() is a blocking startup-time operation anyway, a bit of slowness is not a huge problem. A test involving require.paths modification and absolute loading. Here's the gist of it. Files: /p1/foo.js /p2/foo.js 1. Add "/p1" to require.paths. 2. foo1 = require("foo") 3. assert foo1 === require("/p1/foo") (fail) 4. Remove /p1 from require.paths. 5. Add /p2 to require.paths. 6. foo2 = require("foo") 7. assert foo1 !== foo2 (fail) 8. assert foo2 === require("/p2/foo") (fail) It's an edge case, but it affects how dependencies are mapped by npm. If your module requires foo-1.2.3, and my module requires foo-2.3.4, then you should expect to have require("foo") give you foo-1.2.3, and I should expect require("foo") to give me foo-2.3.4. However, with module ID based caching, if your code loads *first*, then your "foo" is THE "foo", so I'll get your version instead of mine. It hasn't yet been a problem, but only because there are so few modules, and everyone pretty much uses the latest version all the time. But as things start to get to the 1.x and 2.x versions, it'll be an issue, I'm sure. Dependency hell isn't fun, so this is a way to avoid it before it strikes.
14 years ago
exports.expect = require(path.join(__dirname, '../p2/bar'));
Cache modules based on filename rather than ID This is ever so slightly less efficient than caching based on ID, since the filename has to be looked up before we can check the cache. However, it's the most minimal approach possible to get this change in place. Since require() is a blocking startup-time operation anyway, a bit of slowness is not a huge problem. A test involving require.paths modification and absolute loading. Here's the gist of it. Files: /p1/foo.js /p2/foo.js 1. Add "/p1" to require.paths. 2. foo1 = require("foo") 3. assert foo1 === require("/p1/foo") (fail) 4. Remove /p1 from require.paths. 5. Add /p2 to require.paths. 6. foo2 = require("foo") 7. assert foo1 !== foo2 (fail) 8. assert foo2 === require("/p2/foo") (fail) It's an edge case, but it affects how dependencies are mapped by npm. If your module requires foo-1.2.3, and my module requires foo-2.3.4, then you should expect to have require("foo") give you foo-1.2.3, and I should expect require("foo") to give me foo-2.3.4. However, with module ID based caching, if your code loads *first*, then your "foo" is THE "foo", so I'll get your version instead of mine. It hasn't yet been a problem, but only because there are so few modules, and everyone pretty much uses the latest version all the time. But as things start to get to the 1.x and 2.x versions, it'll be an issue, I'm sure. Dependency hell isn't fun, so this is a way to avoid it before it strikes.
14 years ago
exports.actual = exports.foo.bar;