Skip to content

Commit

Permalink
fix: Corerct TS artifact generation when Mesh config is in TS/JS form…
Browse files Browse the repository at this point in the history
…at (#5966)
  • Loading branch information
cweckesser committed May 15, 2024
1 parent 3e246cf commit 8f2349c
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/poor-suits-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/cli': patch
---

Fix TS artifact generation when running Mesh build command so no reference to Mesh config script
files (TS/JS) are included in the built sources.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ jobs:
restore-keys: |
${{runner.os}}-${{matrix.node-version}}-jest-unit-
- name: Generate config schema
run: yarn generate-config-schema

- name: Run Tests
uses: nick-fields/retry@v3
with:
Expand Down
18 changes: 18 additions & 0 deletions packages/legacy/cli/src/commands/ts-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,12 @@ const importFn: ImportFn = <T>(moduleId: string) => {
}
if (pathModule.isAbsolute(importPath)) {
moduleMapProp = pathModule.relative(baseDir, importedModuleName).split('\\').join('/');
moduleMapProp = replaceTypeScriptExtension(moduleMapProp);
importPath = `./${pathModule
.relative(artifactsDir, importedModuleName)
.split('\\')
.join('/')}`;
importPath = replaceTypeScriptExtension(importPath);
}
const importIdentifier = `importedModule$${importedModuleIndex}`;
importCodes.add(`import * as ${importIdentifier} from ${JSON.stringify(importPath)};`);
Expand Down Expand Up @@ -589,3 +591,19 @@ export function compileTS(tsFilePath: string, module: ts.ModuleKind, outputFileP
const program = ts.createProgram([tsFilePath], options, host);
program.emit();
}

/**
* If the specified path corresponds to a TypeScript file, replace
* its extension to `.js`.
*
* @param {string} path The path to a potential TypeScript file
* @returns {string}
*/
function replaceTypeScriptExtension(path: string): string {
let modifiedPath = path;
if (modifiedPath.toLowerCase().endsWith('.ts')) {
const extensionStart = modifiedPath.lastIndexOf('.');
modifiedPath = modifiedPath.substring(0, extensionStart).concat('.js');
}
return modifiedPath;
}
51 changes: 51 additions & 0 deletions packages/legacy/cli/test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
import { fs, path as pathModule } from '@graphql-mesh/cross-helpers';
import { DEFAULT_CLI_PARAMS, graphqlMesh } from '../src/index';

describe('runtime', () => {
it('dummy', async () => {});

describe('build', () => {
const generatedMeshConfiguration = '.mesh';

it('Should replace the `.ts` extension of a config file in the built Mesh index file for the `.js` extension', async () => {
const tsConfigFolder = 'ts-config';
await graphqlMesh(DEFAULT_CLI_PARAMS, [
'build',
`--dir=${pathModule.resolve(__dirname, tsConfigFolder)}`,
]);

const meshConfigPath = pathModule.resolve(
__dirname,
tsConfigFolder,
generatedMeshConfiguration,
'index.ts',
);
const builtMesh = (await fs.promises.readFile(meshConfigPath)).toString();

// Check that the import of the main Mesh module has replaced the extension
expect(builtMesh).toMatch(/import \* as importedModule(.*) from ".*\/\.meshrc.js";/);

// Check that the reference to the relative module in the "importFn" function has replaced the extension
expect(builtMesh).toMatch(/case "\.meshrc.js":/);
});

it('Should keep the `.js` extension of a config file in the built Mesh index file', async () => {
const jsConfigFolder = 'js-config';
await graphqlMesh(DEFAULT_CLI_PARAMS, [
'build',
`--dir=${pathModule.resolve(__dirname, jsConfigFolder)}`,
]);

const meshConfigPath = pathModule.resolve(
__dirname,
jsConfigFolder,
generatedMeshConfiguration,
'index.ts',
);
const builtMesh = (await fs.promises.readFile(meshConfigPath)).toString();

// Check that the import of the main Mesh module has kept the file's extension
expect(builtMesh).toMatch(/import \* as importedModule(.*) from ".*\/\.meshrc.js";/);

// Check that the reference to the relative module in the "importFn" function has kept the file's extension
expect(builtMesh).toMatch(/case "\.meshrc.js":/);
});
});
});
3 changes: 3 additions & 0 deletions packages/legacy/cli/test/fixtures/dummy-schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
greet: String
}
12 changes: 12 additions & 0 deletions packages/legacy/cli/test/js-config/.meshrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
sources: [
{
name: 'Dummy',
handler: {
graphql: {
source: '../fixtures/dummy-schema.graphql',
},
},
},
],
};
12 changes: 12 additions & 0 deletions packages/legacy/cli/test/ts-config/.meshrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
sources: [
{
name: 'Dummy',
handler: {
graphql: {
source: '../fixtures/dummy-schema.graphql',
},
},
},
],
};

0 comments on commit 8f2349c

Please sign in to comment.