Skip to content

Commit de9e1f2

Browse files
committed
turned resolvePath into generic utility function
there's really no need for this to be an instance method while technically, this constitutes a breaking change (due to `lib/util/resolve.js`), we're pretty sure nobody else was using `resolveModulePath`.
1 parent 841e204 commit de9e1f2

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

lib/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
let AssetManager = require("./manager");
4+
let resolvePath = require("./util/resolve");
45
let { abort, repr } = require("./util");
56
let browserslist = require("browserslist");
67
let SerializedRunner = require("./util/runner");
@@ -61,7 +62,7 @@ module.exports = (referenceDir, config, { watch, fingerprint, sourcemaps, compac
6162
runner.run();
6263

6364
if(watch) {
64-
makeWatcher(config.watchDirs, referenceDir, assetManager.resolvePath).
65+
makeWatcher(config.watchDirs, referenceDir).
6566
on("edit", filepaths => {
6667
runner.rerun(filepaths);
6768
});
@@ -73,11 +74,12 @@ function buildStep(plugins) {
7374
then(() => files);
7475
}
7576

76-
function makeWatcher(watchDirs, referenceDir, resolvePath) {
77+
function makeWatcher(watchDirs, referenceDir) {
7778
let niteOwl = require("nite-owl");
7879

7980
if(watchDirs) {
80-
watchDirs = watchDirs.map(dir => resolvePath(dir, { enforceRelative: true }));
81+
watchDirs = watchDirs.map(dir => resolvePath(dir, referenceDir,
82+
{ enforceRelative: true }));
8183
} else {
8284
watchDirs = [referenceDir];
8385
console.error(`you might consider setting ${repr("watchDirs", false)} ` +

lib/manager.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

33
let Manifest = require("./manifest");
4-
let resolveModulePath = require("./util/resolve");
54
let createFile = require("./util/files");
6-
let { abort, generateFingerprint, repr } = require("./util");
5+
let resolvePath = require("./util/resolve");
6+
let { abort, generateFingerprint } = require("./util");
77
let path = require("path");
88

99
module.exports = class AssetManager {
@@ -12,11 +12,10 @@ module.exports = class AssetManager {
1212
this.fingerprint = fingerprint;
1313
this.exitOnError = exitOnError;
1414

15-
// bind methods for convenience
16-
this.writeFile = this.writeFile.bind(this);
17-
this.resolvePath = this.resolvePath.bind(this);
15+
this.writeFile = this.writeFile.bind(this); // for convenience
16+
this.resolvePath = filepath => resolvePath(filepath, referenceDir);
1817

19-
this.manifest = new Manifest(manifestConfig || {}, this.resolvePath);
18+
this.manifest = new Manifest(referenceDir, manifestConfig || {});
2019
}
2120

2221
// NB: `fingerprint` option takes precedence over corresponding instance property
@@ -44,20 +43,6 @@ module.exports = class AssetManager {
4443
});
4544
}
4645

47-
resolvePath(filepath, { enforceRelative } = {}) {
48-
if(filepath.substr(0, 2) === "./") {
49-
return path.resolve(this.referenceDir, filepath);
50-
} else if(enforceRelative) {
51-
abort(`ERROR: path must be relative: ${repr(filepath)}`);
52-
} else { // attempt via Node resolution algorithm
53-
try {
54-
return resolveModulePath(filepath, this.referenceDir);
55-
} catch(err) {
56-
abort(`ERROR: could not resolve ${repr(filepath)}`);
57-
}
58-
}
59-
}
60-
6146
get packagesDir() {
6247
let memo = this._packagesDir;
6348
if(!memo) {

lib/manifest.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"use strict";
22

3-
let path = require("path");
43
let createFile = require("./util/files");
4+
let resolvePath = require("./util/resolve");
55
let { abort } = require("./util");
6+
let path = require("path");
67

78
module.exports = class Manifest {
8-
constructor({ target, key, value, baseURI, webRoot }, resolvePath) {
9+
constructor(referenceDir, { target, key, value, baseURI, webRoot }) {
910
if(target) {
10-
this.filepath = resolvePath(target, {
11+
this.filepath = resolvePath(target, referenceDir, {
1112
enforceRelative: true
1213
});
1314
}
@@ -26,7 +27,8 @@ module.exports = class Manifest {
2627
this.valueTransform = value;
2728
} else {
2829
baseURI = baseURI || "/";
29-
webRoot = resolvePath(webRoot || "./", { enforceRelative: true });
30+
webRoot = resolvePath(webRoot || "./", referenceDir,
31+
{ enforceRelative: true });
3032
this.valueTransform = filepath => baseURI + path.relative(webRoot, filepath);
3133
}
3234
}

lib/util/resolve.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use strict";
22

3+
let { abort, repr } = require("./");
4+
let path = require("path");
5+
36
// older versions of Node do not support `require.resolve`'s `paths` option
47
let legacy = !require.resolve.paths;
58
if(!legacy) { // account for bug in Node v8.9.4 and below
@@ -10,7 +13,21 @@ if(!legacy) { // account for bug in Node v8.9.4 and below
1013
}
1114
}
1215

13-
module.exports = function resolveModulePath(filepath, rootDir) {
16+
module.exports = function resolvePath(filepath, referenceDir, { enforceRelative } = {}) {
17+
if(filepath.substr(0, 2) === "./") {
18+
return path.resolve(referenceDir, filepath);
19+
} else if(enforceRelative) {
20+
abort(`ERROR: path must be relative: ${repr(filepath)}`);
21+
} else { // attempt via Node resolution algorithm
22+
try {
23+
return resolveModulePath(filepath, referenceDir);
24+
} catch(err) {
25+
abort(`ERROR: could not resolve ${repr(filepath)}`);
26+
}
27+
}
28+
};
29+
30+
function resolveModulePath(filepath, rootDir) {
1431
if(legacy) {
1532
legacy = process.env.NODE_PATH; // cache previous value
1633
rootDir = rootDir.replace(/\/{1,}$/, ""); // strip trailing slashes, to be safe
@@ -26,4 +43,4 @@ module.exports = function resolveModulePath(filepath, rootDir) {
2643
}
2744

2845
return res;
29-
};
46+
}

0 commit comments

Comments
 (0)