Skip to content

Commit

Permalink
ensured path resolution on legacy Node supports local modules
Browse files Browse the repository at this point in the history
note that path resolution for third-party packages currently fails on
Node v8 if CWD is not the same as `referenceDir`:
nodejs/node#18686
  • Loading branch information
FND committed Feb 19, 2018
1 parent a16f577 commit cbf2426
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ language: node_js
node_js:
- 6
- 8
- 9

before_install:
- sudo apt-get -qq update
- sudo apt-get install -y realpath

script:
- npm test
2 changes: 1 addition & 1 deletion lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function resolveModulePath(filepath, rootDir) {
if(legacy) {
legacy = process.env.NODE_PATH; // cache previous value
rootDir = rootDir.replace(/\/{1,}$/, ""); // strip trailing slashes, to be safe
process.env.NODE_PATH = rootDir + "/node_modules";
process.env.NODE_PATH = `${rootDir}:${rootDir}/node_modules`;
require("module").Module._initPaths();
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/dummy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "SRC";
1 change: 1 addition & 0 deletions test/fixtures/dummy/src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "SRC";
1 change: 1 addition & 0 deletions test/fixtures/node_modules/dummy/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/node_modules/dummy/pkg.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions test/test_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global describe, before, after, it */
"use strict";

let AssetManager = require("../lib/manager");
let path = require("path");
let assert = require("assert");

let assertSame = assert.strictEqual;

describe("asset manager", _ => {
let cwd;

before(() => {
cwd = process.cwd();
});

after(() => {
process.chdir(cwd);
});

it("resolves file paths for local modules and third-party packages", () => {
let root = path.resolve(__dirname, "fixtures");
process.chdir(root);

let { resolvePath } = new AssetManager(root);

let filepath = resolvePath("dummy/src.js");
assertSame(path.relative(root, filepath), "dummy/src.js");

filepath = resolvePath("dummy/pkg.js");
assertSame(path.relative(root, filepath), "node_modules/dummy/pkg.js");

// local modules take precedence over third-party packages
["dummy", "dummy/index", "dummy/index.js"].forEach(module => {
let filepath = resolvePath(module);
assertSame(path.relative(root, filepath), "dummy/index.js");
});
});
});

0 comments on commit cbf2426

Please sign in to comment.