@dcoder-x/plugin-shared 0.1.1 → 0.1.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.
@@ -6,6 +6,9 @@ export declare class ComponentExtractor {
6
6
  extract(): Promise<DiscoveredElement[]>;
7
7
  private getModuleFilesForRoute;
8
8
  private walkWebpackGraph;
9
+ private findWebpackModule;
10
+ private getWebpackDependencyPaths;
11
+ private getFileImportPaths;
9
12
  private walkRollupGraph;
10
13
  private extractFromSource;
11
14
  private readSource;
@@ -40,19 +40,127 @@ class ComponentExtractor {
40
40
  if (visited.has(current))
41
41
  continue;
42
42
  visited.add(current);
43
- const mod = compilation.moduleGraph.getModuleByIdentifier(current);
44
- if (!mod)
45
- continue;
46
- for (const dep of compilation.moduleGraph.getOutgoingDependencies(mod)) {
47
- const depModule = compilation.moduleGraph.getResolvedModule(dep);
48
- const depPath = normalizeModulePath(depModule?.resource || depModule?.userRequest);
49
- if (depPath && !depPath.includes('node_modules')) {
50
- queue.push(depPath);
43
+ const mod = this.findWebpackModule(compilation, current);
44
+ let queuedFromWebpackGraph = false;
45
+ if (mod) {
46
+ for (const depPath of this.getWebpackDependencyPaths(compilation, mod)) {
47
+ if (depPath && !depPath.includes('node_modules')) {
48
+ queue.push(depPath);
49
+ queuedFromWebpackGraph = true;
50
+ }
51
+ }
52
+ }
53
+ // Fallback for webpack variants where moduleGraph helpers are unavailable.
54
+ if (!queuedFromWebpackGraph) {
55
+ for (const depPath of this.getFileImportPaths(current)) {
56
+ if (depPath && !depPath.includes('node_modules')) {
57
+ queue.push(depPath);
58
+ }
51
59
  }
52
60
  }
53
61
  }
54
62
  return Array.from(visited);
55
63
  }
64
+ findWebpackModule(compilation, filePath) {
65
+ const graph = compilation?.moduleGraph;
66
+ if (graph?.getModuleByIdentifier) {
67
+ const byIdentifier = graph.getModuleByIdentifier(filePath);
68
+ if (byIdentifier)
69
+ return byIdentifier;
70
+ }
71
+ const modules = Array.isArray(compilation?.modules)
72
+ ? compilation.modules
73
+ : compilation?.modules
74
+ ? Array.from(compilation.modules)
75
+ : [];
76
+ for (const mod of modules) {
77
+ const resource = normalizeModulePath(mod?.resource || mod?.userRequest);
78
+ if (resource === filePath)
79
+ return mod;
80
+ const id = normalizeModulePath(typeof mod?.identifier === 'function' ? mod.identifier() : mod?.id);
81
+ if (id === filePath)
82
+ return mod;
83
+ }
84
+ return null;
85
+ }
86
+ getWebpackDependencyPaths(compilation, mod) {
87
+ const graph = compilation?.moduleGraph;
88
+ if (graph?.getOutgoingDependencies && graph?.getResolvedModule) {
89
+ const out = [];
90
+ for (const dep of graph.getOutgoingDependencies(mod)) {
91
+ const depModule = graph.getResolvedModule(dep);
92
+ const depPath = normalizeModulePath(depModule?.resource || depModule?.userRequest);
93
+ if (depPath)
94
+ out.push(depPath);
95
+ }
96
+ return out;
97
+ }
98
+ const fallbackDeps = Array.isArray(mod?.dependencies) ? mod.dependencies : [];
99
+ const out = [];
100
+ for (const dep of fallbackDeps) {
101
+ let depModule = null;
102
+ if (graph?.getModule) {
103
+ depModule = graph.getModule(dep);
104
+ }
105
+ // Legacy fallback for older webpack versions.
106
+ if (!depModule) {
107
+ try {
108
+ depModule = dep?.module || dep?._module;
109
+ }
110
+ catch {
111
+ depModule = null;
112
+ }
113
+ }
114
+ const depPath = normalizeModulePath(depModule?.resource || depModule?.userRequest);
115
+ if (depPath)
116
+ out.push(depPath);
117
+ }
118
+ return out;
119
+ }
120
+ getFileImportPaths(filePath) {
121
+ const source = this.readSource(filePath);
122
+ if (!source)
123
+ return [];
124
+ let ast;
125
+ try {
126
+ ast = (0, parser_1.parse)(source, { sourceType: 'module', plugins: ['typescript', 'jsx'] });
127
+ }
128
+ catch {
129
+ return [];
130
+ }
131
+ const importIds = new Set();
132
+ (0, traverse_1.default)(ast, {
133
+ ImportDeclaration(nodePath) {
134
+ const value = nodePath.node.source?.value;
135
+ if (typeof value === 'string')
136
+ importIds.add(value);
137
+ },
138
+ ExportNamedDeclaration(nodePath) {
139
+ const value = nodePath.node.source?.value;
140
+ if (typeof value === 'string')
141
+ importIds.add(value);
142
+ },
143
+ ExportAllDeclaration(nodePath) {
144
+ const value = nodePath.node.source?.value;
145
+ if (typeof value === 'string')
146
+ importIds.add(value);
147
+ },
148
+ CallExpression(nodePath) {
149
+ if (nodePath.node.callee?.type !== 'Import')
150
+ return;
151
+ const arg = nodePath.node.arguments?.[0];
152
+ if (arg?.type === 'StringLiteral')
153
+ importIds.add(arg.value);
154
+ },
155
+ });
156
+ const resolved = [];
157
+ for (const importId of importIds) {
158
+ const depPath = resolveImportFile(importId, filePath);
159
+ if (depPath)
160
+ resolved.push(depPath);
161
+ }
162
+ return resolved;
163
+ }
56
164
  walkRollupGraph(entryFilePath, graph) {
57
165
  const visited = new Set();
58
166
  const queue = [entryFilePath];
@@ -192,3 +300,35 @@ function inferInteractions(tag, props) {
192
300
  return ['submit'];
193
301
  return ['click'];
194
302
  }
303
+ function resolveImportFile(importId, fromFile) {
304
+ if (!importId)
305
+ return null;
306
+ if (!importId.startsWith('.') && !importId.startsWith('/'))
307
+ return null;
308
+ const base = importId.startsWith('/')
309
+ ? path_1.default.normalize(importId)
310
+ : path_1.default.resolve(path_1.default.dirname(fromFile), importId);
311
+ const candidates = [
312
+ base,
313
+ `${base}.ts`,
314
+ `${base}.tsx`,
315
+ `${base}.js`,
316
+ `${base}.jsx`,
317
+ `${base}.mjs`,
318
+ `${base}.cjs`,
319
+ `${base}.mts`,
320
+ `${base}.cts`,
321
+ path_1.default.join(base, 'index.ts'),
322
+ path_1.default.join(base, 'index.tsx'),
323
+ path_1.default.join(base, 'index.js'),
324
+ path_1.default.join(base, 'index.jsx'),
325
+ path_1.default.join(base, 'index.mjs'),
326
+ path_1.default.join(base, 'index.cjs'),
327
+ ];
328
+ for (const candidate of candidates) {
329
+ if (fs_1.default.existsSync(candidate) && fs_1.default.statSync(candidate).isFile()) {
330
+ return path_1.default.normalize(candidate);
331
+ }
332
+ }
333
+ return null;
334
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcoder-x/plugin-shared",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",