-
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.
Merge pull request #12 from GeorgeTaveras1231/master
Allow relative paths in scoped packages
- Loading branch information
Showing
17 changed files
with
368 additions
and
56 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
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,17 @@ | ||
var Package = require('./resolver/package'); | ||
var Import = require('./resolver/import'); | ||
var nearestPackageRoot = require('./resolver/nearest-package-root'); | ||
|
||
module.exports = function resolve(importUrl, importOriginPath) { | ||
var _import = new Import(importUrl); | ||
|
||
return nearestPackageRoot(_import.packageName(), importOriginPath).then(function (packageRoot) { | ||
var _package = new Package(packageRoot); | ||
|
||
if (_import.isEntrypoint()) { | ||
return _package.fullPathToEntrypoint(); | ||
} else { | ||
return _package.root(_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,35 @@ | ||
var path = require('path'); | ||
var nearestPackageRoot = require('./nearest-package-root'); | ||
|
||
module.exports = Import; | ||
|
||
function Import(importUrl) { | ||
this.importUrl = importUrl; | ||
} | ||
|
||
Import.prototype.isScoped = function isScoped() { | ||
return this.importUrl[0] === '@'; | ||
}; | ||
|
||
Import.prototype.packageName = function packageName() { | ||
if (this.isScoped()) { | ||
return this.importUrl.split(path.sep, 2).join(path.sep); | ||
} else { | ||
return this.importUrl.split(path.sep, 1)[0]; | ||
} | ||
}; | ||
|
||
Import.prototype.isEntrypoint = function isEntrypoint() { | ||
var safePathSplitPattern = new RegExp(path.sep + '.'); | ||
var pathSegmentCount = this.importUrl.split(safePathSplitPattern).length; | ||
|
||
if (this.isScoped()) { | ||
return pathSegmentCount === 2; | ||
} else { | ||
return pathSegmentCount === 1; | ||
} | ||
}; | ||
|
||
Import.prototype.specifiedFilePath = function specifiedFilePath() { | ||
return this.importUrl.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,29 @@ | ||
var path = require('path'); | ||
var findup = require('findup'); | ||
var Promise = require('bluebird'); | ||
|
||
/** | ||
* @param {string} importOriginPath Path of file that made @import reference | ||
*/ | ||
module.exports = function nearestPackageRoot(packageName, importOriginPath) { | ||
var pathToFind = path.join('node_modules', packageName, 'package.json'); | ||
var dirnameOfImportOrigin = path.dirname(importOriginPath); | ||
var promise, handleFoundPath; | ||
|
||
promise = new Promise(function (resolve, reject) { | ||
handleFoundPath = function (err, nearestPackageParent) { | ||
if(err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
var packageJSONLocation = path.join( nearestPackageParent, 'node_modules', packageName); | ||
|
||
resolve(packageJSONLocation); | ||
}; | ||
}); | ||
|
||
findup(dirnameOfImportOrigin, pathToFind, handleFoundPath); | ||
|
||
return promise; | ||
}; |
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,28 @@ | ||
var path = require('path'); | ||
var nearestPackageRoot = require('./nearest-package-root'); | ||
|
||
module.exports = Package; | ||
|
||
function Package(rootPath) { | ||
this.root = path.join.bind(null, rootPath); | ||
this.JSON = require(this.root('package.json')); | ||
}; | ||
|
||
Package.prototype.fullPathToEntrypoint = function fullPathToEntrypoint() { | ||
return this.root(this.entrypoint()); | ||
}; | ||
|
||
Package.prototype.entrypoint = function entrypoint() { | ||
if (this.JSON.sass) { | ||
return this.JSON.sass; | ||
// look for "style" declaration in package.json | ||
} else if (this.JSON.style) { | ||
return this.JSON.style; | ||
// look for a css/sass/scss file in the "main" declaration in package.json | ||
} else if (/\.(sa|c|sc)ss$/.test(this.JSON.main)) { | ||
return this.JSON.main; | ||
// otherwise assume ./styles.scss | ||
} else { | ||
return 'styles'; | ||
} | ||
}; |
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
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions
3
test/fixtures/package-test/node_modules/test-package/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
@import "@lennym/npm-sass-test-sass/child-file"; |
8 changes: 8 additions & 0 deletions
8
test/test-cases/scoped-packages-with-relative-path/package.json
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,8 @@ | ||
{ | ||
"name": "npm-sass-test-npm-modules", | ||
"version": "0.0.0", | ||
"description": "sass compilation with npm aware include paths", | ||
"dependencies": { | ||
"@lennym/npm-sass-test-sass": "0.0.0" | ||
} | ||
} |
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 Import = require('../../lib/resolver/import'); | ||
var assert = require('assert'); | ||
|
||
describe('Import', function () { | ||
describe('#isScoped', function () { | ||
it('returns true if package is scoped', function () { | ||
var subject1 = new Import('@scoped/package'); | ||
var subject2 = new Import('non-scoped-package'); | ||
|
||
assert(subject1.isScoped()); | ||
assert(!subject2.isScoped()); | ||
}); | ||
}); | ||
|
||
describe('#packageName', function () { | ||
it('returns the assumed package name', function () { | ||
var subject1 = new Import('@scoped/package/nested-path'); | ||
var subject2 = new Import('non-scoped-package/nested-path'); | ||
|
||
assert.equal(subject1.packageName(), '@scoped/package'); | ||
assert.equal(subject2.packageName(), 'non-scoped-package'); | ||
}); | ||
}); | ||
|
||
describe('#isEntrypoint', function () { | ||
it('returns true if the import is for the entrypoint', function () { | ||
var subject1 = new Import('@scoped/package/nested-path'); | ||
var subject2 = new Import('@scoped/package'); | ||
var subject3 = new Import('non-scoped-package/nested-path'); | ||
var subject4 = new Import('non-scoped-package'); | ||
|
||
assert(!subject1.isEntrypoint()); | ||
assert(subject2.isEntrypoint()); | ||
assert(!subject3.isEntrypoint()); | ||
assert(subject4.isEntrypoint()); | ||
}); | ||
}); | ||
|
||
describe('#specifiedFilePath', function () { | ||
it('returns the specified nested path', function () { | ||
var subject = new Import('@scoped/package/nested-path1/nested-path2'); | ||
|
||
assert.equal(subject.specifiedFilePath(), '/nested-path1/nested-path2'); | ||
}); | ||
}); | ||
}); |
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,30 @@ | ||
var nearestPackageRoot = require('../../lib/resolver/nearest-package-root'); | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
|
||
var fixturesPath = path.resolve.bind(null, | ||
__dirname, | ||
'..', | ||
'fixtures', | ||
'nearest-package-root-test' | ||
) | ||
|
||
describe('nearestPackageRoot', function () { | ||
it('finds the nearest module folder based on the import origin', function (done) { | ||
var sourcePath = fixturesPath('index.scss'); | ||
nearestPackageRoot('test-module', sourcePath).then(function (result) { | ||
assert.equal(result, fixturesPath('node_modules', 'test-module')); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
|
||
it('works with nested dependencies', function (done) { | ||
var sourcePath = fixturesPath('node_modules', 'test-module', 'index.scss'); | ||
nearestPackageRoot('nested-module', sourcePath).then(function (result) { | ||
assert.equal(result, fixturesPath('node_modules', 'test-module', 'node_modules', 'nested-module')); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
}); |
Oops, something went wrong.