Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
[1.2.3] Handling absolute imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sp00m committed Jul 30, 2017
1 parent a86d155 commit ff36477
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ You can watch for CSS files modifications in addition to SASS ones. In this case

This object is passed to the [`gulp-watch` options](https://www.npmjs.com/package/gulp-watch#options) directly.

##### options.base

Type: `String`
Default: `"."`

Used to resolve absolute `@import`s (cwd / base / path).

## Why?

### `gulp.watch` recompiles all the SASS files:
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ const ImportTree = (() => {
return resolveImportCandidates.call(this, importingFile, importedPath, candidates)
}

function resolveImportedPath(importingFile, importedPath) {
return importedPath.startsWith("/")
? path.resolve(this.cwd, this.base, importedPath.substring(1))
: path.resolve(importingFile.replace(FILENAME, ""), importedPath)
}

function resolveImport(importingFile, importedPath) {
const importedFile = path.resolve(importingFile.replace(FILENAME, ""), importedPath)
const importedFile = resolveImportedPath.call(this, importingFile, importedPath)
gatherImport.call(this, importingFile, STYLESHEET_EXTENSION.test(importedFile)
? resolveImportWithExtension.call(this, importingFile, importedPath, importedFile)
: resolveImportWithoutExtension.call(this, importingFile, importedPath, importedFile))
Expand All @@ -81,7 +87,7 @@ const ImportTree = (() => {
constructor(globs, options = {}) {
this.globs = globs
this.cwd = options.cwd || process.cwd()
this.base = options.cwd || "."
this.base = options.base || "."
this.warn = options.warn || ((message) => gutil.log(gutil.colors.yellow(message)))
this.desc = {}
this.asc = {}
Expand Down
15 changes: 15 additions & 0 deletions test/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ describe("gulp-watch-sass", () => {
globby.sync("*.scss", { cwd }).forEach(remove)
})

it("should handle absolute paths", () => {

create("a.scss", "@import '/b.scss';")
create("b.scss", "div { margin: 0; }")

const tree = new ImportTree("*.scss", { cwd, warn: console.warn }).build()
const handler = new EventHandler(tree)

const stream = handler.change(toVinyl("b.scss"), [])
assertStreamContainsOnly(stream, "a.scss")

console.warn.called.should.be.false()

})

it("should handle SASS files with .scss extension", () => {

create("a.scss", "@import 'b.scss';")
Expand Down

0 comments on commit ff36477

Please sign in to comment.