Skip to content

Commit e48fee5

Browse files
Initial commit
0 parents  commit e48fee5

File tree

14 files changed

+10191
-0
lines changed

14 files changed

+10191
-0
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage/**/*.js
2+
.nyc_output/**/*.js

.eslintrc.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
env: {
3+
browser: true
4+
},
5+
extends: 'airbnb',
6+
parser: 'babel-eslint',
7+
rules : {
8+
'max-len': ['error', {
9+
'code': 150
10+
}],
11+
'comma-dangle': 0,
12+
'no-plusplus': 0,
13+
'arrow-parens': ['error', 'as-needed'],
14+
'no-multi-assign': 0,
15+
'strict': 0,
16+
'no-console': 0,
17+
'prefer-destructuring': 0,
18+
'function-paren-newline': 0,
19+
'global-require': 0,
20+
'prefer-spread': 0,
21+
'prefer-rest-params': 0,
22+
'prefer-arrow-callback': 0,
23+
'arrow-body-style': 0,
24+
'no-restricted-globals': 0,
25+
'no-restricted-syntax': 0,
26+
'consistent-return': 0,
27+
'no-param-reassign': 0,
28+
'no-underscore-dangle': 0,
29+
'import/no-unresolved': 0,
30+
'import/no-dynamic-require': 0,
31+
'import/no-extraneous-dependencies': 0,
32+
'import/extensions': ['warn', 'ignorePackages']
33+
}
34+
};

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
tmp
3+
4+
node_modules
5+
.npm-debug.log
6+
logs
7+
*.log*
8+
9+
.nyc_output
10+
coverage

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
3+
node_js:
4+
- stable
5+
- "8"
6+
- "6"
7+
8+
after_success: npm run coverage

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
This project adheres to
4+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5+
6+
## 0.0.1 - 2018-08-13
7+
- Initial release.

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Dimitri Nicolas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# postcss-import-ext-glob [![Build Status][travis badge]][travis link] [![Coverage Status][coveralls badge]][coveralls link]
2+
3+
A [PostCSS][postcss] plugin to extend [postcss-import][postcss-import] path
4+
resolver to allow [glob][glob ref] usage as path.
5+
6+
You must use this plugin along with [postcss-import][postcss-import], place this
7+
plugin **before** `postcss-import`.
8+
9+
```css
10+
@import-glob "components/**/*.css";
11+
```
12+
13+
## Installation
14+
15+
```console
16+
$ npm install postcss-import-ext-glob
17+
```
18+
19+
## Usage
20+
21+
```js
22+
// Postcss plugins
23+
postcss([
24+
require('postcss-import-ext-glob'),
25+
require('postcss-import')
26+
]);
27+
```
28+
29+
Check out [PostCSS](https://github.com/postcss/postcss) docs for the complete
30+
installation.
31+
32+
### Example
33+
34+
This plugin transform this:
35+
36+
```css
37+
@import-glob "components/**/*.css";
38+
```
39+
40+
into this:
41+
42+
```css
43+
@import "components/form/input.css";
44+
@import "components/form/submit.css";
45+
/* etc */
46+
```
47+
48+
Thereby, the plugin `postcss-import` can now import each file.
49+
50+
You can pass multiple globs in one:
51+
52+
```css
53+
@import-glob "helpers/**/*.css", "components/**/*.css";
54+
```
55+
56+
## Options
57+
58+
You can pass a `sort` option to this plugin with a value of "asc" or "desc":
59+
60+
```js
61+
// Postcss plugins
62+
postcss([
63+
require('postcss-import-ext-glob')({
64+
sort: 'desc'
65+
}),
66+
require('postcss-import')
67+
]);
68+
```
69+
70+
The sort order is by default ascending.
71+
72+
## Related
73+
74+
- [postcss-import][postcss-import] - PostCSS plugin to inline @import rules
75+
content
76+
- [fast-glob][fast-glob] - Module used for getting glob entries
77+
- [fast-sort][fast-sort] - Module used for sorting glob entries
78+
79+
## License
80+
81+
This project is licensed under the [MIT license](LICENSE).
82+
83+
[travis badge]: https://travis-ci.org/dimitrinicolas/postcss-import-ext-glob.svg?branch=master
84+
[travis link]: https://travis-ci.org/dimitrinicolas/postcss-import-ext-glob
85+
[coveralls badge]: https://coveralls.io/repos/github/dimitrinicolas/postcss-import-ext-glob/badge.svg?branch=master
86+
[coveralls link]: https://coveralls.io/github/dimitrinicolas/postcss-import-ext-glob?branch=master
87+
88+
[postcss]: https://github.com/postcss/postcss
89+
[postcss-import]: https://github.com/postcss/postcss-import
90+
[fast-glob]: https://www.npmjs.com/package/fast-glob
91+
[fast-sort]: https://www.npmjs.com/package/fast-sort
92+
93+
[glob ref]: https://en.wikipedia.org/wiki/Glob_(programming)

fixtures/css/foo/bar.css

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

fixtures/css/foo/foo.css

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

fixtures/css/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
margin: auto;
3+
}

index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const postcss = require('postcss');
2+
const valueParser = require('postcss-value-parser');
3+
4+
const path = require('path');
5+
const fg = require('fast-glob');
6+
const sort = require('fast-sort');
7+
8+
const SORTERS = ['asc', 'desc'];
9+
const DEFAULT_SORTER = 'asc';
10+
11+
module.exports = postcss.plugin('postcss-import-ext-glob', (opts = {}) => {
12+
const sorter = SORTERS.indexOf(opts.sort) > -1
13+
? opts.sort
14+
: DEFAULT_SORTER;
15+
16+
return css => {
17+
const promisesList = [];
18+
19+
css.walkAtRules('import-glob', rule => {
20+
promisesList.push(new Promise((resolve, reject) => {
21+
const globList = [];
22+
const params = valueParser(rule.params).nodes;
23+
24+
const dirname = typeof rule.source.input.file === 'string'
25+
? path.dirname(rule.source.input.file)
26+
: __dirname;
27+
28+
for (const param of params) {
29+
if (param.type === 'string') {
30+
globList.push(path.join(dirname, param.value));
31+
}
32+
}
33+
34+
if (globList.length) {
35+
fg(globList)
36+
.then(entries => {
37+
const sortedEntries = sort(entries)[sorter]();
38+
39+
for (const entry of sortedEntries) {
40+
css.insertBefore(rule, {
41+
name: 'import',
42+
params: `"${entry}"`
43+
});
44+
}
45+
rule.remove();
46+
resolve();
47+
});
48+
} else {
49+
reject(new Error(
50+
`No string found with rule @import-glob ${rule.params}`
51+
));
52+
}
53+
}));
54+
});
55+
56+
return Promise.all(promisesList);
57+
};
58+
});

0 commit comments

Comments
 (0)