vscode-apollo 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ orbs:
6
6
  executors:
7
7
  node:
8
8
  docker:
9
- - image: cimg/node:22.5.1
9
+ - image: cimg/node:22.6.0
10
10
  working_directory: ~/vscode-graphql
11
11
 
12
12
  commands:
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#171](https://github.com/apollographql/vscode-graphql/pull/171) [`37a2b292`](https://github.com/apollographql/vscode-graphql/commit/37a2b292c0de22ee76645fc2dcde03b8f4843051) Thanks [@phryneas](https://github.com/phryneas)! - Also try parsing `.ts` files as CommonJS, not only ESM.
8
+
3
9
  ## 2.0.0
4
10
 
5
11
  ### Major Changes
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "vscode-apollo",
3
3
  "displayName": "Apollo GraphQL",
4
4
  "description": "Rich editor support for GraphQL client and server development that seamlessly integrates with the Apollo platform",
5
- "version": "2.0.0",
5
+ "version": "2.0.1",
6
6
  "referenceID": "87197759-7617-40d0-b32e-46d378e907c7",
7
7
  "author": "Apollo GraphQL <opensource@apollographql.com>",
8
8
  "license": "MIT",
@@ -158,6 +158,30 @@ Object {
158
158
  const config = await loadConfig({ configPath: dirPath });
159
159
  expect(config?.client?.service).toEqual("hello");
160
160
  });
161
+
162
+ it("loads config from a ts file with CommonJs", async () => {
163
+ writeFilesToDir(dir, {
164
+ "apollo.config.ts": `module.exports = {"client": {"service": "hello"} }`,
165
+ });
166
+ const config = await loadConfig({ configPath: dirPath });
167
+ expect(config?.client?.service).toEqual("hello");
168
+ });
169
+
170
+ it("loads config from a cjs file", async () => {
171
+ writeFilesToDir(dir, {
172
+ "apollo.config.cjs": `module.exports = {"client": {"service": "hello"} }`,
173
+ });
174
+ const config = await loadConfig({ configPath: dirPath });
175
+ expect(config?.client?.service).toEqual("hello");
176
+ });
177
+
178
+ it("loads config from a mjs file", async () => {
179
+ writeFilesToDir(dir, {
180
+ "apollo.config.mjs": `export default {"client": {"service": "hello"} }`,
181
+ });
182
+ const config = await loadConfig({ configPath: dirPath });
183
+ expect(config?.client?.service).toEqual("hello");
184
+ });
161
185
  });
162
186
 
163
187
  describe("errors", () => {
@@ -1,4 +1,4 @@
1
- import { cosmiconfig } from "cosmiconfig";
1
+ import { cosmiconfig, defaultLoaders } from "cosmiconfig";
2
2
  import { resolve } from "path";
3
3
  import { readFileSync, existsSync, lstatSync } from "fs";
4
4
  import {
@@ -9,6 +9,7 @@ import {
9
9
  import { getServiceFromKey } from "./utils";
10
10
  import { URI } from "vscode-uri";
11
11
  import { Debug } from "../utilities";
12
+ import { loadTs } from "./loadTsConfig";
12
13
 
13
14
  // config settings
14
15
  const MODULE_NAME = "apollo";
@@ -37,11 +38,16 @@ export type ConfigResult<T> = {
37
38
  } | null;
38
39
 
39
40
  // XXX load .env files automatically
41
+
40
42
  export async function loadConfig({
41
43
  configPath,
42
44
  }: LoadConfigSettings): Promise<ApolloConfig | null> {
43
45
  const explorer = cosmiconfig(MODULE_NAME, {
44
46
  searchPlaces: defaultFileNames,
47
+ loaders: {
48
+ ...defaultLoaders,
49
+ [".ts"]: loadTs,
50
+ },
45
51
  });
46
52
 
47
53
  // search can fail if a file can't be parsed (ex: a nonsense js file) so we wrap in a try/catch
@@ -0,0 +1,70 @@
1
+ import { Loader, defaultLoaders } from "cosmiconfig";
2
+ import { dirname } from "node:path";
3
+ import { rm, writeFile } from "node:fs/promises";
4
+ import { existsSync } from "node:fs";
5
+
6
+ // implementation based on https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/src/loaders.ts
7
+ // Copyright (c) 2015 David Clark licensed MIT. Full license can be found here:
8
+ // https://github.com/cosmiconfig/cosmiconfig/blob/a5a842547c13392ebb89a485b9e56d9f37e3cbd3/LICENSE
9
+
10
+ let typescript: typeof import("typescript");
11
+ export const loadTs: Loader = async function loadTs(filepath, content) {
12
+ try {
13
+ return await defaultLoaders[".ts"](filepath, content);
14
+ } catch (error) {
15
+ if (
16
+ !(error instanceof Error) ||
17
+ !error.message.includes("module is not defined")
18
+ )
19
+ throw error;
20
+ }
21
+
22
+ if (typescript === undefined) {
23
+ typescript = await import("typescript");
24
+ }
25
+ const compiledFilepath = `${filepath.slice(0, -2)}cjs`;
26
+ let transpiledContent;
27
+ try {
28
+ try {
29
+ const config = resolveTsConfig(dirname(filepath)) ?? {};
30
+ config.compilerOptions = {
31
+ ...config.compilerOptions,
32
+ module: typescript.ModuleKind.CommonJS,
33
+ moduleResolution: typescript.ModuleResolutionKind.Bundler,
34
+ target: typescript.ScriptTarget.ES2022,
35
+ noEmit: false,
36
+ };
37
+ transpiledContent = typescript.transpileModule(
38
+ content,
39
+ config,
40
+ ).outputText;
41
+ await writeFile(compiledFilepath, transpiledContent);
42
+ } catch (error: any) {
43
+ error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
44
+ throw error;
45
+ }
46
+ // eslint-disable-next-line @typescript-eslint/return-await
47
+ return await defaultLoaders[".js"](compiledFilepath, transpiledContent);
48
+ } finally {
49
+ if (existsSync(compiledFilepath)) {
50
+ await rm(compiledFilepath);
51
+ }
52
+ }
53
+ };
54
+
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ function resolveTsConfig(directory: string): any {
57
+ const filePath = typescript.findConfigFile(directory, (fileName) => {
58
+ return typescript.sys.fileExists(fileName);
59
+ });
60
+ if (filePath !== undefined) {
61
+ const { config, error } = typescript.readConfigFile(filePath, (path) =>
62
+ typescript.sys.readFile(path),
63
+ );
64
+ if (error) {
65
+ throw new Error(`Error in ${filePath}: ${error.messageText.toString()}`);
66
+ }
67
+ return config;
68
+ }
69
+ return;
70
+ }