Skip to content

Commit e11ac12

Browse files
author
Jeff Escalante
committed
fix config fn context error, error handling with invalid options
1 parent c4bc347 commit e11ac12

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

lib/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ module.exports = function (source) {
66

77
// configure options
88
const qs = loaderUtils.parseQuery(this.query)
9-
const config = parseOptions(this.options.posthtml, qs)
109
const cb = this.async()
10+
let config
11+
try {
12+
config = parseOptions.call(this, this.options.posthtml, qs)
13+
} catch (err) {
14+
return cb(err)
15+
}
1116

1217
// configure custom parser argument if necessary
1318
const processArgs = [source.toString()]
@@ -16,7 +21,7 @@ module.exports = function (source) {
1621
// run posthtml
1722
const ph = posthtml(config.plugins)
1823
ph.process.apply(ph, processArgs)
19-
.then((result) => cb(null, result.html), (err) => console.error(err))
24+
.then((result) => cb(null, result.html), cb)
2025
}
2126

2227
function parseOptions (config = [], qs) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"coveralls": "^2.11.9",
2323
"nyc": "^7.0.0",
2424
"posthtml-custom-elements": "^1.0.3",
25+
"posthtml-exp": "^0.9.0",
2526
"source-loader": "^0.2.0",
2627
"standard": "^7.1.2",
2728
"sugarml": "0.0.1",

test/fixtures/locals/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./index.html')

test/fixtures/locals/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>{{ foo }}</p>

test/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path')
44
const fs = require('fs')
55
const node = require('when/node')
66
const customElements = require('posthtml-custom-elements')
7+
const exp = require('posthtml-exp')
78
const sugarml = require('sugarml')
89
const fixtures = path.join(__dirname, 'fixtures')
910

@@ -51,11 +52,22 @@ test('custom parser', (t) => {
5152

5253
test('invalid config', (t) => {
5354
return webpackCompile('custom_parser', 5)
54-
.catch((err) => {
55+
.then(() => t.fail('invalid config, no error'))
56+
.catch(({outputPath, err}) => {
5557
t.truthy(err.toString().match(/Error: Configuration must return an array or object/))
58+
fs.unlinkSync(outputPath)
5659
})
5760
})
5861

62+
test('function called with correct context', (t) => {
63+
return webpackCompile('locals', (ctx) => {
64+
return [exp({ locals: { foo: ctx.resourcePath } })]
65+
}).then(({outputPath, src}) => {
66+
t.truthy(src.match(/test\/fixtures\/locals\/index\.html/))
67+
fs.unlinkSync(outputPath)
68+
})
69+
})
70+
5971
// Utility: compile a fixture with webpack, return results
6072
function webpackCompile (name, config, qs = '') {
6173
const testPath = path.join(fixtures, name)
@@ -73,5 +85,5 @@ function webpackCompile (name, config, qs = '') {
7385
if (stats.compilation.errors.length) throw stats.compilation.errors
7486
const src = fs.readFileSync(outputPath, 'utf8')
7587
return {outputPath, src}
76-
})
88+
}).catch((err) => { throw {outputPath, err} }) // eslint-disable-line
7789
}

0 commit comments

Comments
 (0)