Skip to content

Commit

Permalink
Merge pull request #131 from postcss/re-glob
Browse files Browse the repository at this point in the history
Simple glob resolver
  • Loading branch information
MoOx committed Jan 4, 2016
2 parents 2ec41ac + 3c1cc7d commit 0216fa3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See [resolve option](https://github.com/postcss/postcss-import#resolve) for more
([#120](https://github.com/postcss/postcss-import/pull/120))
- Changed: custom resolve should include glob resolver
([#121](https://github.com/postcss/postcss-import/pull/121))
- Changed: glob resolver do not add `moduleDirectories` and parse all uri as glob patterns

# 7.1.3 - 2015-11-05

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Type: `Boolean`
Default: `false`

Set to `true` if you want @import rules to parse glob patterns.
Files will be searched in base directory and paths specified in `path` option.

#### `resolve`

Expand Down
27 changes: 4 additions & 23 deletions lib/resolve-id.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require("fs")
var path = require("path")
var glob = require("glob")
var globby = require("globby")
var resolve = require("resolve")

var moduleDirectories = [
Expand Down Expand Up @@ -33,31 +33,12 @@ function resolveModule(id, opts) {
})
}

function resolveGlob(patterns) {
var files = []
var promises = patterns.map(function(pattern) {
return new Promise(function(resolve, reject) {
glob(pattern, function(err, result) {
if (err) {
return reject(err)
}
files = files.concat(result)
resolve()
})
})
})
return Promise.all(promises).then(function() {
return files
})
}

module.exports = function(id, base, options) {
var paths = options.path

if (options.glob && glob.hasMagic(id)) {
// Remove moduleDirectories from paths
paths = [ base ].concat(paths, moduleDirectories)
return resolveGlob(paths.map(function(p) {
if (options.glob) {
paths = [ base ].concat(paths)
return globby(paths.map(function(p) {
return path.resolve(p, id)
}))
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"index.js"
],
"dependencies": {
"glob": "^5.0.14",
"globby": "^4.0.0",
"object-assign": "^4.0.1",
"postcss": "^5.0.2",
"postcss-value-parser": "^3.2.3",
Expand Down
9 changes: 7 additions & 2 deletions test/glob.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import test from "ava"
import compareFixtures from "./lib/compare-fixtures"
import path from "path"
import glob from "glob"
import globby from "globby"

test("should handle a glob pattern", t => {
return compareFixtures(t, "glob", {
path: [
"fixtures/imports",
"node_modules",
"web_modules",
],
glob: true,
})
})
Expand All @@ -28,7 +33,7 @@ test("should fail silently, skipping the globbed import," +
test("should handle a glob by custom resolver", t => {
return compareFixtures(t, "glob-resolve", {
resolve: (id, base) => {
return glob.sync(path.resolve(base, id))
return globby.sync(path.resolve(base, id))
},
}, {
from: "fixtures/glob-resolve.css",
Expand Down

0 comments on commit 0216fa3

Please sign in to comment.