postcss-import-ext-glob
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -6,3 +6,8 @@ # Change Log | ||
## 0.0.1 - 2018-08-13 | ||
- Initial release. | ||
## 1.1.0 - 2018-08-15 | ||
### Added | ||
- Source for new nodes to generate an accurate source map. | ||
- Warning when no file found for a glob. | ||
## 1.0.0 - 2018-08-13 | ||
- Initial release. |
25
index.js
@@ -16,7 +16,7 @@ const postcss = require('postcss'); | ||
return css => { | ||
return (root, result) => { | ||
const promisesList = []; | ||
css.walkAtRules('import-glob', rule => { | ||
promisesList.push(new Promise((resolve, reject) => { | ||
root.walkAtRules('import-glob', rule => { | ||
promisesList.push(new Promise(resolve => { | ||
const globList = []; | ||
@@ -38,8 +38,16 @@ const params = valueParser(rule.params).nodes; | ||
.then(entries => { | ||
if (!entries.length) { | ||
result.warn( | ||
`No file found for @import-glob ${rule.params}`, | ||
{ node: rule } | ||
); | ||
} | ||
const sortedEntries = sort(entries)[sorter](); | ||
for (const entry of sortedEntries) { | ||
css.insertBefore(rule, { | ||
root.insertBefore(rule, { | ||
name: 'import', | ||
params: `"${entry}"` | ||
params: `"${entry}"`, | ||
source: rule.source | ||
}); | ||
@@ -51,5 +59,6 @@ } | ||
} else { | ||
reject(new Error( | ||
`No string found with rule @import-glob ${rule.params}` | ||
)); | ||
throw rule.error( | ||
`No string found with rule @import-glob ${rule.params}`, | ||
{ plugin: 'postcss-import-ext-glob' } | ||
); | ||
} | ||
@@ -56,0 +65,0 @@ })); |
{ | ||
"name": "postcss-import-ext-glob", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A PostCSS plugin to extend postcss-import path resolver to allow glob usage as path", | ||
@@ -20,3 +20,4 @@ "license": "MIT", | ||
"extension", | ||
"glob" | ||
"glob", | ||
"postcss-plugin" | ||
], | ||
@@ -28,3 +29,3 @@ "bugs": { | ||
"scripts": { | ||
"publish": "clean-publish --files fixtures" | ||
"publish": "clean-publish --files .nyc_output coverage fixtures" | ||
}, | ||
@@ -31,0 +32,0 @@ "dependencies": { |
# postcss-import-ext-glob [![Build Status][travis badge]][travis link] [![Coverage Status][coveralls badge]][coveralls link] | ||
A [PostCSS][postcss] plugin to extend [postcss-import][postcss-import] path | ||
resolver to allow [glob][glob ref] usage as path. | ||
A [PostCSS][postcss] plugin to extend [postcss-import][postcss-import] path | ||
resolver to allow [glob][glob ref] usage as a path. | ||
You must use this plugin along with [postcss-import][postcss-import], place this | ||
plugin **before** `postcss-import`. | ||
You must use this plugin along with [postcss-import][postcss-import], place | ||
this plugin **before** `postcss-import`. | ||
```scss | ||
```pcss | ||
@import-glob "components/**/*.css"; | ||
@@ -29,3 +29,3 @@ ``` | ||
Check out [PostCSS](https://github.com/postcss/postcss) docs for the complete | ||
Check out [PostCSS](https://github.com/postcss/postcss) docs for the complete | ||
installation. | ||
@@ -37,3 +37,3 @@ | ||
```scss | ||
```pcss | ||
@import-glob "components/**/*.css"; | ||
@@ -44,3 +44,3 @@ ``` | ||
```css | ||
```pcss | ||
@import "components/form/input.css"; | ||
@@ -55,3 +55,3 @@ @import "components/form/submit.css"; | ||
```scss | ||
```pcss | ||
@import-glob "helpers/**/*.css", "components/**/*.css"; | ||
@@ -78,6 +78,8 @@ ``` | ||
- [postcss-import][postcss-import] - PostCSS plugin to inline @import rules | ||
- [postcss-import][postcss-import] - PostCSS plugin to inline @import rules | ||
content | ||
- [fast-glob][fast-glob] - Module used for getting glob entries | ||
- [fast-sort][fast-sort] - Module used for sorting glob entries | ||
- [ava-postcss-tester][ava-postcss-tester] - Simply test your PostCSS plugin | ||
with AVA | ||
@@ -97,3 +99,4 @@ ## License | ||
[fast-sort]: https://www.npmjs.com/package/fast-sort | ||
[ava-postcss-tester]: https://github.com/dimitrinicolas/ava-postcss-tester | ||
[glob ref]: https://en.wikipedia.org/wiki/Glob_(programming) |
183
test.js
import test from 'ava'; | ||
import PostcssTester from 'ava-postcss-tester'; | ||
@@ -8,66 +9,52 @@ import postcss from 'postcss'; | ||
const testPostcss = async (input, output, t, opts = {}) => { | ||
const plugins = []; | ||
const tester = new PostcssTester({ | ||
postcss, | ||
plugin: postcssImportExtGlob | ||
}); | ||
for (const plugin of opts.pluginsBefore || []) { | ||
plugins.push(plugin); | ||
} | ||
plugins.push(opts.plugin || postcssImportExtGlob()); | ||
for (const plugin of opts.pluginsAfter || []) { | ||
plugins.push(plugin); | ||
} | ||
test('simple test', async t => { | ||
const input = /* scss */` | ||
@import-glob "fixtures/css/foo/**/*.css"; | ||
`; | ||
const output = /* scss */` | ||
@import "${__dirname}/fixtures/css/foo/bar.css"; | ||
@import "${__dirname}/fixtures/css/foo/foo.css"; | ||
`; | ||
await tester.test(input, output, t); | ||
}); | ||
if (output instanceof Error) { | ||
t.plan(1); | ||
} else { | ||
// t.plan(2); | ||
t.plan(1); | ||
} | ||
await postcss(plugins).process(input, { from: '' }).then(result => { | ||
if (output instanceof Error) { | ||
t.fail(); | ||
return; | ||
test('simple test postcss-import', async t => { | ||
const input = /* scss */` | ||
@import-glob "fixtures/css/foo/**/*.css"; | ||
`; | ||
const output = /* scss */` | ||
.bar { | ||
display: inline-block; | ||
} | ||
t.is(result.css.replace(/\n|\r/g, ' '), output.replace(/\n|\r/g, ' ')); | ||
// t.is(result.warnings().length, 0); | ||
}).catch(err => { | ||
if (!(output instanceof Error)) { | ||
t.fail(err); | ||
return; | ||
.foo { | ||
display: block; | ||
} | ||
t.is(err.message, output.message); | ||
`; | ||
await tester.test(input, output, t, { | ||
pluginsAfter: [postcssImport] | ||
}); | ||
}; | ||
test('simple test', async t => { | ||
const input = '@import-glob "fixtures/css/foo/**/*.css";'; | ||
const output = [ | ||
'.bar {', | ||
' display: inline-block;', | ||
'}', | ||
'.foo {', | ||
' display: block;', | ||
'}' | ||
].join(' '); | ||
await testPostcss(input, output, t, { | ||
pluginsAfter: [postcssImport()] | ||
}); | ||
}); | ||
test('sort option', async t => { | ||
const input = '@import-glob "fixtures/css/foo/**/*.css";'; | ||
const output = [ | ||
'.foo {', | ||
' display: block;', | ||
'}', | ||
'.bar {', | ||
' display: inline-block;', | ||
'}' | ||
].join(' '); | ||
await testPostcss(input, output, t, { | ||
plugin: postcssImportExtGlob({ | ||
const input = /* scss */` | ||
@import-glob "fixtures/css/foo/**/*.css"; | ||
`; | ||
const output = /* scss */` | ||
.foo { | ||
display: block; | ||
} | ||
.bar { | ||
display: inline-block; | ||
} | ||
`; | ||
await tester.test(input, output, t, { | ||
pluginOptions: { | ||
sort: 'desc' | ||
}), | ||
pluginsAfter: [postcssImport()] | ||
}, | ||
pluginsAfter: [postcssImport] | ||
}); | ||
@@ -77,16 +64,19 @@ }); | ||
test('multiple globs', async t => { | ||
const input = '@import-glob "fixtures/css/foo/**/*.css"; @import-glob "fixtures/css/*.css";'; | ||
const output = [ | ||
'.bar {', | ||
' display: inline-block;', | ||
'}', | ||
'.foo {', | ||
' display: block;', | ||
'}', | ||
'div {', | ||
' margin: auto;', | ||
'}' | ||
].join(' '); | ||
await testPostcss(input, output, t, { | ||
pluginsAfter: [postcssImport()] | ||
const input = /* scss */` | ||
@import-glob "fixtures/css/foo/**/*.css"; | ||
@import-glob "fixtures/css/*.css"; | ||
`; | ||
const output = /* scss */` | ||
.bar { | ||
display: inline-block; | ||
} | ||
.foo { | ||
display: block; | ||
} | ||
div { | ||
margin: auto; | ||
} | ||
`; | ||
await tester.test(input, output, t, { | ||
pluginsAfter: [postcssImport] | ||
}); | ||
@@ -96,16 +86,18 @@ }); | ||
test('multiple globs inline', async t => { | ||
const input = '@import-glob "fixtures/css/foo/**/*.css", "fixtures/css/*.css";'; | ||
const output = [ | ||
'.bar {', | ||
' display: inline-block;', | ||
'}', | ||
'.foo {', | ||
' display: block;', | ||
'}', | ||
'div {', | ||
' margin: auto;', | ||
'}' | ||
].join(' '); | ||
await testPostcss(input, output, t, { | ||
pluginsAfter: [postcssImport()] | ||
const input = /* scss */` | ||
@import-glob "fixtures/css/foo/**/*.css", "fixtures/css/*.css"; | ||
`; | ||
const output = /* scss */` | ||
.bar { | ||
display: inline-block; | ||
} | ||
.foo { | ||
display: block; | ||
} | ||
div { | ||
margin: auto; | ||
} | ||
`; | ||
await tester.test(input, output, t, { | ||
pluginsAfter: [postcssImport] | ||
}); | ||
@@ -115,7 +107,24 @@ }); | ||
test('error empty param test', async t => { | ||
const input = '@import-glob'; | ||
const output = new Error('No string found with rule @import-glob '); | ||
await testPostcss(input, output, t, { | ||
pluginsAfter: [postcssImport()] | ||
const input = /* scss */` | ||
@import-glob; | ||
`; | ||
await tester.test(input, err => { | ||
t.true(/No string found with rule @import-glob/.test(err)); | ||
}, t, { | ||
pluginsAfter: [postcssImport] | ||
}); | ||
}); | ||
test('no entries warning', async t => { | ||
const warningsTester = new PostcssTester({ | ||
postcss, | ||
plugin: postcssImportExtGlob, | ||
tolerateWarnings: true | ||
}); | ||
const input = /* scss */` | ||
@import-glob "fixtures/css/_unknow/**/*.css"; | ||
`; | ||
await warningsTester.test(input, '', t, { | ||
pluginsAfter: [postcssImport] | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9248
172
97
1