Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tsconfig file to get absolute imports path #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/__tests__/__snapshots__/macro.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["src"]
}
46 changes: 46 additions & 0 deletions src/__tests__/macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down
13 changes: 10 additions & 3 deletions src/utils/resolveImportPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand Down