-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The resolver module implements a path resolving algorithm that leverages node's own require. This has a couple benefits: * more likely to continue working as expected when features are introduced to npm/node * reduction of code duplication (npm/node already implements the module resolution we are interested in for this project) * allow the use of NODE_PATH variable to control module directory Additionally, `resolver` leverages `Package` and `Import` constructors which implement some of the same logic previously found on the `find` function in `lib/importer.js`. The benefit of creating these constructors will obvious when unit tests are introduced for this project. `Package`: implements logic related to retrieving package data, such as getting the package.json, and getting the sass 'main' file for a given package. `Import`: implements logic to make sense of a sass import.
- Loading branch information
1 parent
ec16eb2
commit 230e069
Showing
5 changed files
with
104 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var Package = require('./resolver/package'); | ||
var Import = require('./resolver/import'); | ||
|
||
exports.Resolver = function Resolver(requireFn) { | ||
/* Facilitate testing by allowing requireFn to be specified */ | ||
requireFn = (requireFn || require) ; | ||
|
||
return function resolve(sassImportPath) { | ||
var _import = new Import(sassImportPath); | ||
var _package = new Package(_import.packageName(), requireFn); | ||
|
||
if (_import.isEntrypoint()) { | ||
return _package.resolveEntrypoint(); | ||
} else { | ||
return _package.safeResolve(_import.specifiedFilePath()); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var path = require('path'); | ||
|
||
module.exports = Import; | ||
|
||
function Import(sassImportPath) { | ||
this.sassImportPath = sassImportPath; | ||
}; | ||
|
||
Import.prototype.isScoped = function isScoped() { | ||
return this.sassImportPath[0] === '@'; | ||
}; | ||
|
||
Import.prototype.packageName = function packageName() { | ||
if (this.isScoped()) { | ||
return this.sassImportPath.split(path.sep, 2).join(path.sep); | ||
} else { | ||
return this.sassImportPath.split(path.sep, 1)[0]; | ||
} | ||
}; | ||
|
||
Import.prototype.isEntrypoint = function isEntrypoint() { | ||
var safePathSplitPattern = new RegExp(path.sep + '.'); | ||
var pathSegmentCount = this.sassImportPath.split(safePathSplitPattern).length; | ||
|
||
if (this.isScoped()) { | ||
return pathSegmentCount === 2; | ||
} else { | ||
return pathSegmentCount === 1; | ||
} | ||
}; | ||
|
||
Import.prototype.specifiedFilePath = function specifiedFileName() { | ||
return this.sassImportPath.slice(this.packageName().length); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var path = require('path'); | ||
|
||
module.exports = Package; | ||
|
||
function Package(name, requireFn) { | ||
this.requireFn = requireFn; | ||
|
||
this.path = path.join.bind(null, name); | ||
}; | ||
|
||
Package.prototype.json = function packageJSON() { | ||
return this.requireFn(this.path('package.json')); | ||
}; | ||
|
||
Package.prototype.resolve = function resolve(path) { | ||
return this.requireFn.resolve(this.path(path)); | ||
}; | ||
|
||
Package.prototype.safeResolve = function safeResolve(potentiallyNonExistentPath) { | ||
return path.join(this.dir(), potentiallyNonExistentPath); | ||
}; | ||
|
||
Package.prototype.dir = function dir() { | ||
return path.dirname(this.resolve('package.json')); | ||
}; | ||
|
||
Package.prototype.entrypoint = function entrypoint() { | ||
var packageJson = this.json(); | ||
|
||
if (packageJson.sass) { | ||
return packageJson.sass; | ||
// look for "style" declaration in package.json | ||
} else if (packageJson.style) { | ||
return packageJson.style; | ||
// look for a css/sass/scss file in the "main" declaration in package.json | ||
} else if (/\.(sa|c|sc)ss$/.test(packageJson.main)) { | ||
return packageJson.main; | ||
// otherwise assume ./styles.scss | ||
} else { | ||
return 'styles'; | ||
} | ||
}; | ||
|
||
Package.prototype.resolveEntrypoint = function resolveEntrypoint() { | ||
return this.safeResolve(this.entrypoint()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters