@dxup/nuxt 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -8,8 +8,9 @@ This is a collection of TypeScript and Vue plugins that improves Nuxt DX.
8
8
 
9
9
  ## Features
10
10
 
11
- - Go to definition for nitro routes on data fetching methods
11
+ - Go to definition for nitro routes in data fetching methods within Vue files
12
12
  - Go to definition for runtime config
13
+ - [@dxup/unimport](/packages/unimport)
13
14
 
14
15
  ## Installation
15
16
 
@@ -9,14 +9,11 @@ function* forEachNode(node) {
9
9
  for (const child of children) yield* forEachNode(child);
10
10
  }
11
11
  function walkNodes(node, callback) {
12
- function next() {
13
- const children = [];
12
+ callback(node, () => {
14
13
  node.forEachChild((child) => {
15
- children.push(child);
14
+ walkNodes(child, callback);
16
15
  });
17
- for (const child of children) walkNodes(child, callback);
18
- }
19
- callback(node, next);
16
+ });
20
17
  }
21
18
 
22
19
  //#endregion
package/dist/module.d.ts CHANGED
@@ -1,13 +1,9 @@
1
1
  import * as _nuxt_schema0 from "@nuxt/schema";
2
2
 
3
- //#region src/typescript/index.d.ts
4
- interface Options {
3
+ //#region src/module/index.d.ts
4
+ interface ModuleOptions {
5
5
  nitroRoutes?: boolean;
6
6
  runtimeConfig?: boolean;
7
- }
8
- //#endregion
9
- //#region src/module/index.d.ts
10
- interface ModuleOptions extends Options {
11
7
  unimport?: boolean;
12
8
  }
13
9
  declare const _default: _nuxt_schema0.NuxtModule<ModuleOptions, ModuleOptions, false>;
package/dist/module.js CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule } from "@nuxt/kit";
1
+ import { addTemplate, defineNuxtModule } from "@nuxt/kit";
2
2
 
3
3
  //#region package.json
4
4
  var name = "@dxup/nuxt";
@@ -16,15 +16,8 @@ var module_default = defineNuxtModule({
16
16
  unimport: true
17
17
  },
18
18
  async setup(options, nuxt) {
19
- const pluginsTs = [];
19
+ const pluginsTs = [{ name: "@dxup/nuxt" }];
20
20
  const pluginsVue = [];
21
- pluginsTs.push({
22
- name: "@dxup/nuxt",
23
- options: {
24
- nitroRoutes: options.nitroRoutes,
25
- runtimeConfig: options.runtimeConfig
26
- }
27
- });
28
21
  if (options.nitroRoutes) pluginsVue.push("@dxup/nuxt/vue/nitro-routes");
29
22
  if (options.unimport) pluginsTs.push({ name: "@dxup/unimport" });
30
23
  append(pluginsTs, nuxt.options, "typescript", "tsConfig", "compilerOptions");
@@ -32,6 +25,18 @@ var module_default = defineNuxtModule({
32
25
  append(pluginsTs, nuxt.options, "typescript", "sharedTsConfig", "compilerOptions");
33
26
  append(pluginsTs, nuxt.options, "typescript", "nodeTsConfig", "compilerOptions");
34
27
  append(pluginsVue, nuxt.options, "typescript", "tsConfig", "vueCompilerOptions");
28
+ addTemplate({
29
+ filename: "dxup.json",
30
+ write: true,
31
+ getContents() {
32
+ const data = {
33
+ nitroRoutes: options.nitroRoutes,
34
+ runtimeConfig: options.runtimeConfig,
35
+ configFiles: [...nuxt.options._nuxtConfigFiles, ...nuxt.options._layers.map((layer) => layer._configFile).filter(Boolean)]
36
+ };
37
+ return JSON.stringify(data, null, 2);
38
+ }
39
+ });
35
40
  }
36
41
  });
37
42
  function append(plugins, target, ...keys) {
@@ -21,7 +21,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  }) : target, mod));
22
22
 
23
23
  //#endregion
24
- const require_ast = require('./ast-BfhxgPs7.cjs');
24
+ const require_ast = require('./ast-DJW3KREB.cjs');
25
25
  let node_path = require("node:path");
26
26
  node_path = __toESM(node_path);
27
27
 
@@ -29,12 +29,19 @@ node_path = __toESM(node_path);
29
29
  const plugin = (module$1) => {
30
30
  const { typescript: ts } = module$1;
31
31
  return { create(info) {
32
- const options = {
32
+ const path = (0, node_path.join)(info.languageServiceHost.getCurrentDirectory(), "dxup.json");
33
+ const data = {
33
34
  nitroRoutes: true,
34
35
  runtimeConfig: true,
35
- ...info.config.options
36
+ configFiles: [],
37
+ ...JSON.parse(ts.sys.readFile(path) ?? "{}")
36
38
  };
37
- for (const [key, method] of [["getDefinitionAndBoundSpan", getDefinitionAndBoundSpan.bind(null, ts, info, options)]]) {
39
+ const context = {
40
+ ts,
41
+ info,
42
+ data
43
+ };
44
+ for (const [key, method] of [["getDefinitionAndBoundSpan", getDefinitionAndBoundSpan.bind(null, context)]]) {
38
45
  const original = info.languageService[key];
39
46
  info.languageService[key] = method(original);
40
47
  }
@@ -42,7 +49,8 @@ const plugin = (module$1) => {
42
49
  } };
43
50
  };
44
51
  var typescript_default = plugin;
45
- function getDefinitionAndBoundSpan(ts, info, options, getDefinitionAndBoundSpan$1) {
52
+ function getDefinitionAndBoundSpan(context, getDefinitionAndBoundSpan$1) {
53
+ const { info, data } = context;
46
54
  return (fileName, position) => {
47
55
  const result = getDefinitionAndBoundSpan$1(fileName, position);
48
56
  if (!result?.definitions?.length) return result;
@@ -53,8 +61,8 @@ function getDefinitionAndBoundSpan(ts, info, options, getDefinitionAndBoundSpan$
53
61
  const sourceFile = program.getSourceFile(definition.fileName);
54
62
  if (!sourceFile) continue;
55
63
  let result$1 = [];
56
- if (options.nitroRoutes && definition.fileName.endsWith("nitro-routes.d.ts")) result$1 = visitNitroRoutes(ts, sourceFile, definition, getDefinitionAndBoundSpan$1);
57
- else if (options.runtimeConfig && definition.fileName.endsWith("runtime-config.d.ts")) result$1 = visitRuntimeConfig(ts, info, sourceFile, definition);
64
+ if (data.nitroRoutes && definition.fileName.endsWith("nitro-routes.d.ts")) result$1 = visitNitroRoutes(context, sourceFile, definition, getDefinitionAndBoundSpan$1);
65
+ else if (data.runtimeConfig && definition.fileName.endsWith("runtime-config.d.ts")) result$1 = visitRuntimeConfig(context, sourceFile, definition);
58
66
  if (result$1?.length) {
59
67
  for (const definition$1 of result$1) definitions.add(definition$1);
60
68
  skippedDefinitions.push(definition);
@@ -67,7 +75,8 @@ function getDefinitionAndBoundSpan(ts, info, options, getDefinitionAndBoundSpan$
67
75
  };
68
76
  };
69
77
  }
70
- function visitNitroRoutes(ts, sourceFile, definition, getDefinitionAndBoundSpan$1) {
78
+ function visitNitroRoutes(context, sourceFile, definition, getDefinitionAndBoundSpan$1) {
79
+ const { ts } = context;
71
80
  const definitions = [];
72
81
  for (const node of require_ast.forEachNode(sourceFile)) {
73
82
  if (!ts.isPropertySignature(node) || !node.type || !ts.isTypeLiteralNode(node.type)) continue;
@@ -87,7 +96,8 @@ function visitNitroRoutes(ts, sourceFile, definition, getDefinitionAndBoundSpan$
87
96
  }
88
97
  return definitions;
89
98
  }
90
- function visitRuntimeConfig(ts, info, sourceFile, definition) {
99
+ function visitRuntimeConfig(context, sourceFile, definition) {
100
+ const { ts } = context;
91
101
  let definitions = [];
92
102
  const path = [];
93
103
  require_ast.walkNodes(sourceFile, (node, next) => {
@@ -100,7 +110,7 @@ function visitRuntimeConfig(ts, info, sourceFile, definition) {
100
110
  const end = node.name.getEnd();
101
111
  if (start === textSpan.start && end - start === textSpan.length) {
102
112
  path.push(key);
103
- definitions = [...proxyRuntimeConfig(ts, info, definition, path)];
113
+ definitions = [...proxyRuntimeConfig(context, definition, path)];
104
114
  return;
105
115
  }
106
116
  }
@@ -110,7 +120,8 @@ function visitRuntimeConfig(ts, info, sourceFile, definition) {
110
120
  });
111
121
  return definitions;
112
122
  }
113
- function* proxyRuntimeConfig(ts, info, definition, path) {
123
+ function* proxyRuntimeConfig(context, definition, path) {
124
+ const { ts, info, data } = context;
114
125
  switch (path[0]) {
115
126
  case "SharedRuntimeConfig":
116
127
  path.shift();
@@ -120,41 +131,47 @@ function* proxyRuntimeConfig(ts, info, definition, path) {
120
131
  break;
121
132
  default: return;
122
133
  }
123
- const configName = (0, node_path.join)((0, node_path.dirname)(info.project.getProjectName()), "tsconfig.node.json");
124
- const nodeProject = info.project.projectService.findProject(configName);
134
+ const configFile = data.configFiles[0];
135
+ if (configFile === void 0) return;
136
+ const { configFileName } = info.project.projectService.openClientFile(configFile);
137
+ if (configFileName === void 0) return;
138
+ const nodeProject = info.project.projectService.findProject(configFileName);
125
139
  if (!nodeProject) return;
126
140
  const nodeProgram = nodeProject.getLanguageService().getProgram();
127
141
  if (!nodeProgram) return;
128
- const nuxtConfigName = nodeProgram.getRootFileNames().find((name) => name.endsWith("nuxt.config.ts"));
129
- if (!nuxtConfigName) return;
130
- const sourceFile = nodeProgram.getSourceFile(nuxtConfigName);
131
- if (!sourceFile) return;
132
142
  const checker = nodeProgram.getTypeChecker();
133
- for (const node of sourceFile.statements) if (ts.isExportAssignment(node) && ts.isCallExpression(node.expression) && node.expression.arguments.length) {
134
- const arg = node.expression.arguments[0];
135
- let currentSymbol;
136
- let currentType = checker.getTypeAtLocation(arg);
137
- for (const key of ["runtimeConfig", ...path]) {
138
- const symbol = currentType.getProperties().find((s) => s.name === key);
139
- if (!symbol) return;
140
- currentSymbol = symbol;
141
- currentType = checker.getTypeOfSymbol(symbol);
142
- }
143
- for (const decl of currentSymbol.declarations ?? []) {
144
- const sourceFile$1 = decl.getSourceFile();
145
- const contextSpan = {
146
- start: decl.getStart(sourceFile$1),
147
- length: decl.getWidth(sourceFile$1)
148
- };
149
- yield {
150
- ...definition,
151
- contextSpan,
152
- fileName: sourceFile$1.fileName,
153
- textSpan: ts.isPropertyAssignment(decl) ? {
143
+ for (const configFile$1 of data.configFiles) {
144
+ const sourceFile = nodeProgram.getSourceFile(configFile$1);
145
+ if (!sourceFile) continue;
146
+ outer: for (const node of sourceFile.statements) {
147
+ if (!ts.isExportAssignment(node) || !ts.isCallExpression(node.expression) || !node.expression.arguments.length) continue;
148
+ const arg = node.expression.arguments[0];
149
+ let currentSymbol;
150
+ let currentType = checker.getTypeAtLocation(arg);
151
+ for (const key of ["runtimeConfig", ...path]) {
152
+ const symbol = currentType.getProperties().find((s) => s.name === key);
153
+ if (!symbol) break outer;
154
+ currentSymbol = symbol;
155
+ currentType = checker.getTypeOfSymbol(symbol);
156
+ }
157
+ for (const decl of currentSymbol?.declarations ?? []) {
158
+ const sourceFile$1 = decl.getSourceFile();
159
+ const contextSpan = {
160
+ start: decl.getStart(sourceFile$1),
161
+ length: decl.getWidth(sourceFile$1)
162
+ };
163
+ let textSpan = contextSpan;
164
+ if (ts.isPropertyAssignment(decl) || ts.isPropertySignature(decl)) textSpan = {
154
165
  start: decl.name.getStart(sourceFile$1),
155
166
  length: decl.name.getWidth(sourceFile$1)
156
- } : contextSpan
157
- };
167
+ };
168
+ yield {
169
+ ...definition,
170
+ fileName: sourceFile$1.fileName,
171
+ textSpan,
172
+ contextSpan
173
+ };
174
+ }
158
175
  }
159
176
  }
160
177
  }
@@ -1,10 +1,5 @@
1
1
  import ts from "typescript";
2
2
 
3
3
  //#region src/typescript/index.d.ts
4
- interface Options {
5
- nitroRoutes?: boolean;
6
- runtimeConfig?: boolean;
7
- }
8
4
  declare const plugin: ts.server.PluginModuleFactory;
9
- //#endregion
10
- export { Options, plugin as default };
5
+ export = plugin;
@@ -1,4 +1,4 @@
1
- const require_ast = require('../ast-BfhxgPs7.cjs');
1
+ const require_ast = require('../ast-DJW3KREB.cjs');
2
2
 
3
3
  //#region src/vue/nitro-routes.ts
4
4
  const functionNames = new Set([
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dxup/nuxt",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "description": "TypeScript and Vue plugins for Nuxt",
6
6
  "author": "KazariEX",
7
7
  "license": "MIT",