@dxup/nuxt 0.1.0 → 0.1.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.
package/dist/module.d.ts CHANGED
@@ -2,9 +2,25 @@ import * as _nuxt_schema0 from "@nuxt/schema";
2
2
 
3
3
  //#region src/module/index.d.ts
4
4
  interface ModuleOptions {
5
+ /**
6
+ * Whether to update references when renaming auto imported component files.
7
+ * @default true
8
+ */
5
9
  components?: boolean;
10
+ /**
11
+ * Whether to enable Go to Definition for nitro routes in data fetching methods.
12
+ * @default true
13
+ */
6
14
  nitroRoutes?: boolean;
15
+ /**
16
+ * Whether to enable Go to Definition for runtime config.
17
+ * @default true
18
+ */
7
19
  runtimeConfig?: boolean;
20
+ /**
21
+ * Whether to enable enhanced navigation for auto imported APIs.
22
+ * @default true
23
+ */
8
24
  unimport?: boolean;
9
25
  }
10
26
  declare const _default: _nuxt_schema0.NuxtModule<ModuleOptions, ModuleOptions, false>;
package/dist/module.js CHANGED
@@ -1,4 +1,4 @@
1
- import { addTemplate, defineNuxtModule } from "@nuxt/kit";
1
+ import { addTemplate, defineNuxtModule, useNitro } from "@nuxt/kit";
2
2
  import { Buffer } from "node:buffer";
3
3
  import EventEmitter from "node:events";
4
4
  import { mkdir, open, readFile, writeFile } from "node:fs/promises";
@@ -85,13 +85,14 @@ var module_default = defineNuxtModule({
85
85
  addTemplate({
86
86
  filename: "dxup/data.json",
87
87
  write: true,
88
- getContents() {
88
+ getContents({ nuxt: nuxt$1 }) {
89
+ const nitro = useNitro();
90
+ const nitroRoutes = options.nitroRoutes && Object.fromEntries(nitro.scannedHandlers.filter((item) => item.route).map((item) => [`${item.route}+${item.method ?? "get"}`, item.handler]));
89
91
  const data = {
90
- buildDir: nuxt.options.buildDir,
91
- serverDir: nuxt.options.serverDir,
92
- configFiles: [...nuxt.options._nuxtConfigFiles, ...nuxt.options._layers.map((layer) => layer._configFile).filter(Boolean)],
92
+ buildDir: nuxt$1.options.buildDir,
93
+ configFiles: [...nuxt$1.options._nuxtConfigFiles, ...nuxt$1.options._layers.map((layer) => layer._configFile).filter(Boolean)],
93
94
  components: options.components,
94
- nitroRoutes: options.nitroRoutes,
95
+ nitroRoutes,
95
96
  runtimeConfig: options.runtimeConfig
96
97
  };
97
98
  return JSON.stringify(data, null, 2);
@@ -63,10 +63,8 @@ function* binaryVisit(ts, sourceFile, node, position) {
63
63
  while (left <= right) {
64
64
  const mid = Math.floor((left + right) / 2);
65
65
  const node$1 = nodes[mid];
66
- const start = node$1.getStart(sourceFile);
67
- const end = node$1.getEnd();
68
- if (position < start) right = mid - 1;
69
- else if (position > end) left = mid + 1;
66
+ if (position > node$1.getEnd()) left = mid + 1;
67
+ else if (position < node$1.getStart(sourceFile)) right = mid - 1;
70
68
  else {
71
69
  yield node$1;
72
70
  yield* binaryVisit(ts, sourceFile, node$1, position);
@@ -74,6 +72,9 @@ function* binaryVisit(ts, sourceFile, node, position) {
74
72
  }
75
73
  }
76
74
  }
75
+ function isTextSpanEqual(node, textSpan, sourceFile) {
76
+ return textSpan.start + textSpan.length === node.getEnd() && textSpan.start === node.getStart(sourceFile);
77
+ }
77
78
 
78
79
  //#endregion
79
80
  //#region src/typescript/features/getDefinitionAndBoundSpan.ts
@@ -82,7 +83,6 @@ const fetchFunctions = new Set([
82
83
  "useFetch",
83
84
  "useLazyFetch"
84
85
  ]);
85
- const nonApiRE = /^(?!\/api\/)/;
86
86
  function getDefinitionAndBoundSpan(context, getDefinitionAndBoundSpan$1) {
87
87
  const { ts, info, data } = context;
88
88
  return (...args) => {
@@ -99,19 +99,30 @@ function getDefinitionAndBoundSpan(context, getDefinitionAndBoundSpan$1) {
99
99
  const end = firstArg.getEnd();
100
100
  if (args[1] < start || args[1] > end) continue;
101
101
  const resolvedSignature = checker.getResolvedSignature(node);
102
- if (!resolvedSignature) break;
102
+ if (!resolvedSignature) continue;
103
103
  const typeArguments = checker.getTypeArgumentsForResolvedSignature(resolvedSignature);
104
- const [routeType, methodType] = (node.expression.text === "$fetch" ? typeArguments?.[2] && checker.getTypeArguments(typeArguments?.[2]) : typeArguments?.slice(2)) ?? [];
105
- if (!routeType?.isStringLiteral() || !methodType?.isStringLiteral()) break;
106
- const route = routeType.value.replace(nonApiRE, "/routes");
107
- const method = methodType.value;
108
- const path = (0, pathe.join)(data.serverDir, `${route}.${method}.ts`);
104
+ let routeType;
105
+ let methodType;
106
+ if (node.expression.text === "$fetch") {
107
+ routeType = typeArguments?.[1];
108
+ const symbol = typeArguments?.[2].getProperty("method");
109
+ methodType = symbol ? checker.getTypeOfSymbol(symbol) : void 0;
110
+ } else {
111
+ routeType = typeArguments?.[2];
112
+ methodType = typeArguments?.[3];
113
+ }
114
+ if (!routeType?.isStringLiteral()) continue;
115
+ const paths = [];
116
+ for (const type of methodType?.isUnion() ? methodType.types : [methodType]) if (type?.isStringLiteral()) {
117
+ const path = data.nitroRoutes[`${routeType.value}+${type.value}`];
118
+ if (path !== void 0) paths.push(path);
119
+ }
109
120
  return {
110
121
  textSpan: {
111
122
  start,
112
123
  length: end - start
113
124
  },
114
- definitions: [{
125
+ definitions: paths.map((path) => ({
115
126
  fileName: path,
116
127
  textSpan: {
117
128
  start: 0,
@@ -121,10 +132,9 @@ function getDefinitionAndBoundSpan(context, getDefinitionAndBoundSpan$1) {
121
132
  name: path,
122
133
  containerKind: ts.ScriptElementKind.unknown,
123
134
  containerName: ""
124
- }]
135
+ }))
125
136
  };
126
137
  }
127
- return;
128
138
  }
129
139
  if (!result?.definitions?.length) return result;
130
140
  const program = info.languageService.getProgram();
@@ -154,10 +164,7 @@ function visitRuntimeConfig(context, sourceFile, definition) {
154
164
  if (ts.isInterfaceDeclaration(node) && ts.isIdentifier(node.name)) key = node.name.text;
155
165
  else if (ts.isPropertySignature(node) && ts.isIdentifier(node.name)) {
156
166
  key = node.name.text;
157
- const { textSpan } = definition;
158
- const start = node.name.getStart(sourceFile);
159
- const end = node.name.getEnd();
160
- if (start === textSpan.start && end - start === textSpan.length) {
167
+ if (isTextSpanEqual(node.name, definition.textSpan, sourceFile)) {
161
168
  path.push(key);
162
169
  definitions = [...forwardRuntimeConfig(context, definition, path)];
163
170
  break;
@@ -280,19 +287,10 @@ function getEditsForFileRename(context, getEditsForFileRename$1) {
280
287
  const plugin = (module$1) => {
281
288
  const { typescript: ts } = module$1;
282
289
  return { create(info) {
283
- const currentDirectory = info.languageServiceHost.getCurrentDirectory();
284
- const path = (0, pathe.join)(currentDirectory, "dxup/data.json");
285
290
  const context = {
286
291
  ts,
287
292
  info,
288
- data: {
289
- buildDir: currentDirectory,
290
- configFiles: [],
291
- components: true,
292
- nitroRoutes: true,
293
- runtimeConfig: true,
294
- ...JSON.parse(ts.sys.readFile(path) ?? "{}")
295
- },
293
+ data: createData(ts, info),
296
294
  server: createEventServer(info)
297
295
  };
298
296
  setTimeout(() => {
@@ -310,6 +308,27 @@ const plugin = (module$1) => {
310
308
  } };
311
309
  };
312
310
  var typescript_default = plugin;
311
+ function createData(ts, info) {
312
+ const initialValue = {
313
+ buildDir: "",
314
+ configFiles: [],
315
+ components: true,
316
+ nitroRoutes: {},
317
+ runtimeConfig: true
318
+ };
319
+ const path = (0, pathe.join)(info.languageServiceHost.getCurrentDirectory(), "dxup/data.json");
320
+ const data = {};
321
+ update();
322
+ ts.sys.watchFile?.(path, update);
323
+ return data;
324
+ function update() {
325
+ const text = ts.sys.readFile(path);
326
+ Object.assign(data, {
327
+ ...initialValue,
328
+ ...text ? JSON.parse(text) : {}
329
+ });
330
+ }
331
+ }
313
332
 
314
333
  //#endregion
315
334
  module.exports = typescript_default;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dxup/nuxt",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "TypeScript plugin for Nuxt",
6
6
  "author": "KazariEX",
7
7
  "license": "MIT",