@graphitation/apollo-react-relay-duct-tape
Advanced tools
Comparing version 1.0.1-alpha.3 to 1.0.1-alpha.4
# Change Log - @graphitation/apollo-react-relay-duct-tape | ||
This log was last generated on Thu, 07 Apr 2022 17:44:09 GMT and should not be manually modified. | ||
This log was last generated on Thu, 26 May 2022 00:34:23 GMT and should not be manually modified. | ||
<!-- Start content --> | ||
## 1.0.1-alpha.4 | ||
Thu, 26 May 2022 00:34:23 GMT | ||
### Changes | ||
- [duct-tape] Export import transform for ts-jest (eloy.de.enige@gmail.com) | ||
- [duct-tape] Make artefact import transform work for es5 modules (eloy.de.enige@gmail.com) | ||
## 1.0.1-alpha.3 | ||
Thu, 07 Apr 2022 17:44:09 GMT | ||
Thu, 07 Apr 2022 17:44:14 GMT | ||
@@ -11,0 +20,0 @@ ### Changes |
@@ -9,2 +9,3 @@ /** | ||
export declare function createImportDocumentsTransform(): ts.TransformerFactory<ts.SourceFile>; | ||
export { createImportDocumentsTransform as factory }; | ||
//# sourceMappingURL=createImportDocumentsTransform.d.ts.map |
@@ -25,3 +25,4 @@ var __create = Object.create; | ||
__export(exports, { | ||
createImportDocumentsTransform: () => createImportDocumentsTransform | ||
createImportDocumentsTransform: () => createImportDocumentsTransform, | ||
factory: () => createImportDocumentsTransform | ||
}); | ||
@@ -40,13 +41,9 @@ var import_invariant = __toModule(require("invariant")); | ||
if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.escapedText === "graphql") { | ||
const documentNodes = createGraphQLDocumentNodes(node); | ||
const documentNodes = createGraphQLDocumentNodes(node, context.getCompilerOptions().module); | ||
if (documentNodes) { | ||
const [importDeclaration, replacementNode] = documentNodes; | ||
let emitImport = true; | ||
const sourceFile = node.getSourceFile(); | ||
if (sourceFile) { | ||
const artefactFile = path.join(path.dirname(sourceFile.fileName), importDeclaration.moduleSpecifier.text) + ".ts"; | ||
emitImport = fs.existsSync(artefactFile); | ||
} | ||
const [modulePath, importStatement, replacementNode] = documentNodes; | ||
const artefactFile = path.join(path.dirname(node.getSourceFile().fileName), modulePath.text) + ".ts"; | ||
const emitImport = fs.existsSync(artefactFile); | ||
if (emitImport) { | ||
imports.push(importDeclaration); | ||
imports.push(importStatement); | ||
return replacementNode; | ||
@@ -67,3 +64,3 @@ } | ||
} | ||
function createGraphQLDocumentNodes(graphqlTagTemplateNode) { | ||
function createGraphQLDocumentNodes(graphqlTagTemplateNode, moduleKind = ts.ModuleKind.ES2015) { | ||
var _a; | ||
@@ -81,6 +78,6 @@ const graphqlDoc = ts.isNoSubstitutionTemplateLiteral(graphqlTagTemplateNode.template) ? graphqlTagTemplateNode.template.rawText : graphqlTagTemplateNode.template.head.rawText; | ||
const namespaceName = `${PREFIX}${QUERIES_NAMESPACE}_${operationName}`; | ||
const modulePath = createModulePathNode(operationName); | ||
return [ | ||
ts.factory.createImportDeclaration(void 0, void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([ | ||
ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("documents"), ts.factory.createIdentifier(namespaceName)) | ||
])), ts.factory.createStringLiteral(`./__generated__/${operationName}.graphql`)), | ||
modulePath, | ||
createImportStatement(moduleKind, namespaceName, modulePath), | ||
ts.factory.createIdentifier(namespaceName) | ||
@@ -92,6 +89,6 @@ ]; | ||
const namespaceName = `${PREFIX}${QUERIES_NAMESPACE}_${fragmentName}`; | ||
const modulePath = createModulePathNode(queryName); | ||
return [ | ||
ts.factory.createImportDeclaration(void 0, void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([ | ||
ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("documents"), ts.factory.createIdentifier(namespaceName)) | ||
])), ts.factory.createStringLiteral(`./__generated__/${queryName}.graphql`)), | ||
modulePath, | ||
createImportStatement(moduleKind, namespaceName, modulePath), | ||
ts.factory.createIdentifier(namespaceName) | ||
@@ -102,2 +99,16 @@ ]; | ||
} | ||
function createModulePathNode(baseName) { | ||
return ts.factory.createStringLiteral(`./__generated__/${baseName}.graphql`); | ||
} | ||
function createImportStatement(moduleKind, namespaceName, modulePath) { | ||
if (moduleKind === ts.ModuleKind.CommonJS) { | ||
return ts.factory.createVariableStatement(void 0, ts.factory.createVariableDeclarationList([ | ||
ts.factory.createVariableDeclaration(namespaceName, void 0, void 0, ts.factory.createPropertyAccessExpression(ts.factory.createCallExpression(ts.factory.createIdentifier("require"), [], [modulePath]), ts.factory.createIdentifier("documents"))) | ||
])); | ||
} else { | ||
return ts.factory.createImportDeclaration(void 0, void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([ | ||
ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("documents"), ts.factory.createIdentifier(namespaceName)) | ||
])), modulePath); | ||
} | ||
} | ||
function getQueryName(definitionNode) { | ||
@@ -104,0 +115,0 @@ var _a, _b, _c, _d; |
@@ -21,2 +21,5 @@ var __create = Object.create; | ||
var import_createImportDocumentsTransform = __toModule(require("./createImportDocumentsTransform")); | ||
var _fs = __toModule(require("fs")); | ||
const fs = _fs; | ||
jest.mock("fs"); | ||
expect.addSnapshotSerializer({ | ||
@@ -28,11 +31,53 @@ test: (val) => typeof val === "string", | ||
}); | ||
function transform(sourceText) { | ||
const sourceFile = ts.createSourceFile("SomeComponent.tsx", sourceText, ts.ScriptTarget.Latest); | ||
const result = ts.transform(sourceFile, [(0, import_createImportDocumentsTransform.createImportDocumentsTransform)()]); | ||
const printer = ts.createPrinter(); | ||
return printer.printFile(result.transformed[0]); | ||
} | ||
describe(import_createImportDocumentsTransform.createImportDocumentsTransform, () => { | ||
it("works with documents without interpolation", () => { | ||
const source = ` | ||
describe.each([ | ||
[ | ||
"es6", | ||
{ | ||
target: ts.ScriptTarget.ES2015, | ||
module: ts.ModuleKind.ES2015 | ||
} | ||
], | ||
[ | ||
"es5", | ||
{ | ||
target: ts.ScriptTarget.ES5, | ||
module: ts.ModuleKind.CommonJS | ||
} | ||
] | ||
])("when emitting %s modules", (_targetName, compilerOptions) => { | ||
function transform(sourceText) { | ||
return ts.transpileModule(sourceText, { | ||
fileName: __filename, | ||
transformers: {before: [(0, import_createImportDocumentsTransform.createImportDocumentsTransform)()]}, | ||
compilerOptions | ||
}).outputText; | ||
} | ||
it("works with documents without interpolation", () => { | ||
fs.existsSync.mockImplementation(() => true); | ||
const source = ` | ||
const doc = graphql\` | ||
fragment SomeComponentFragment on Query { | ||
helloWorld | ||
} | ||
\` | ||
`; | ||
expect(transform(source)).toMatchSnapshot(); | ||
}); | ||
it("works with documents with interpolation", () => { | ||
fs.existsSync.mockImplementation(() => true); | ||
const source = ` | ||
const doc = graphql\` | ||
query SomeComponentQuery($id: ID!) { | ||
helloWorld | ||
...SomeOtherComponentFragment | ||
} | ||
\${SomeOtherComponentFragment} | ||
\` | ||
`; | ||
expect(transform(source)).toMatchSnapshot(); | ||
}); | ||
it("does not emit an import for a fragment on non-Node/Query types (for which no artefacts are emitted)", () => { | ||
fs.existsSync.mockImplementation(() => false); | ||
const source = ` | ||
const doc = graphql\` | ||
@@ -44,22 +89,5 @@ fragment SomeComponentFragment on Query { | ||
`; | ||
expect(transform(source)).toMatchInlineSnapshot(` | ||
import { documents as __graphitation_generatedQueries_SomeComponentFragment } from "./__generated__/SomeComponentWatchNodeQuery.graphql"; | ||
const doc = __graphitation_generatedQueries_SomeComponentFragment; | ||
`); | ||
expect(transform(source)).toMatchSnapshot(); | ||
}); | ||
}); | ||
it("works with documents with interpolation", () => { | ||
const source = ` | ||
const doc = graphql\` | ||
query SomeComponentQuery($id: ID!) { | ||
helloWorld | ||
...SomeOtherComponentFragment | ||
} | ||
\${SomeOtherComponentFragment} | ||
\` | ||
`; | ||
expect(transform(source)).toMatchInlineSnapshot(` | ||
import { documents as __graphitation_generatedQueries_SomeComponentQuery } from "./__generated__/SomeComponentQuery.graphql"; | ||
const doc = __graphitation_generatedQueries_SomeComponentQuery; | ||
`); | ||
}); | ||
}); |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.1-alpha.3", | ||
"version": "1.0.1-alpha.4", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
962183
11127
6