-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow relative paths in scoped packages #12
Merged
Merged
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ec16eb2
Allow relative paths in scoped packages
GeorgeTaveras1231 230e069
Introduce resolver
GeorgeTaveras1231 9dab304
Fix test run
GeorgeTaveras1231 1c4ce24
Add Package unit tests and unit test script
GeorgeTaveras1231 a8cad50
Add unit tests for Import
GeorgeTaveras1231 4b4fdcf
Add unit tests for resolver
GeorgeTaveras1231 17e3c65
Attempt to be cross platform friendly
GeorgeTaveras1231 3dace1a
Ignore log files and vim swap files
GeorgeTaveras1231 d61b2c4
Use findup implementation again to properly resolve nested dependencies
GeorgeTaveras1231 ca314c7
Update unit tests for Package and resolver
GeorgeTaveras1231 df955e5
Refactor test scripts to run all tests with `npm t`
GeorgeTaveras1231 66f9799
Remove unused dependency
GeorgeTaveras1231 3b89108
Remove string expansion fron test script
GeorgeTaveras1231 f7bad67
Make indentation consistent across source files
GeorgeTaveras1231 f0a7a90
Only ignore node_modules in test-cases and root of project
GeorgeTaveras1231 550fff0
Remove unnecessary fixture files
GeorgeTaveras1231 f74e5bf
Avoid blocking the event loop
GeorgeTaveras1231 0375885
Introduce Promises for async operations
GeorgeTaveras1231 3084a15
Handle exception in nearestPackageRoot test
GeorgeTaveras1231 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,19 @@ | ||
var Package = require('./resolver/package'); | ||
var Import = require('./resolver/import'); | ||
|
||
module.exports = 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 specifiedFilePath() { | ||
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
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,9 @@ | ||
{ | ||
"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", | ||
"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
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'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind refactoring this a little to rename what is now the
test
task, and then havetest
run both of the existing test tasks?That way we can get better coverage in CI.