From 1a734fc048468b5978ca804c0b1a98093d4cdda5 Mon Sep 17 00:00:00 2001 From: Grex Date: Sun, 22 Sep 2024 15:03:18 -0800 Subject: [PATCH] fix(graphql): handle parentheses in fragment import file paths. (#1746) fix(graphql): handle parentheses in fragment import file paths --- packages/graphql/src/toESModules.js | 10 ++++------ .../(parentheses)/fragment.graphql | 3 +++ .../[brackets]/fragment.graphql | 3 +++ .../fragments-with-special-characters/index.js | 2 ++ .../fragments-with-special-characters/query.graphql | 7 +++++++ packages/graphql/test/test.js | 12 ++++++++++++ 6 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 packages/graphql/test/fixtures/fragments-with-special-characters/(parentheses)/fragment.graphql create mode 100644 packages/graphql/test/fixtures/fragments-with-special-characters/[brackets]/fragment.graphql create mode 100644 packages/graphql/test/fixtures/fragments-with-special-characters/index.js create mode 100644 packages/graphql/test/fixtures/fragments-with-special-characters/query.graphql diff --git a/packages/graphql/src/toESModules.js b/packages/graphql/src/toESModules.js index 08647fc36..cb2ccc6ce 100644 --- a/packages/graphql/src/toESModules.js +++ b/packages/graphql/src/toESModules.js @@ -29,15 +29,13 @@ function replaceRequires(source) { let index = 0; // replace a require statement with a variable - const replaceSource = source.replace(/require\(([^)]+)\)/gi, (match, path) => { - const replacePath = path.replace(/["']+/g, ''); - - if (!imports[replacePath]) { + const replaceSource = source.replace(/require\(["']([^"']+)["']\)/gi, (match, path) => { + if (!imports[path]) { index += 1; - imports[replacePath] = `frgmt${index}`; + imports[path] = `frgmt${index}`; } - return imports[replacePath]; + return imports[path]; }); // prepare import statements diff --git a/packages/graphql/test/fixtures/fragments-with-special-characters/(parentheses)/fragment.graphql b/packages/graphql/test/fixtures/fragments-with-special-characters/(parentheses)/fragment.graphql new file mode 100644 index 000000000..706b5d691 --- /dev/null +++ b/packages/graphql/test/fixtures/fragments-with-special-characters/(parentheses)/fragment.graphql @@ -0,0 +1,3 @@ +fragment ParenthesesFragment on Parentheses { + id +} diff --git a/packages/graphql/test/fixtures/fragments-with-special-characters/[brackets]/fragment.graphql b/packages/graphql/test/fixtures/fragments-with-special-characters/[brackets]/fragment.graphql new file mode 100644 index 000000000..3821b0882 --- /dev/null +++ b/packages/graphql/test/fixtures/fragments-with-special-characters/[brackets]/fragment.graphql @@ -0,0 +1,3 @@ +fragment BracketsFragment on Brackets { + id +} diff --git a/packages/graphql/test/fixtures/fragments-with-special-characters/index.js b/packages/graphql/test/fixtures/fragments-with-special-characters/index.js new file mode 100644 index 000000000..286dd2116 --- /dev/null +++ b/packages/graphql/test/fixtures/fragments-with-special-characters/index.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line import/prefer-default-export +export { default as doc } from './query.graphql'; diff --git a/packages/graphql/test/fixtures/fragments-with-special-characters/query.graphql b/packages/graphql/test/fixtures/fragments-with-special-characters/query.graphql new file mode 100644 index 000000000..390e3cc12 --- /dev/null +++ b/packages/graphql/test/fixtures/fragments-with-special-characters/query.graphql @@ -0,0 +1,7 @@ +#import "./(parentheses)/fragment.graphql" +#import "./[brackets]/fragment.graphql" + +query Query { + ...ParenthesesFragment + ...BracketsFragment +} diff --git a/packages/graphql/test/test.js b/packages/graphql/test/test.js index b4af33306..d085da2b4 100755 --- a/packages/graphql/test/test.js +++ b/packages/graphql/test/test.js @@ -62,3 +62,15 @@ test('should support graphqls schema files', async (t) => { t.truthy('doc' in module.exports); t.is(module.exports.doc.kind, 'Document'); }); + +test('should support fragment imports with brackets and parentheses in file paths', async (t) => { + const bundle = await rollup({ + input: 'fixtures/fragments-with-special-characters/index.js', + plugins: [graphql()] + }); + + const { module } = await testBundle(t, bundle); + + t.truthy('doc' in module.exports); + t.is(module.exports.doc.kind, 'Document'); +});