diff --git a/src/__tests__/__snapshots__/macro.test.js.snap b/src/__tests__/__snapshots__/macro.test.js.snap index 778b778..be2c51a 100644 --- a/src/__tests__/__snapshots__/macro.test.js.snap +++ b/src/__tests__/__snapshots__/macro.test.js.snap @@ -362,6 +362,17 @@ const query = { `; +exports[`macros [loader] error with absolute path and both jsconfig and tsconfig files: [loader] error with absolute path and both jsconfig and tsconfig files 1`] = ` + +import { loader } from 'graphql.macro'; +const query = loader('__tests__/fixtures/simpleFragment.graphql'); + + ↓ ↓ ↓ ↓ ↓ ↓ + +Error: You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file. + +`; + exports[`macros [loader] should work with relative path issue#52: [loader] should work with relative path issue#52 1`] = ` import { loader } from 'graphql.macro'; @@ -543,6 +554,58 @@ const query = { `; +exports[`macros [loader] with absolute path and tsconfig include property: [loader] with absolute path and tsconfig include property 1`] = ` + +import { loader } from 'graphql.macro'; +const query = loader('__tests__/fixtures/simpleFragment.graphql'); + + ↓ ↓ ↓ ↓ ↓ ↓ + +const query = { + "kind": "Document", + "definitions": [{ + "kind": "FragmentDefinition", + "name": { + "kind": "Name", + "value": "UserEntry1" + }, + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "User" + } + }, + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [{ + "kind": "Field", + "name": { + "kind": "Name", + "value": "firstName" + }, + "arguments": [], + "directives": [] + }] + } + }], + "loc": { + "start": 0, + "end": 44, + "source": { + "body": "fragment UserEntry1 on User {\\n firstName\\n}\\n", + "name": "GraphQL request", + "locationOffset": { + "line": 1, + "column": 1 + } + } + } +}; + +`; + exports[`macros [loader] with absolute path: [loader] with absolute path 1`] = ` import { loader } from 'graphql.macro'; diff --git a/src/__tests__/fixtures/tsconfig.json b/src/__tests__/fixtures/tsconfig.json new file mode 100644 index 0000000..bcd8df0 --- /dev/null +++ b/src/__tests__/fixtures/tsconfig.json @@ -0,0 +1,3 @@ +{ + "include": ["src"] +} diff --git a/src/__tests__/macro.test.js b/src/__tests__/macro.test.js index cfaf3d1..ac633ab 100644 --- a/src/__tests__/macro.test.js +++ b/src/__tests__/macro.test.js @@ -114,6 +114,52 @@ pluginTester({ ); }, }, + '[loader] with absolute path and tsconfig include property': { + error: false, + code: ` + import { loader } from '../macro'; + const query = loader('__tests__/fixtures/simpleFragment.graphql'); + `, + setup: () => { + fs.symlinkSync( + 'src/__tests__/fixtures/tsconfig.json', + path.resolve(fs.realpathSync(process.cwd()), 'tsconfig.json'), + 'file', + ); + }, + teardown: () => { + fs.unlinkSync( + path.resolve(fs.realpathSync(process.cwd()), 'tsconfig.json'), + ); + }, + }, + '[loader] error with absolute path and both jsconfig and tsconfig files': { + error: true, + code: ` + import { loader } from '../macro'; + const query = loader('__tests__/fixtures/simpleFragment.graphql'); + `, + setup: () => { + fs.symlinkSync( + 'src/__tests__/fixtures/jsconfig.json', + path.resolve(fs.realpathSync(process.cwd()), 'jsconfig.json'), + 'file', + ); + fs.symlinkSync( + 'src/__tests__/fixtures/tsconfig.json', + path.resolve(fs.realpathSync(process.cwd()), 'tsconfig.json'), + 'file', + ); + }, + teardown: () => { + fs.unlinkSync( + path.resolve(fs.realpathSync(process.cwd()), 'jsconfig.json'), + ); + fs.unlinkSync( + path.resolve(fs.realpathSync(process.cwd()), 'tsconfig.json'), + ); + }, + }, '[loader] with nested circular fragments': { error: false, code: ` diff --git a/src/utils/resolveImportPath.js b/src/utils/resolveImportPath.js index efe3f94..7e3ad86 100644 --- a/src/utils/resolveImportPath.js +++ b/src/utils/resolveImportPath.js @@ -2,13 +2,20 @@ import path from 'path'; import fs from 'fs'; +const CONFIG_FILENAMES = ['jsconfig.json', 'tsconfig.json']; const CMD = fs.realpathSync(process.cwd()); -const jsconfigPath = path.resolve(CMD, 'jsconfig.json'); +const configPaths = CONFIG_FILENAMES.map(filename => + path.resolve(CMD, filename), +).filter(actPath => fs.existsSync(actPath)); let jsconfigInclude; -if (fs.existsSync(jsconfigPath)) { - const jsconfig = JSON.parse(fs.readFileSync(jsconfigPath, 'utf8')); +if (configPaths.length === 1) { + const jsconfig = JSON.parse(fs.readFileSync(configPaths[0], 'utf8')); jsconfigInclude = jsconfig.include ? jsconfig.include[0] : null; +} else if (configPaths.length > 1) { + throw new Error( + 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.', + ); } const resolveImportPath = ({