Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
fix: multiple exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Feb 18, 2019
1 parent 09dc902 commit d699178
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/toESModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function(source) {
function replaceModuleExports(source) {
return source
.replace('module.exports = doc', 'export default doc')
.replace(/module\.exports\["(.*)"] = oneQuery\(doc, "(.*)"\)/, (match, g1, g2) => `export const ${g1} = oneQuery(doc, "${g2}")`);
.replace(/module\.exports\["(.*)"] = oneQuery\(doc, "(.*)"\)/g, (match, g1, g2) => `export const ${g1} = oneQuery(doc, "${g2}")`);
}

function replaceRequires(source) {
Expand Down
22 changes: 22 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,26 @@ describe('plugin', () => {
expect(exports.doc.kind).toBe('Document');
expect(exports.doc.definitions[1].name.value).toBe('HeroFragment');
});

it('should support multi-imports', async function () {
const bundle = await rollup.rollup({
input: 'samples/multi-imports/index.js',
plugins: [graphql()]
});
const { output: [{ code }] } = await bundle.generate({
format: 'cjs'
});
const exports = {};
const fn = new Function('exports', code);

fn(exports);

expect(exports.GetHero).toBeDefined();
expect(exports.GetHero.kind).toBe('Document');
expect(exports.GetHero.definitions[0].name.value).toBe('GetHero');

expect(exports.GetHeros).toBeDefined();
expect(exports.GetHeros.kind).toBe('Document');
expect(exports.GetHeros.definitions[0].name.value).toBe('GetHeros');
});
});
14 changes: 14 additions & 0 deletions tests/samples/multi-imports/hero.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query GetHero ($id: ID!) {
hero(id: $id) {
id
name
}
}

query GetHeros {
hero {
id
name
}
}

1 change: 1 addition & 0 deletions tests/samples/multi-imports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { GetHero, GetHeros } from './hero.graphql';

0 comments on commit d699178

Please sign in to comment.