Skip to content

Commit 13aa421

Browse files
authored
remove webpack option (#18)
* remove webpack option * 0.3.0-0 * add wallaby * better docs and tests, add append/prepend plugins opts * 0.3.0-1 * update deps * remove wallaby
1 parent 21cdc6c commit 13aa421

File tree

6 files changed

+1476
-1226
lines changed

6 files changed

+1476
-1226
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
.nyc_output
66
.travis.yml
77
yarn.lock
8+
wallaby.js

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,27 @@ By default, the css standard plugin pack includes:
4444
Any of these plugins can be customized by passing the [options](#options) described below. You can also add additional postCSS plugins (like the popular [`lost`](https://github.com/peterramsing/lost) grid, for example) on top of this package:
4545

4646
```js
47-
// app.js
4847
const cssStandards = require('spike-css-standards')
4948
const lost = require('lost')
5049

51-
// ...
52-
postcss: (ctx) => {
53-
const css = cssStandards({ webpack: ctx })
54-
css.plugins.push(lost())
55-
return css
56-
},
57-
// ...
50+
const css = cssStandards()
51+
css.plugins.push(lost())
5852
```
5953

6054
### Options
6155

6256
| Name | Description | Default |
6357
| ---- | ----------- | ------- |
64-
| **root** | Root used to resolve `path`(s) from | If `webpack` option is provided, `options.context` |
65-
| **path** | A path to a folder or an array of paths, telling postcss-import where to look for sss or css files to `@import`. | If `webpack` option is provided, `loaderContext.resourcePath` |
66-
| **webpack** | Shortcut for webpack users to set the `root` and `path` options more easily. Pass webpack loader context. | |
58+
| **root** | Root used to resolve `path`(s) from | |
59+
| **path** | A path to a folder or an array of paths, telling postcss-import where to look for sss or css files to `@import`. | |
6760
| **browsers** | Browser support provided to [autoprefixer](http://cssnext.io/usage/#browsers) | `> 1%, last 2 versions, Firefox ESR` |
6861
| **features** | Enable or disable [cssnext features](http://cssnext.io/usage/#features) | |
6962
| **warnForDuplicates** | Enable or disable [cssnext duplicate warnings](http://cssnext.io/usage/#warnforduplicates) | `true` |
7063
| **rucksack** | Options passed directly to [rucksack](http://simplaio.github.io/rucksack/docs/#options) | |
7164
| **parser** | custom css parser if desired. pass `false` to use the default css parser | `sugarss` |
7265
| **minify** | Minifies the css output by removing excess spaces and line breaks | `false` |
66+
| **appendPlugins** | Adds a single plugin or array of plugins after all the defaults | |
67+
| **prependPlugins** | Adds a single plugin or array of plugins before all the defaults | |
7368

7469
### License & Contributing
7570

lib/index.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,61 @@ let postcssImport = require('postcss-import')
33
let cssnext = require('postcss-cssnext')
44
let rucksack = require('rucksack-css')
55

6+
/**
7+
* Primary export, formats options and returns an object with intelligent
8+
* defaults.
9+
* @param {Object} [options={}] - options object
10+
* @param {Function} [options.parser=sugarss] - if false, is set to undefined
11+
* @param {Array|String} options.path - passed to import plugin
12+
* @param {String} options.root - passed to import plugin
13+
* @param {Array} options.browsers - passed to cssnext plugin
14+
* @param {Object} options.features - passed to cssnext plugin
15+
* @param {Boolean} options.warnForDuplicates - passed to cssnext plugin
16+
* @param {Object} options.rucksack - passed to rucksack plugin
17+
* @param {Boolean} options.minify - whether or not to add the minify plugin
18+
* @return {Object} valid postcss options object
19+
*/
620
module.exports = (options = {}) => {
721
// sugarss by default unless false or custom parser
822
let parser = options.parser || sugarss
923
if (options.parser === false) parser = undefined
1024
options.path = options.path ? Array.prototype.concat(options.path) : []
1125

12-
// define root/path defaults if the webpack object is provided
13-
if (options.webpack) {
14-
options.root = options.webpack.options.context
15-
options.path.push(options.webpack.resourcePath)
16-
}
17-
1826
// standard options merge
19-
const importOpt = selectiveMerge(options, ['root', 'path'])
20-
const cssnextOpt = selectiveMerge(options, ['browsers', 'features', 'warnForDuplicates'])
27+
const importOpt = selectKeys(options, ['root', 'path'])
28+
const cssnextOpt = selectKeys(options, ['browsers', 'features', 'warnForDuplicates'])
2129

22-
// define normal plugin list
30+
// define default plugin list
2331
const plugins = [
2432
postcssImport(importOpt),
2533
cssnext(cssnextOpt),
2634
rucksack(options.rucksack)
2735
]
2836

37+
// append and prepend plugins if needed
38+
if (options.appendPlugins) {
39+
plugins.push(...Array.prototype.concat(options.appendPlugins))
40+
}
41+
42+
if (options.prependPlugins) {
43+
plugins.unshift(...Array.prototype.concat(options.prependPlugins))
44+
}
45+
2946
// add cssnano if minify config present
3047
if (options.minify) plugins.push(require('cssnano')())
3148

3249
return {parser, plugins}
3350
}
3451

35-
function selectiveMerge (opts, optNames) {
52+
/**
53+
* Given an options object and an array of key names, return an object filtered
54+
* to contain only the keys in the optNames array, if they exist on the options
55+
* object.
56+
* @param {Object} opts - full options object
57+
* @param {Array} optNames - keys to filter
58+
* @return {Object} object filtered for the specific keys
59+
*/
60+
function selectKeys (opts, optNames) {
3661
return optNames.reduce((m, opt) => {
3762
if (typeof opts[opt] !== 'undefined') { m[opt] = opts[opt] }; return m
3863
}, {})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "spike-css-standards",
33
"description": "standard plugin pack for postcss",
4-
"version": "0.2.0",
4+
"version": "0.3.0-1",
55
"author": "Jeff Escalante",
66
"ava": {
77
"verbose": "true"

test/index.js

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
11
const rewire = require('rewire')
2-
const cssStandardsRewired = rewire('..')
3-
const cssStandards = require('..')
2+
const cssStandardsRewired = rewire('../lib')
3+
const cssStandards = require('../lib')
44
const test = require('ava')
55

6-
test('basic', (t) => {
7-
cssStandardsRewired.__set__('postcssImport', (opts) => {
8-
t.truthy(opts.root === 'test')
9-
t.truthy(opts.path[0] === 'test/test1')
10-
t.truthy(opts.path[1] === 'test/test2')
11-
t.truthy(opts.path[2] === 'test') // webpack.resourcePath
12-
})
6+
test('passes parser opt correctly', (t) => {
7+
const out = cssStandards({ parser: 'test' })
8+
const out2 = cssStandards({ parser: false })
9+
t.is(out.parser, 'test')
10+
t.is(out2.parser, undefined)
11+
})
1312

14-
cssStandardsRewired.__set__('cssnext', (opts) => {
15-
t.truthy(opts.features === 'test')
16-
t.truthy(opts.browsers === 'test')
17-
t.truthy(opts.warnForDuplicates === 'test')
13+
test('passes import opts correctly', (t) => {
14+
const undo = cssStandardsRewired.__set__('postcssImport', (opts) => {
15+
t.is(opts.root, 'test')
16+
t.is(opts.path[0], 'test')
1817
})
18+
cssStandardsRewired({ root: 'test', path: 'test' })
19+
undo()
20+
})
1921

20-
cssStandardsRewired.__set__('rucksack', (opts) => {
21-
t.truthy(opts === 'test')
22+
test('passes cssnext opts correctly', (t) => {
23+
const undo = cssStandardsRewired.__set__('cssnext', (opts) => {
24+
t.is(opts.browsers, 'test')
25+
t.is(opts.features, 'test')
26+
t.is(opts.warnForDuplicates, 'test')
2227
})
23-
24-
const out1 = cssStandardsRewired({
25-
parser: false,
26-
webpack: {
27-
resourcePath: 'test',
28-
options: { context: 'test' }
29-
},
30-
path: ['test/test1', 'test/test2'],
31-
features: 'test',
28+
cssStandardsRewired({
3229
browsers: 'test',
33-
warnForDuplicates: 'test',
34-
rucksack: 'test'
30+
features: 'test',
31+
warnForDuplicates: 'test'
32+
})
33+
undo()
34+
})
35+
36+
test('passes rucksack opts correctly', (t) => {
37+
const undo = cssStandardsRewired.__set__('rucksack', (opts) => {
38+
t.is(opts, 'test')
3539
})
40+
cssStandardsRewired({ rucksack: 'test' })
41+
undo()
42+
})
3643

37-
t.truthy(out1.plugins.length === 3)
38-
t.falsy(out1.parser)
44+
test('default plugins working', (t) => {
45+
const out = cssStandards()
46+
t.is(out.plugins.length, 3)
47+
})
3948

40-
const out2 = cssStandards({ minify: true })
49+
test('minify option working', (t) => {
50+
const out = cssStandards({ minify: true })
51+
t.is(out.plugins.length, 4)
52+
t.is(out.plugins[out.plugins.length - 1].postcssPlugin, 'cssnano')
53+
})
54+
55+
test('appendPlugins option', (t) => {
56+
const out = cssStandards({ appendPlugins: ['test'] })
57+
const out2 = cssStandards({ appendPlugins: 'test' })
58+
t.truthy(out.plugins[out.plugins.length - 1] === 'test')
59+
t.truthy(out2.plugins[out.plugins.length - 1] === 'test')
60+
})
4161

42-
t.truthy(out2.parser)
43-
t.truthy(out2.plugins.length === 4)
62+
test('prependPlugins option', (t) => {
63+
const out = cssStandards({ prependPlugins: ['test'] })
64+
const out2 = cssStandards({ prependPlugins: 'test' })
65+
t.truthy(out.plugins[0] === 'test')
66+
t.truthy(out2.plugins[0] === 'test')
4467
})

0 commit comments

Comments
 (0)