Skip to content

Commit

Permalink
feat(src/index): support synchronous config loading (rc.sync)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatcuk authored and michael-ciniawsky committed Jun 10, 2019
1 parent 497d4f9 commit f230d40
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 56 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ module.exports = (ctx) => ({
}
```

### `Async`

```js
const { readFileSync } = require('fs')

Expand All @@ -339,6 +341,21 @@ postcssrc(ctx).then(({ plugins, options }) => {
})
```

### `Sync`

```js
const { readFileSync } = require('fs')

const postcss = require('postcss')
const postcssrc = require('postcss-load-config')

const css = readFileSync('index.sss', 'utf8')

const ctx = { parser: true, map: 'inline' }

const { plugins, options } = postcssrc.sync(ctx)
```

<div align="center">
<img width="80" height="80" halign="10" src="https://worldvectorlogo.com/logos/gulp.svg">
</div>
Expand Down
109 changes: 74 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,42 @@ const loadOptions = require('./options.js')
const loadPlugins = require('./plugins.js')

/**
* Load Config
* Process the result from cosmiconfig
*
* @method rc
* @param {Object} ctx Config Context
* @param {Object} result Cosmiconfig result
*
* @return {Object} PostCSS Config
*/
const processResult = (ctx, result) => {
let file = result.filepath || ''
let config = result.config || {}

if (typeof config === 'function') {
config = config(ctx)
} else {
config = Object.assign({}, config, ctx)
}

if (!config.plugins) {
config.plugins = []
}

return {
plugins: loadPlugins(config, file),
options: loadOptions(config, file),
file: file
}
}

/**
* Builds the Config Context
*
* @param {Object} ctx Config Context
* @param {String} path Config Path
* @param {Object} options Config Options
*
* @return {Promise} config PostCSS Config
* @return {Object} Config Context
*/
const rc = (ctx, path, options) => {
const createContext = (ctx) => {
/**
* @type {Object}
*
Expand All @@ -29,24 +54,35 @@ const rc = (ctx, path, options) => {
cwd: process.cwd(),
env: process.env.NODE_ENV
}, ctx)

if (!ctx.env) {
process.env.NODE_ENV = 'development'
}

return ctx
}

/**
* Load Config
*
* @method rc
*
* @param {Object} ctx Config Context
* @param {String} path Config Path
* @param {Object} options Config Options
*
* @return {Promise} config PostCSS Config
*/
const rc = (ctx, path, options) => {
/**
* @type {String} `process.cwd()`
*
* @type {Object} The full Config Context
*/
path = path ? resolve(path) : process.cwd()
ctx = createContext(ctx)

/**
* @type {Object}
*
* @prop {Boolean} rcExtensions=true
* @type {String} `process.cwd()`
*/
options = Object.assign({
rcExtensions: true
}, options)

if (!ctx.env) {
process.env.NODE_ENV = 'development'
}
path = path ? resolve(path) : process.cwd()

return config('postcss', options)
.search(path)
Expand All @@ -55,25 +91,28 @@ const rc = (ctx, path, options) => {
throw new Error(`No PostCSS Config found in: ${path}`)
}

let file = result.filepath || ''
let config = result.config || {}
return processResult(ctx, result)
})
}

if (typeof config === 'function') {
config = config(ctx)
} else {
config = Object.assign({}, config, ctx)
}
rc.sync = (ctx, path, options) => {
/**
* @type {Object} The full Config Context
*/
ctx = createContext(ctx)

if (!config.plugins) {
config.plugins = []
}
/**
* @type {String} `process.cwd()`
*/
path = path ? resolve(path) : process.cwd()

return {
plugins: loadPlugins(config, file),
options: loadOptions(config, file),
file: file
}
})
const result = config('postcss', options).searchSync(path)

if (!result) {
throw new Error(`No PostCSS Config found in: ${path}`)
}

return processResult(ctx, result)
}

/**
Expand Down
22 changes: 9 additions & 13 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ const req = require('import-cwd')
* @return {Function} PostCSS Plugin
*/
const load = (plugin, options, file) => {
if (
options === null ||
options === undefined ||
Object.keys(options).length === 0
) {
try {
try {
if (
options === null ||
options === undefined ||
Object.keys(options).length === 0
) {
return req(plugin)
} catch (err) {
throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
}
} else {
try {
} else {
return req(plugin)(options)
} catch (err) {
throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
}
} catch (err) {
throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
}
}

Expand Down
94 changes: 94 additions & 0 deletions test/Errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ test('Loading Config - {Error}', () => {
})
})

test('Loading Config - Sync - {Error}', () => {
try {
postcssrc.sync({}, 'test/err')
} catch (err) {
expect(err.message).toMatch(
/^No PostCSS Config found in: (.*)$/
)
}
})

describe('Loading Plugins - {Error}', () => {
test('Plugin - {Type} - Invalid', () => {
return postcssrc({}, 'test/err/plugins').catch((err) => {
Expand Down Expand Up @@ -52,6 +62,58 @@ describe('Loading Plugins - {Error}', () => {
})
})

describe('Loading Plugins - Sync - {Error}', () => {
test('Plugin - {Type} - Invalid', () => {
try {
postcssrc.sync({}, 'test/err/plugins')
} catch (err) {
expect(err.message).toMatch(
/^Invalid PostCSS Plugin found at: (.*)\n\n\(@.*\)$/
)
}
})

test('Plugin - {Object}', () => {
try {
postcssrc.sync({}, 'test/err/plugins/object')
} catch (err) {
expect(err.message).toMatch(
/^Loading PostCSS Plugin failed: (.*)\n\n\(@.*\)$/
)
}
})

test('Plugin - {Object} - Options', () => {
try {
postcssrc.sync({}, 'test/err/plugins/object/options')
} catch (err) {
expect(err.message).toMatch(
/^Loading PostCSS Plugin failed: (.*)\n\n\(@.*\)$/
)
}
})

test('Plugin - {Array}', () => {
try {
postcssrc.sync({}, 'test/err/plugins/array')
} catch (err) {
expect(err.message).toMatch(
/^Cannot find (.*)$/
)
}
})

test('Plugin - {Array} - Options', () => {
try {
postcssrc.sync({}, 'test/err/plugins/array/options')
} catch (err) {
expect(err.message).toMatch(
/^Cannot find (.*)$/
)
}
})
})

describe('Loading Options - {Error}', () => {
test('Parser - {String}', () => {
return postcssrc({}, 'test/err/options/parser').catch((err) => {
Expand All @@ -77,3 +139,35 @@ describe('Loading Options - {Error}', () => {
})
})
})

describe('Loading Options - Sync - {Error}', () => {
test('Parser - {String}', () => {
try {
postcssrc.sync({}, 'test/err/options/parser')
} catch (err) {
expect(err.message).toMatch(
/^Loading PostCSS Parser failed: (.*)\n\n\(@.*\)$/
)
}
})

test('Syntax - {String}', () => {
try {
postcssrc.sync({}, 'test/err/options/syntax')
} catch (err) {
expect(err.message).toMatch(
/^Loading PostCSS Syntax failed: (.*)\n\n\(@.*\)$/
)
}
})

test('Stringifier - {String}', () => {
try {
postcssrc.sync({}, 'test/err/options/stringifier')
} catch (err) {
expect(err.message).toMatch(
/^Loading PostCSS Stringifier failed: (.*)\n\n\(@.*\)$/
)
}
})
})
28 changes: 24 additions & 4 deletions test/js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const postcssrc = require('../src/index.js')

const { fixture, expected } = require('./utils.js')

test('postcss.config.js - {Object} - Load Config', () => {
describe('postcss.config.js - {Object} - Load Config', () => {
const ctx = {
parser: true,
syntax: true
}

return postcssrc(ctx, 'test/js/object').then((config) => {
const expected = (config) => {
expect(config.options.parser).toEqual(require('sugarss'))
expect(config.options.syntax).toEqual(require('sugarss'))
expect(config.options.map).toEqual(false)
Expand All @@ -26,6 +26,16 @@ test('postcss.config.js - {Object} - Load Config', () => {

expect(config.file)
.toEqual(path.resolve('test/js/object', 'postcss.config.js'))
}

test('Async', () => {
return postcssrc(ctx, 'test/js/object').then(expected)
})

test('Sync', () => {
const config = postcssrc.sync(ctx, 'test/js/object')

expected(config)
})
})

Expand Down Expand Up @@ -60,13 +70,13 @@ test('postcss.config.js - {Object} - Process SSS', () => {
})
})

test('postcss.config.js - {Array} - Load Config', () => {
describe('postcss.config.js - {Array} - Load Config', () => {
const ctx = {
parser: true,
syntax: true
}

return postcssrc(ctx, 'test/js/array').then((config) => {
const expected = (config) => {
expect(config.options.parser).toEqual(require('sugarss'))
expect(config.options.syntax).toEqual(require('sugarss'))
expect(config.options.map).toEqual(false)
Expand All @@ -79,6 +89,16 @@ test('postcss.config.js - {Array} - Load Config', () => {

expect(config.file)
.toEqual(path.resolve('test/js/array', 'postcss.config.js'))
}

test('Async', () => {
return postcssrc(ctx, 'test/js/array').then(expected)
})

test('Sync', () => {
const config = postcssrc.sync(ctx, 'test/js/array')

expected(config)
})
})

Expand Down
Loading

0 comments on commit f230d40

Please sign in to comment.