Skip to content

Commit 1a734fc

Browse files
authored
fix(graphql): handle parentheses in fragment import file paths. (#1746)
fix(graphql): handle parentheses in fragment import file paths
1 parent 68e50ca commit 1a734fc

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

packages/graphql/src/toESModules.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ function replaceRequires(source) {
2929
let index = 0;
3030

3131
// replace a require statement with a variable
32-
const replaceSource = source.replace(/require\(([^)]+)\)/gi, (match, path) => {
33-
const replacePath = path.replace(/["']+/g, '');
34-
35-
if (!imports[replacePath]) {
32+
const replaceSource = source.replace(/require\(["']([^"']+)["']\)/gi, (match, path) => {
33+
if (!imports[path]) {
3634
index += 1;
37-
imports[replacePath] = `frgmt${index}`;
35+
imports[path] = `frgmt${index}`;
3836
}
3937

40-
return imports[replacePath];
38+
return imports[path];
4139
});
4240

4341
// prepare import statements
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment ParenthesesFragment on Parentheses {
2+
id
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment BracketsFragment on Brackets {
2+
id
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line import/prefer-default-export
2+
export { default as doc } from './query.graphql';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import "./(parentheses)/fragment.graphql"
2+
#import "./[brackets]/fragment.graphql"
3+
4+
query Query {
5+
...ParenthesesFragment
6+
...BracketsFragment
7+
}

packages/graphql/test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ test('should support graphqls schema files', async (t) => {
6262
t.truthy('doc' in module.exports);
6363
t.is(module.exports.doc.kind, 'Document');
6464
});
65+
66+
test('should support fragment imports with brackets and parentheses in file paths', async (t) => {
67+
const bundle = await rollup({
68+
input: 'fixtures/fragments-with-special-characters/index.js',
69+
plugins: [graphql()]
70+
});
71+
72+
const { module } = await testBundle(t, bundle);
73+
74+
t.truthy('doc' in module.exports);
75+
t.is(module.exports.doc.kind, 'Document');
76+
});

0 commit comments

Comments
 (0)