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 f810a4c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
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' })
})
})
44 changes: 22 additions & 22 deletions lib/quibble.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,37 @@ function getStubsInfo (moduleUrl) {
}

function transformModuleSource ([moduleKey, stubs]) {
return `
${Object.keys(stubs.namedExportStubs || {})
.map(
(name) =>
`export let ${name} = global.__quibble.quibbledModules.get(${JSON.stringify(
moduleKey
)}).namedExportStubs["${name}"]`
)
.join(';\n')};
${
stubs.defaultExportStub
? `export default global.__quibble.quibbledModules.get(${JSON.stringify(
moduleKey
)}).defaultExportStub;`
: ''
const stringified = JSON.stringify(moduleKey)
const namedExportStubs = Object.keys(stubs.namedExportStubs || {})
.map(name => `export const ${name} = global.__quibble.quibbledModules.get(${stringified}).namedExportStubs["${name}"]`)
.join('\n')
const defaultExportStub = stubs.defaultExportStub ?
`export default global.__quibble.quibbledModules.get(${stringified}).defaultExportStub` : ''

return [namedExportStubs, defaultExportStub].join('\n')
}
`

function transformJsonSource([moduleKey, stubs]) {
return JSON.stringify(global.__quibble.quibbledModules.get(moduleKey).defaultExportStub)
}

/**
* @param {string} url
* @param {{
* format: string,
* }} context
* @param {{ format: string }} context
* @param {Function} defaultLoad
* @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array), format: string}>}
*/
export async function load (url, context, defaultLoad) {
const stubsInfo = getStubsInfo(new URL(url))
const isJson = url.includes('.json')

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

return { source: transformModuleSource(stubsInfo), format: 'module' }
}

return defaultLoad(url, context, defaultLoad)
}

0 comments on commit f810a4c

Please sign in to comment.