Skip to content

Commit

Permalink
feat: add support for json modules
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 10, 2021
1 parent 1a19c6e commit ba24262
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions example-esm/lib/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"quibble": "rocks"
}
1 change: 1 addition & 0 deletions example-esm/test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const quibble = require('quibble')
beforeEach(function () {
quibble.esm('../lib/animals/bear.mjs', undefined, function () { return 'a fake bear' })
quibble.esm('../lib/animals/lion.mjs', undefined, function () { return 'a fake lion' })
quibble.esm('../lib/foo.json', undefined, { 'hello': 'quibble' })
})

afterEach(function () {
Expand Down
6 changes: 6 additions & 0 deletions example-esm/test/lib/zoo-spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { expect } = chai

describe('zoo', function () {
var subject
var json

beforeEach(async function () {
subject = await import('../../lib/zoo.mjs')
json = await import('../../lib/foo.json')
})

it('contains a fake bear', function () {
Expand All @@ -17,4 +19,8 @@ describe('zoo', function () {
it('contains a fake lion', function () {
expect(subject.default().animals).to.contain('a fake lion')
})

it('manipulates json', function () {
expect(json.default).to.deep.equal({ hello: 'quibble' })
})
})
3 changes: 2 additions & 1 deletion lib/quibble.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ ${
*/
export async function load (url, context, defaultLoad) {
const stubsInfo = getStubsInfo(new URL(url))
const isJson = url.endsWith('.json')

return stubsInfo
? { source: transformModuleSource(stubsInfo), format: 'module' }
? { source: transformModuleSource(stubsInfo), format: isJson ? 'json' : 'module' }
: defaultLoad(url, context, defaultLoad)
}

0 comments on commit ba24262

Please sign in to comment.