Skip to content

Commit d169e75

Browse files
committed
Infer whether file is a glob or module
This builds on the efforts by @toomuchdesign in #7 and #13. Instead of using a `glob` option and being forced to pick one or the other it now uses `isGlob` to detect what kind of resolve function should be used. This allows both types to be used. Example: ```css @import "suitcss-utils-display"; /* node_modules */ @import "./theme.css"; /* relative path */ @import "./components/*.css" /* glob */ ```
1 parent 6188172 commit d169e75

File tree

8 files changed

+72
-12
lines changed

8 files changed

+72
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## HEAD
4+
5+
- Remove `glob` option and infer it from the path to the file.
6+
37
## 1.0.1
48

59
- Fix `package.json` files filter

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ postcss([ require('postcss-easy-import') ])
1515

1616
See [PostCSS] docs for examples for your environment.
1717

18+
## Resolving files with globs
19+
20+
The path to the file will be checked and if it contains a glob it will be used
21+
to locate it. These can be mixed and matched with normal module paths:
22+
23+
```css
24+
@import "suitcss-utils-display"; /* node_modules */
25+
@import "./theme.css"; /* relative path */
26+
@import "./components/*.css"; /* glob */
27+
```
28+
1829
## Options
1930

2031
This plugin is a [postcss-import] extension which introduces its own `resolve` option.
2132

22-
### prefix
33+
### `prefix`
2334

2435
Type: `false` or `string`
2536
Default: `false`
@@ -31,20 +42,13 @@ Allows partial-like importing with a prefix before the filename.
3142
/* will import modules/_partial.css */
3243
```
3344

34-
### extensions
45+
### `extensions`
3546

3647
Type: `array` or `string`
3748
Default: `.css`
3849

3950
Defines file extensions which will be looked for.
4051

41-
### glob
42-
43-
Type: `boolean`
44-
Default: `false`
45-
46-
Disables module lookup and imports modules based on a glob pattern.
47-
4852
# License
4953

5054
MIT © [Bogdan Chadkin](mailto:trysound@yandex.ru)

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
var postcss = require('postcss');
22
var assign = require('object-assign');
33
var postcssImport = require('postcss-import');
4+
var isGlob = require('is-glob');
45
var resolveGlob = require('./lib/resolve-glob.js');
56
var resolveModule = require('./lib/resolve-module.js');
67

8+
function resolve(id) {
9+
var resolver = isGlob(id) ? resolveGlob : resolveModule;
10+
return resolver.apply(null, arguments);
11+
}
12+
713
module.exports = postcss.plugin('postcss-easy-import', function (opts) {
814
opts = assign({
915
prefix: false,
10-
extensions: '.css',
11-
glob: false
16+
extensions: '.css'
1217
}, opts);
1318

14-
opts.resolve = opts.glob ? resolveGlob : resolveModule;
19+
opts.resolve = resolve;
1520

1621
if (opts.prefix && typeof opts.prefix !== 'string') {
1722
throw Error(

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"homepage": "https://github.com/TrySound/postcss-easy-import",
2020
"dependencies": {
2121
"globby": "^6.1.0",
22+
"is-glob": "^3.1.0",
2223
"object-assign": "^4.0.1",
2324
"pify": "^2.3.0",
2425
"postcss": "^5.0.15",

test/easy-import.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import test from 'ava';
22
import easyImport from '../';
3+
import path from 'path';
4+
import postcss from 'postcss';
35

46
const msg = err => 'postcss-easy-import: ' + err;
57

8+
function preprocess(input, output, opts, t) {
9+
return postcss([ easyImport(opts) ]).process(input)
10+
.then(result => {
11+
t.is(result.css, output);
12+
t.is(result.warnings().length, 0);
13+
});
14+
}
15+
616
test('should fail on incorrect \'prefix\'', t => {
717
t.throws(() => {
818
easyImport({
@@ -68,3 +78,30 @@ test('should not fail on correct \'extensions\'', t => {
6878
});
6979
});
7080
});
81+
82+
test('should handle glob imports', t => {
83+
return preprocess(
84+
'@import "./*.css";\n',
85+
'.bar {\n color: green;\n}\n.foo {\n color: red;\n}\n',
86+
{ root: path.resolve('./fixtures/integration') },
87+
t
88+
);
89+
});
90+
91+
test('should handle module imports', t => {
92+
return preprocess(
93+
'@import "./module/baz.css";\n',
94+
'.baz {\n color: blue;\n}\n',
95+
{ root: path.resolve('./fixtures/integration') },
96+
t
97+
);
98+
});
99+
100+
test('should handle glob and module imports together', t => {
101+
return preprocess(
102+
'@import "./module/baz.css";\n @import "./*.css";',
103+
'.baz {\n color: blue;\n}\n .bar {\n color: green;\n}\n .foo {\n color: red;\n}', // eslint-disable-line max-len
104+
{ root: path.resolve('./fixtures/integration') },
105+
t
106+
);
107+
});

test/fixtures/integration/bar.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bar {
2+
color: green;
3+
}

test/fixtures/integration/foo.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.baz {
2+
color: blue;
3+
}

0 commit comments

Comments
 (0)