knip 6.25.0 → 6.27.0
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/ConfigurationChief.d.ts +108 -0
- package/dist/DependencyDeputy.d.ts +1 -1
- package/dist/DependencyDeputy.js +2 -2
- package/dist/ProjectPrincipal.d.ts +3 -3
- package/dist/ProjectPrincipal.js +4 -4
- package/dist/binaries/fallback.js +5 -1
- package/dist/binaries/resolvers/bun.js +13 -5
- package/dist/binaries/resolvers/npm.js +4 -2
- package/dist/binaries/resolvers/pnpm.js +11 -1
- package/dist/binaries/resolvers/yarn.js +10 -4
- package/dist/binaries/util.d.ts +4 -0
- package/dist/binaries/util.js +14 -0
- package/dist/compilers/index.d.ts +180 -0
- package/dist/compilers/less.js +1 -1
- package/dist/compilers/scss.js +1 -1
- package/dist/compilers/stylus.js +1 -1
- package/dist/constants.js +19 -0
- package/dist/graph/analyze.js +1 -1
- package/dist/graph/build.js +24 -7
- package/dist/plugins/_vue/auto-import.d.ts +12 -0
- package/dist/plugins/_vue/auto-import.js +332 -0
- package/dist/plugins/_vue/types.d.ts +45 -0
- package/dist/plugins/_vue/types.js +1 -0
- package/dist/plugins/astro/index.js +2 -37
- package/dist/plugins/astro/resolveFromAST.d.ts +4 -4
- package/dist/plugins/astro/resolveFromAST.js +34 -7
- package/dist/plugins/electron-vite/index.d.ts +3 -0
- package/dist/plugins/electron-vite/index.js +51 -0
- package/dist/plugins/electron-vite/types.d.ts +23 -0
- package/dist/plugins/electron-vite/types.js +1 -0
- package/dist/plugins/esbuild/index.d.ts +3 -0
- package/dist/plugins/esbuild/index.js +17 -0
- package/dist/plugins/eslint/resolveFromAST.js +18 -21
- package/dist/plugins/index.d.ts +18 -0
- package/dist/plugins/index.js +36 -0
- package/dist/plugins/laravel-vite-plugin/index.d.ts +3 -0
- package/dist/plugins/laravel-vite-plugin/index.js +14 -0
- package/dist/plugins/laravel-vite-plugin/resolveFromAST.d.ts +2 -0
- package/dist/plugins/laravel-vite-plugin/resolveFromAST.js +27 -0
- package/dist/plugins/next/index.js +2 -2
- package/dist/plugins/next-mdx/index.js +2 -8
- package/dist/plugins/next-mdx/resolveFromAST.d.ts +3 -2
- package/dist/plugins/next-mdx/resolveFromAST.js +17 -21
- package/dist/plugins/nuxt/index.js +5 -68
- package/dist/plugins/nuxt/types.d.ts +0 -34
- package/dist/plugins/nuxtjs-i18n/index.d.ts +3 -0
- package/dist/plugins/nuxtjs-i18n/index.js +12 -0
- package/dist/plugins/oclif/index.js +10 -5
- package/dist/plugins/orval/index.js +1 -3
- package/dist/plugins/orval/resolveFromAST.d.ts +2 -2
- package/dist/plugins/orval/resolveFromAST.js +4 -7
- package/dist/plugins/oxlint/index.js +23 -2
- package/dist/plugins/pnpm/index.js +2 -1
- package/dist/plugins/pnpm/types.d.ts +2 -4
- package/dist/plugins/quasar/index.d.ts +3 -0
- package/dist/plugins/quasar/index.js +15 -0
- package/dist/plugins/quasar/resolveFromAST.d.ts +3 -0
- package/dist/plugins/quasar/resolveFromAST.js +63 -0
- package/dist/plugins/qwik/index.js +1 -25
- package/dist/plugins/qwik/resolveFromAST.d.ts +5 -3
- package/dist/plugins/qwik/resolveFromAST.js +28 -9
- package/dist/plugins/react-email/index.js +1 -0
- package/dist/plugins/rolldown/index.js +2 -5
- package/dist/plugins/rollup/index.js +2 -5
- package/dist/plugins/rslib/index.js +2 -5
- package/dist/plugins/sst/index.js +1 -5
- package/dist/plugins/sst/resolveFromAST.d.ts +1 -1
- package/dist/plugins/sst/resolveFromAST.js +1 -1
- package/dist/plugins/starlight/index.js +1 -19
- package/dist/plugins/starlight/resolveFromAST.d.ts +2 -2
- package/dist/plugins/starlight/resolveFromAST.js +12 -8
- package/dist/plugins/stencil/index.js +2 -37
- package/dist/plugins/stencil/resolveFromAST.d.ts +3 -0
- package/dist/plugins/stencil/resolveFromAST.js +36 -0
- package/dist/plugins/storybook/index.js +3 -0
- package/dist/plugins/sveltekit/index.js +1 -48
- package/dist/plugins/sveltekit/resolveFromAST.d.ts +2 -0
- package/dist/plugins/sveltekit/resolveFromAST.js +45 -0
- package/dist/plugins/tailwind/compiler.js +9 -4
- package/dist/plugins/tanstack-router/index.js +3 -1
- package/dist/plugins/tauri/index.d.ts +3 -0
- package/dist/plugins/tauri/index.js +42 -0
- package/dist/plugins/tauri/types.d.ts +16 -0
- package/dist/plugins/tauri/types.js +1 -0
- package/dist/plugins/temporal/index.d.ts +3 -0
- package/dist/plugins/temporal/index.js +12 -0
- package/dist/plugins/tsdown/index.js +16 -7
- package/dist/plugins/tsdown/types.d.ts +4 -1
- package/dist/plugins/tsup/index.js +2 -5
- package/dist/plugins/unplugin-auto-import/index.d.ts +3 -0
- package/dist/plugins/unplugin-auto-import/index.js +18 -0
- package/dist/plugins/unplugin-icons/index.d.ts +3 -0
- package/dist/plugins/unplugin-icons/index.js +17 -0
- package/dist/plugins/unplugin-vue-components/index.d.ts +3 -0
- package/dist/plugins/unplugin-vue-components/index.js +16 -0
- package/dist/plugins/unplugin-vue-i18n/index.d.ts +3 -0
- package/dist/plugins/unplugin-vue-i18n/index.js +10 -0
- package/dist/plugins/unplugin-vue-markdown/index.d.ts +3 -0
- package/dist/plugins/unplugin-vue-markdown/index.js +16 -0
- package/dist/plugins/unplugin-vue-router/index.d.ts +3 -0
- package/dist/plugins/unplugin-vue-router/index.js +14 -0
- package/dist/plugins/vite/helpers.d.ts +2 -0
- package/dist/plugins/vite/helpers.js +46 -31
- package/dist/plugins/vite/index.js +1 -1
- package/dist/plugins/vite-plugin-pages/index.d.ts +3 -0
- package/dist/plugins/vite-plugin-pages/index.js +26 -0
- package/dist/plugins/vite-plugin-pwa/index.d.ts +3 -0
- package/dist/plugins/vite-plugin-pwa/index.js +17 -0
- package/dist/plugins/vite-plugin-pwa/resolveFromAST.d.ts +4 -0
- package/dist/plugins/vite-plugin-pwa/resolveFromAST.js +37 -0
- package/dist/plugins/vite-plugin-vue-layouts-next/index.d.ts +3 -0
- package/dist/plugins/vite-plugin-vue-layouts-next/index.js +23 -0
- package/dist/plugins/vite-plus/index.d.ts +3 -0
- package/dist/plugins/vite-plus/index.js +14 -0
- package/dist/plugins/vite-plus/resolveFromAST.d.ts +2 -0
- package/dist/plugins/vite-plus/resolveFromAST.js +37 -0
- package/dist/plugins/vite-pwa-assets-generator/index.d.ts +3 -0
- package/dist/plugins/vite-pwa-assets-generator/index.js +12 -0
- package/dist/plugins/vitepress/index.js +5 -1
- package/dist/plugins/vitest/index.js +19 -0
- package/dist/plugins/vitest/types.d.ts +8 -0
- package/dist/plugins/vue/index.js +5 -3
- package/dist/plugins/yarn/index.js +2 -1
- package/dist/plugins/yarn/types.d.ts +2 -4
- package/dist/run.js +3 -3
- package/dist/schema/configuration.d.ts +270 -0
- package/dist/schema/plugins.d.ts +90 -0
- package/dist/schema/plugins.js +18 -0
- package/dist/types/PluginNames.d.ts +2 -2
- package/dist/types/PluginNames.js +18 -0
- package/dist/types/config.d.ts +1 -0
- package/dist/types/package-json.d.ts +8 -4
- package/dist/typescript/ast-helpers.d.ts +7 -2
- package/dist/typescript/ast-helpers.js +36 -8
- package/dist/typescript/ast-nodes.d.ts +2 -0
- package/dist/typescript/ast-nodes.js +8 -0
- package/dist/typescript/get-imports-and-exports.js +8 -5
- package/dist/typescript/resolve-module-names.d.ts +2 -2
- package/dist/typescript/resolve-module-names.js +97 -18
- package/dist/typescript/visitors/walk.d.ts +2 -1
- package/dist/typescript/visitors/walk.js +13 -2
- package/dist/util/cli-arguments.d.ts +1 -1
- package/dist/util/cli-arguments.js +1 -1
- package/dist/util/create-input-handler.js +8 -1
- package/dist/util/create-options.d.ts +180 -0
- package/dist/util/package-json.d.ts +7 -0
- package/dist/util/package-json.js +37 -0
- package/dist/util/resolve.d.ts +1 -0
- package/dist/util/resolve.js +5 -2
- package/dist/util/to-source-path.d.ts +4 -3
- package/dist/util/to-source-path.js +23 -6
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/schema.json +72 -0
- package/dist/plugins/next/resolveFromAST.d.ts +0 -2
- package/dist/plugins/next/resolveFromAST.js +0 -2
- package/dist/plugins/nuxt/helpers.d.ts +0 -15
- package/dist/plugins/nuxt/helpers.js +0 -133
- package/dist/plugins/rolldown/resolveFromAST.d.ts +0 -2
- package/dist/plugins/rolldown/resolveFromAST.js +0 -4
- package/dist/plugins/rollup/resolveFromAST.d.ts +0 -2
- package/dist/plugins/rollup/resolveFromAST.js +0 -4
- package/dist/plugins/rslib/resolveFromAST.d.ts +0 -2
- package/dist/plugins/rslib/resolveFromAST.js +0 -4
- package/dist/plugins/tsdown/resolveFromAST.d.ts +0 -2
- package/dist/plugins/tsdown/resolveFromAST.js +0 -4
- package/dist/plugins/tsup/resolveFromAST.d.ts +0 -2
- package/dist/plugins/tsup/resolveFromAST.js +0 -4
- package/dist/plugins/vue/compiler.d.ts +0 -3
- package/dist/plugins/vue/compiler.js +0 -10
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { Visitor } from 'oxc-parser';
|
|
4
|
+
import { scriptBodies } from '../../compilers/compilers.js';
|
|
5
|
+
import { stylePreprocessorImports } from '../../compilers/style-preprocessors.js';
|
|
6
|
+
import { _parseFile } from '../../typescript/ast-nodes.js';
|
|
7
|
+
import { isFile } from '../../util/fs.js';
|
|
8
|
+
import { _syncGlob } from '../../util/glob.js';
|
|
9
|
+
import { dirname, isInNodeModules, join } from '../../util/path.js';
|
|
10
|
+
const getVueSfc = (cwd) => {
|
|
11
|
+
try {
|
|
12
|
+
const sfc = createRequire(join(cwd, 'package.json'))('vue/compiler-sfc');
|
|
13
|
+
if (typeof sfc.parseComponent === 'function') {
|
|
14
|
+
return {
|
|
15
|
+
parse: (source, path) => ({
|
|
16
|
+
descriptor: sfc.parse({ source, filename: path, sourceMap: false }),
|
|
17
|
+
}),
|
|
18
|
+
compileTemplate: (source, path, isTS, preprocessLang) => sfc.compileTemplate({
|
|
19
|
+
source,
|
|
20
|
+
filename: path,
|
|
21
|
+
sourceMap: false,
|
|
22
|
+
isProduction: true,
|
|
23
|
+
prettify: false,
|
|
24
|
+
isTS,
|
|
25
|
+
preprocessLang,
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
parse: (source, path) => sfc.parse(source, { filename: path, sourceMap: false }),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
catch { }
|
|
34
|
+
return {
|
|
35
|
+
parse: (source, path) => ({
|
|
36
|
+
descriptor: { script: { content: scriptBodies(source, path) }, scriptSetup: null, template: { content: '' } },
|
|
37
|
+
}),
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const readFile = (filePath) => {
|
|
41
|
+
try {
|
|
42
|
+
return readFileSync(filePath, 'utf8');
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return '';
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
export const readAndParseFile = (filePath) => _parseFile(filePath, readFile(filePath));
|
|
49
|
+
const collectIdentifiers = (source, fileName) => {
|
|
50
|
+
const identifiers = new Set();
|
|
51
|
+
const visitor = new Visitor({
|
|
52
|
+
Identifier(node) {
|
|
53
|
+
identifiers.add(node.name);
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
visitor.visit(_parseFile(fileName, source).program);
|
|
57
|
+
return identifiers;
|
|
58
|
+
};
|
|
59
|
+
const collectTemplateInfo = (tree) => {
|
|
60
|
+
const tags = new Set();
|
|
61
|
+
const identifiers = new Set();
|
|
62
|
+
const visited = new Set();
|
|
63
|
+
const addExprIdentifiers = (expr) => {
|
|
64
|
+
for (const id of collectIdentifiers(expr, 'expr.ts'))
|
|
65
|
+
identifiers.add(id);
|
|
66
|
+
};
|
|
67
|
+
const visit = (node) => {
|
|
68
|
+
if (visited.has(node))
|
|
69
|
+
return;
|
|
70
|
+
visited.add(node);
|
|
71
|
+
if (node.tag)
|
|
72
|
+
tags.add(node.tag);
|
|
73
|
+
if (node.type === 5 && node.content && !node.content.isStatic)
|
|
74
|
+
addExprIdentifiers(node.content.content);
|
|
75
|
+
if (node.props) {
|
|
76
|
+
for (const prop of node.props) {
|
|
77
|
+
if (prop.type === 7) {
|
|
78
|
+
if (prop.exp && !prop.exp.isStatic)
|
|
79
|
+
addExprIdentifiers(prop.exp.content);
|
|
80
|
+
if (prop.arg && !prop.arg.isStatic)
|
|
81
|
+
addExprIdentifiers(prop.arg.content);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (node.children)
|
|
86
|
+
for (const child of node.children)
|
|
87
|
+
visit(child);
|
|
88
|
+
if (node.ifConditions)
|
|
89
|
+
for (const condition of node.ifConditions)
|
|
90
|
+
visit(condition.block);
|
|
91
|
+
if (node.scopedSlots)
|
|
92
|
+
for (const name in node.scopedSlots)
|
|
93
|
+
visit(node.scopedSlots[name]);
|
|
94
|
+
};
|
|
95
|
+
visit(tree);
|
|
96
|
+
return { tags, identifiers };
|
|
97
|
+
};
|
|
98
|
+
const collectVue2TemplateIdentifiers = (source) => {
|
|
99
|
+
const identifiers = new Set();
|
|
100
|
+
const visitor = new Visitor({
|
|
101
|
+
MemberExpression(node) {
|
|
102
|
+
if (!node.computed &&
|
|
103
|
+
node.object.type === 'Identifier' &&
|
|
104
|
+
node.object.name === '_vm' &&
|
|
105
|
+
node.property.type === 'Identifier') {
|
|
106
|
+
const name = node.property.name;
|
|
107
|
+
if (name !== '_self' && name !== '$set' && name !== '$delete' && !(name.length === 2 && name[0] === '_')) {
|
|
108
|
+
identifiers.add(name);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
visitor.visit(_parseFile('template.js', source).program);
|
|
114
|
+
return identifiers;
|
|
115
|
+
};
|
|
116
|
+
const toKebabCase = (s) => s.replace(/[A-Z]/g, (m, i) => (i ? '-' : '') + m.toLowerCase());
|
|
117
|
+
const isLocalSpecifier = (specifier) => specifier.startsWith('.') && !isInNodeModules(specifier);
|
|
118
|
+
export const collectLocalImportPaths = (filePath, result) => {
|
|
119
|
+
const dir = dirname(filePath);
|
|
120
|
+
const paths = new Set();
|
|
121
|
+
const visitor = new Visitor({
|
|
122
|
+
TSImportType(node) {
|
|
123
|
+
const specifier = node.source.value;
|
|
124
|
+
if (isLocalSpecifier(specifier))
|
|
125
|
+
paths.add(join(dir, specifier));
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
visitor.visit(result.program);
|
|
129
|
+
return paths;
|
|
130
|
+
};
|
|
131
|
+
export function buildAutoImportMap(filePath, result, maps, isComponents) {
|
|
132
|
+
const dir = dirname(filePath);
|
|
133
|
+
const importTypes = [];
|
|
134
|
+
const collectVisitor = new Visitor({
|
|
135
|
+
TSImportType(node) {
|
|
136
|
+
importTypes.push({ start: node.start, end: node.end, specifier: node.source.value });
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
collectVisitor.visit(result.program);
|
|
140
|
+
const addEntry = (name, start, end) => {
|
|
141
|
+
const importType = importTypes.find(it => it.start >= start && it.end <= end);
|
|
142
|
+
if (!importType || !isLocalSpecifier(importType.specifier))
|
|
143
|
+
return;
|
|
144
|
+
const absSpecifier = join(dir, importType.specifier);
|
|
145
|
+
if (isComponents) {
|
|
146
|
+
const components = maps.componentMap.get(name);
|
|
147
|
+
if (components) {
|
|
148
|
+
if (!components.includes(absSpecifier))
|
|
149
|
+
components.push(absSpecifier);
|
|
150
|
+
}
|
|
151
|
+
else
|
|
152
|
+
maps.componentMap.set(name, [absSpecifier]);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
maps.importMap.set(name, absSpecifier);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const matchVisitor = new Visitor({
|
|
159
|
+
VariableDeclarator(node) {
|
|
160
|
+
if (node.id?.type !== 'Identifier' || node.id.name.startsWith('Lazy'))
|
|
161
|
+
return;
|
|
162
|
+
addEntry(node.id.name, node.start, node.end);
|
|
163
|
+
},
|
|
164
|
+
TSPropertySignature(node) {
|
|
165
|
+
if (!isComponents)
|
|
166
|
+
return;
|
|
167
|
+
const key = node.key;
|
|
168
|
+
const name = key?.type === 'Identifier' ? key.name : key?.type === 'Literal' ? String(key.value) : undefined;
|
|
169
|
+
if (name)
|
|
170
|
+
addEntry(name, node.start, node.end);
|
|
171
|
+
},
|
|
172
|
+
ExportNamedDeclaration(node) {
|
|
173
|
+
if (!node.source)
|
|
174
|
+
return;
|
|
175
|
+
const specifier = node.source.value;
|
|
176
|
+
if (!isLocalSpecifier(specifier))
|
|
177
|
+
return;
|
|
178
|
+
const absSpecifier = join(dir, specifier);
|
|
179
|
+
for (const s of node.specifiers) {
|
|
180
|
+
const name = s.exported.type === 'Identifier' ? s.exported.name : s.exported.value;
|
|
181
|
+
if (name)
|
|
182
|
+
maps.importMap.set(name, absSpecifier);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
matchVisitor.visit(result.program);
|
|
187
|
+
}
|
|
188
|
+
export const createAutoImportMaps = () => ({ importMap: new Map(), componentMap: new Map() });
|
|
189
|
+
export const findGeneratedDts = (cwd, signature) => _syncGlob({ cwd, patterns: ['*.d.ts', 'src/**/*.d.ts'] }).filter(filePath => readFile(filePath).slice(0, 300).includes(signature));
|
|
190
|
+
const generators = [
|
|
191
|
+
{ signature: 'Generated by unplugin-vue-components', isComponents: true },
|
|
192
|
+
{ signature: 'Generated by unplugin-auto-import', isComponents: false },
|
|
193
|
+
];
|
|
194
|
+
const getVueAutoImportMaps = (cwd) => {
|
|
195
|
+
const maps = createAutoImportMaps();
|
|
196
|
+
for (const filePath of _syncGlob({ cwd, patterns: ['*.d.ts', 'src/**/*.d.ts'] })) {
|
|
197
|
+
const source = readFile(filePath);
|
|
198
|
+
const generator = generators.find(({ signature }) => source.slice(0, 300).includes(signature));
|
|
199
|
+
if (generator)
|
|
200
|
+
buildAutoImportMap(filePath, _parseFile(filePath, source), maps, generator.isComponents);
|
|
201
|
+
}
|
|
202
|
+
return maps;
|
|
203
|
+
};
|
|
204
|
+
const getSyntheticImports = (maps, identifiers, templateTags) => {
|
|
205
|
+
const { importMap, componentMap } = maps;
|
|
206
|
+
if (importMap.size === 0 && (!templateTags || componentMap.size === 0))
|
|
207
|
+
return [];
|
|
208
|
+
const syntheticImports = [];
|
|
209
|
+
for (const [name, specifier] of importMap) {
|
|
210
|
+
if (identifiers.has(name))
|
|
211
|
+
syntheticImports.push(`import { ${name} } from '${specifier}';`);
|
|
212
|
+
}
|
|
213
|
+
if (templateTags) {
|
|
214
|
+
for (const [name, specifiers] of componentMap) {
|
|
215
|
+
const kebab = toKebabCase(name);
|
|
216
|
+
if (templateTags.has(name) ||
|
|
217
|
+
templateTags.has(kebab) ||
|
|
218
|
+
templateTags.has(`Lazy${name}`) ||
|
|
219
|
+
templateTags.has(`lazy-${kebab}`)) {
|
|
220
|
+
syntheticImports.push(`import { default as ${name} } from '${specifiers[0]}';`);
|
|
221
|
+
for (let i = 1; i < specifiers.length; i++)
|
|
222
|
+
syntheticImports.push(`import '${specifiers[i]}';`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return syntheticImports;
|
|
227
|
+
};
|
|
228
|
+
const compileVueSfc = (source, path, maps, root) => {
|
|
229
|
+
if (maps.importMap.size === 0 && maps.componentMap.size === 0) {
|
|
230
|
+
return [scriptBodies(source, path), stylePreprocessorImports(source, path)].filter(Boolean).join(';\n');
|
|
231
|
+
}
|
|
232
|
+
const sfc = sfcForRoot(root);
|
|
233
|
+
const { descriptor } = sfc.parse(source, path);
|
|
234
|
+
const scripts = [];
|
|
235
|
+
if (descriptor.script?.content)
|
|
236
|
+
scripts.push(descriptor.script.content);
|
|
237
|
+
if (descriptor.scriptSetup?.content)
|
|
238
|
+
scripts.push(descriptor.scriptSetup.content);
|
|
239
|
+
const identifiers = scripts.length === 0 ? new Set() : collectIdentifiers(scripts.join('\n'), path);
|
|
240
|
+
const template = descriptor.template;
|
|
241
|
+
const compiled = template && !template.ast && sfc.compileTemplate
|
|
242
|
+
? sfc.compileTemplate(template.content, path, descriptor.script?.lang === 'ts' ||
|
|
243
|
+
descriptor.script?.lang === 'tsx' ||
|
|
244
|
+
descriptor.scriptSetup?.lang === 'ts' ||
|
|
245
|
+
descriptor.scriptSetup?.lang === 'tsx', template.lang)
|
|
246
|
+
: undefined;
|
|
247
|
+
let templateTags;
|
|
248
|
+
const templateAst = template?.ast ?? compiled?.ast;
|
|
249
|
+
if (templateAst) {
|
|
250
|
+
const info = collectTemplateInfo(templateAst);
|
|
251
|
+
templateTags = info.tags;
|
|
252
|
+
for (const id of info.identifiers)
|
|
253
|
+
identifiers.add(id);
|
|
254
|
+
}
|
|
255
|
+
if (compiled?.code) {
|
|
256
|
+
for (const id of collectVue2TemplateIdentifiers(compiled.code))
|
|
257
|
+
identifiers.add(id);
|
|
258
|
+
}
|
|
259
|
+
scripts.push(...getSyntheticImports(maps, identifiers, templateTags));
|
|
260
|
+
const styles = stylePreprocessorImports(source, path);
|
|
261
|
+
if (styles)
|
|
262
|
+
scripts.push(styles);
|
|
263
|
+
return scripts.join(';\n');
|
|
264
|
+
};
|
|
265
|
+
const compileTs = (source, path, maps) => {
|
|
266
|
+
if (maps.importMap.size === 0 || path.endsWith('.d.ts') || path.endsWith('.config.ts'))
|
|
267
|
+
return source;
|
|
268
|
+
const syntheticImports = getSyntheticImports(maps, collectIdentifiers(source, path));
|
|
269
|
+
return syntheticImports.length === 0 ? source : `${source}\n${syntheticImports.join('\n')}`;
|
|
270
|
+
};
|
|
271
|
+
const tagMatcher = /<([a-zA-Z][\w.-]*)/g;
|
|
272
|
+
const fencedCodeMatcher = /(`{3,}|~{3,})[\s\S]*?^[ \t]*\1/gm;
|
|
273
|
+
const inlineCodeMatcher = /`[^`\n]*`/g;
|
|
274
|
+
const stripCode = (md) => md.replace(fencedCodeMatcher, '').replace(inlineCodeMatcher, '');
|
|
275
|
+
const compileMarkdown = (source, path, maps) => {
|
|
276
|
+
const code = stripCode(source);
|
|
277
|
+
const scripts = scriptBodies(code, path);
|
|
278
|
+
if (maps.importMap.size === 0 && maps.componentMap.size === 0)
|
|
279
|
+
return scripts;
|
|
280
|
+
const tags = new Set();
|
|
281
|
+
for (const [, tag] of code.matchAll(tagMatcher))
|
|
282
|
+
tags.add(tag);
|
|
283
|
+
const identifiers = scripts ? collectIdentifiers(scripts, path) : new Set();
|
|
284
|
+
return [scripts, ...getSyntheticImports(maps, identifiers, tags)].filter(Boolean).join(';\n');
|
|
285
|
+
};
|
|
286
|
+
export const createVueCompiler = (maps, cwd) => (source, path) => compileVueSfc(source, path, maps, cwd);
|
|
287
|
+
export const createTsCompiler = (maps) => (source, path) => compileTs(source, path, maps);
|
|
288
|
+
const EMPTY_MAPS = createAutoImportMaps();
|
|
289
|
+
const rootByDir = new Map();
|
|
290
|
+
const mapsByRoot = new Map();
|
|
291
|
+
const sfcByRoot = new Map();
|
|
292
|
+
const findRoot = (fromDir) => {
|
|
293
|
+
const cached = rootByDir.get(fromDir);
|
|
294
|
+
if (cached !== undefined)
|
|
295
|
+
return cached;
|
|
296
|
+
let dir = fromDir;
|
|
297
|
+
let root = '';
|
|
298
|
+
for (;;) {
|
|
299
|
+
if (isFile(join(dir, 'package.json'))) {
|
|
300
|
+
root = dir;
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
const parent = dirname(dir);
|
|
304
|
+
if (parent === dir)
|
|
305
|
+
break;
|
|
306
|
+
dir = parent;
|
|
307
|
+
}
|
|
308
|
+
rootByDir.set(fromDir, root);
|
|
309
|
+
return root;
|
|
310
|
+
};
|
|
311
|
+
const mapsForFile = (path) => {
|
|
312
|
+
const root = findRoot(dirname(path));
|
|
313
|
+
if (!root)
|
|
314
|
+
return EMPTY_MAPS;
|
|
315
|
+
let maps = mapsByRoot.get(root);
|
|
316
|
+
if (!maps) {
|
|
317
|
+
maps = getVueAutoImportMaps(root);
|
|
318
|
+
mapsByRoot.set(root, maps);
|
|
319
|
+
}
|
|
320
|
+
return maps;
|
|
321
|
+
};
|
|
322
|
+
function sfcForRoot(root) {
|
|
323
|
+
let sfc = sfcByRoot.get(root);
|
|
324
|
+
if (!sfc) {
|
|
325
|
+
sfc = getVueSfc(root);
|
|
326
|
+
sfcByRoot.set(root, sfc);
|
|
327
|
+
}
|
|
328
|
+
return sfc;
|
|
329
|
+
}
|
|
330
|
+
export const vueAutoImportCompiler = (source, path) => compileVueSfc(source, path, mapsForFile(path), findRoot(dirname(path)));
|
|
331
|
+
export const tsAutoImportCompiler = (source, path) => compileTs(source, path, mapsForFile(path));
|
|
332
|
+
export const markdownAutoImportCompiler = (source, path) => compileMarkdown(source, path, mapsForFile(path));
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
interface TemplateExpressionNode {
|
|
2
|
+
content: string;
|
|
3
|
+
isStatic: boolean;
|
|
4
|
+
}
|
|
5
|
+
interface TemplateAstProp {
|
|
6
|
+
type: number;
|
|
7
|
+
exp?: TemplateExpressionNode;
|
|
8
|
+
arg?: TemplateExpressionNode;
|
|
9
|
+
}
|
|
10
|
+
export interface TemplateAstNode {
|
|
11
|
+
type?: number;
|
|
12
|
+
tag?: string;
|
|
13
|
+
props?: TemplateAstProp[];
|
|
14
|
+
content?: TemplateExpressionNode;
|
|
15
|
+
children?: TemplateAstNode[];
|
|
16
|
+
ifConditions?: {
|
|
17
|
+
block: TemplateAstNode;
|
|
18
|
+
}[];
|
|
19
|
+
scopedSlots?: Record<string, TemplateAstNode>;
|
|
20
|
+
}
|
|
21
|
+
interface DescriptorBlock {
|
|
22
|
+
content: string;
|
|
23
|
+
lang?: string;
|
|
24
|
+
}
|
|
25
|
+
interface Descriptor {
|
|
26
|
+
script: DescriptorBlock | null;
|
|
27
|
+
scriptSetup: DescriptorBlock | null;
|
|
28
|
+
template: (DescriptorBlock & {
|
|
29
|
+
ast?: TemplateAstNode;
|
|
30
|
+
}) | null;
|
|
31
|
+
}
|
|
32
|
+
export interface VueSfc {
|
|
33
|
+
parse: (source: string, path: string) => {
|
|
34
|
+
descriptor: Descriptor;
|
|
35
|
+
};
|
|
36
|
+
compileTemplate?: (source: string, path: string, isTS: boolean, preprocessLang?: string) => {
|
|
37
|
+
ast?: TemplateAstNode;
|
|
38
|
+
code: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface AutoImportMaps {
|
|
42
|
+
importMap: Map<string, string>;
|
|
43
|
+
componentMap: Map<string, string[]>;
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,47 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { toConfig, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
|
|
1
|
+
import { toDependency } from '../../util/input.js';
|
|
3
2
|
import { hasDependency } from '../../util/plugin.js';
|
|
4
|
-
import { getAliasInputs } from '../vitest/helpers.js';
|
|
5
3
|
import compiler from './compiler.js';
|
|
6
4
|
import mdxCompiler from './compiler-mdx.js';
|
|
7
|
-
import {
|
|
5
|
+
import { entry, production, resolveFromAST } from './resolveFromAST.js';
|
|
8
6
|
const title = 'Astro';
|
|
9
7
|
const enablers = ['astro'];
|
|
10
8
|
const isEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
|
|
11
9
|
export const config = ['astro.config.{js,cjs,mjs,ts,mts}'];
|
|
12
|
-
const entry = ['src/content/config.ts', 'src/content.config.ts'];
|
|
13
|
-
const production = [
|
|
14
|
-
'src/pages/**/*.{astro,mdx,js,ts}',
|
|
15
|
-
'!src/pages/**/_*',
|
|
16
|
-
'!src/pages/**/_*/**',
|
|
17
|
-
'src/content/**/*.mdx',
|
|
18
|
-
'src/middleware.{js,ts}',
|
|
19
|
-
'src/middleware/index.{js,ts}',
|
|
20
|
-
'src/actions/index.{js,ts}',
|
|
21
|
-
];
|
|
22
|
-
const resolveFromAST = (program, options) => {
|
|
23
|
-
const srcDir = getSrcDir(program);
|
|
24
|
-
const setSrcDir = (entry) => entry.replace(/^src\//, `${srcDir}/`);
|
|
25
|
-
const inputs = [
|
|
26
|
-
...entry.map(setSrcDir).map(path => toEntry(path)),
|
|
27
|
-
...production.map(setSrcDir).map(path => toProductionEntry(path)),
|
|
28
|
-
...getAliasInputs(getViteAliases(program), options.cwd),
|
|
29
|
-
];
|
|
30
|
-
if (!usesPassthroughImageService(program))
|
|
31
|
-
inputs.push(toDependency('sharp', { optional: true }));
|
|
32
|
-
const importMap = getImportMap(program);
|
|
33
|
-
const lunariaImportName = getDefaultImportName(importMap, '@lunariajs/starlight');
|
|
34
|
-
if (lunariaImportName) {
|
|
35
|
-
const lunariaConfig = findCallArg(program, lunariaImportName);
|
|
36
|
-
if (lunariaConfig) {
|
|
37
|
-
const configPaths = getPropertyValues(lunariaConfig, 'configPath');
|
|
38
|
-
for (const id of configPaths) {
|
|
39
|
-
inputs.push(toConfig('lunaria', id));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return inputs;
|
|
44
|
-
};
|
|
45
10
|
const registerCompilers = ({ registerCompiler, hasDependency }) => {
|
|
46
11
|
if (hasDependency('astro'))
|
|
47
12
|
registerCompiler({ extension: '.astro', compiler });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
1
|
+
import type { ResolveFromAST } from '../../types/config.ts';
|
|
2
|
+
export declare const entry: string[];
|
|
3
|
+
export declare const production: string[];
|
|
4
|
+
export declare const resolveFromAST: ResolveFromAST;
|
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
import { collectFirstPropertyValue, findImportedCallArg, findProperty, getPropertyKey, getPropertyValues, hasImportSpecifier, resolveObjectArg, } from '../../typescript/ast-helpers.js';
|
|
1
2
|
import { getStringValue } from '../../typescript/ast-nodes.js';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
3
|
+
import { toConfig, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
|
|
4
|
+
import { getAliasInputs } from '../vitest/helpers.js';
|
|
5
|
+
export const entry = ['src/content/config.ts', 'src/content.config.ts'];
|
|
6
|
+
export const production = [
|
|
7
|
+
'src/pages/**/*.{astro,mdx,js,ts}',
|
|
8
|
+
'!src/pages/**/_*',
|
|
9
|
+
'!src/pages/**/_*/**',
|
|
10
|
+
'src/content/**/*.mdx',
|
|
11
|
+
'src/middleware.{js,ts}',
|
|
12
|
+
'src/middleware/index.{js,ts}',
|
|
13
|
+
'src/actions/index.{js,ts}',
|
|
14
|
+
];
|
|
15
|
+
const getSrcDir = (program) => collectFirstPropertyValue(program, 'srcDir') ?? 'src';
|
|
16
|
+
const usesPassthroughImageService = (program) => hasImportSpecifier(program, 'astro/config', 'passthroughImageService');
|
|
8
17
|
const findFirstStringArg = (node) => {
|
|
9
18
|
const literal = getStringValue(node);
|
|
10
19
|
if (literal)
|
|
@@ -17,7 +26,7 @@ const findFirstStringArg = (node) => {
|
|
|
17
26
|
}
|
|
18
27
|
}
|
|
19
28
|
};
|
|
20
|
-
|
|
29
|
+
const getViteAliases = (program) => {
|
|
21
30
|
const aliases = {};
|
|
22
31
|
for (const node of program.body ?? []) {
|
|
23
32
|
if (node.type !== 'ExportDefaultDeclaration')
|
|
@@ -40,3 +49,21 @@ export const getViteAliases = (program) => {
|
|
|
40
49
|
}
|
|
41
50
|
return aliases;
|
|
42
51
|
};
|
|
52
|
+
export const resolveFromAST = (program, options) => {
|
|
53
|
+
const srcDir = getSrcDir(program);
|
|
54
|
+
const setSrcDir = (entry) => entry.replace(/^src\//, `${srcDir}/`);
|
|
55
|
+
const inputs = [
|
|
56
|
+
...entry.map(setSrcDir).map(path => toEntry(path)),
|
|
57
|
+
...production.map(setSrcDir).map(path => toProductionEntry(path)),
|
|
58
|
+
...getAliasInputs(getViteAliases(program), options.cwd),
|
|
59
|
+
];
|
|
60
|
+
if (!usesPassthroughImageService(program))
|
|
61
|
+
inputs.push(toDependency('sharp', { optional: true }));
|
|
62
|
+
const lunariaConfig = findImportedCallArg(program, '@lunariajs/starlight');
|
|
63
|
+
if (lunariaConfig) {
|
|
64
|
+
for (const id of getPropertyValues(lunariaConfig, 'configPath')) {
|
|
65
|
+
inputs.push(toConfig('lunaria', id));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return inputs;
|
|
69
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { toProductionEntry } from '../../util/input.js';
|
|
2
|
+
import { isAbsolute, join } from '../../util/path.js';
|
|
3
|
+
import { hasDependency } from '../../util/plugin.js';
|
|
4
|
+
import { getHtmlScriptEntries } from '../vite/helpers.js';
|
|
5
|
+
const title = 'electron-vite';
|
|
6
|
+
const enablers = ['electron-vite'];
|
|
7
|
+
const isEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
|
|
8
|
+
const config = ['electron.vite.config.{js,mjs,cjs,ts,mts,cts}'];
|
|
9
|
+
const defaultEntry = {
|
|
10
|
+
main: 'src/main/index.{js,mjs,cjs,ts,mts,cts}',
|
|
11
|
+
preload: 'src/preload/index.{js,mjs,cjs,ts,mts,cts}',
|
|
12
|
+
renderer: 'src/renderer/index.html',
|
|
13
|
+
};
|
|
14
|
+
const production = Object.values(defaultEntry);
|
|
15
|
+
const buildEnv = { command: 'build', mode: 'production' };
|
|
16
|
+
const resolveConfig = async (localConfig, options) => {
|
|
17
|
+
const { configFileDir } = options;
|
|
18
|
+
const config = typeof localConfig === 'function' ? await localConfig(buildEnv) : localConfig;
|
|
19
|
+
const inputs = [];
|
|
20
|
+
for (const name of ['main', 'preload', 'renderer']) {
|
|
21
|
+
let section = config?.[name];
|
|
22
|
+
if (typeof section === 'function')
|
|
23
|
+
section = await section(buildEnv);
|
|
24
|
+
const ids = [];
|
|
25
|
+
for (const input of [section?.build?.rollupOptions?.input, section?.build?.lib?.entry]) {
|
|
26
|
+
if (typeof input === 'string')
|
|
27
|
+
ids.push(input);
|
|
28
|
+
else if (input)
|
|
29
|
+
ids.push(...Object.values(input));
|
|
30
|
+
}
|
|
31
|
+
const patterns = ids.length > 0 ? ids : [defaultEntry[name]];
|
|
32
|
+
for (const id of patterns) {
|
|
33
|
+
const resolved = isAbsolute(id) ? id : join(configFileDir, id);
|
|
34
|
+
inputs.push(toProductionEntry(resolved));
|
|
35
|
+
if (name === 'renderer' && resolved.endsWith('.html')) {
|
|
36
|
+
for (const input of await getHtmlScriptEntries(resolved))
|
|
37
|
+
inputs.push(input);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return inputs;
|
|
42
|
+
};
|
|
43
|
+
const plugin = {
|
|
44
|
+
title,
|
|
45
|
+
enablers,
|
|
46
|
+
isEnabled,
|
|
47
|
+
config,
|
|
48
|
+
production,
|
|
49
|
+
resolveConfig,
|
|
50
|
+
};
|
|
51
|
+
export default plugin;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type EntryInput = string | string[] | Record<string, string>;
|
|
2
|
+
interface ViteBuildConfig {
|
|
3
|
+
build?: {
|
|
4
|
+
rollupOptions?: {
|
|
5
|
+
input?: EntryInput;
|
|
6
|
+
};
|
|
7
|
+
lib?: {
|
|
8
|
+
entry?: EntryInput;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
type ConfigEnv = {
|
|
13
|
+
command: string;
|
|
14
|
+
mode: string;
|
|
15
|
+
};
|
|
16
|
+
type Section = ViteBuildConfig | ((env: ConfigEnv) => ViteBuildConfig | Promise<ViteBuildConfig>);
|
|
17
|
+
type ElectronViteConfigObject = {
|
|
18
|
+
main?: Section;
|
|
19
|
+
preload?: Section;
|
|
20
|
+
renderer?: Section;
|
|
21
|
+
};
|
|
22
|
+
export type ElectronViteConfig = ElectronViteConfigObject | ((env: ConfigEnv) => ElectronViteConfigObject | Promise<ElectronViteConfigObject>);
|
|
23
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { collectPropertyValues } from '../../typescript/ast-helpers.js';
|
|
2
|
+
import { toProductionEntry } from '../../util/input.js';
|
|
3
|
+
import { join } from '../../util/path.js';
|
|
4
|
+
import { hasDependency } from '../../util/plugin.js';
|
|
5
|
+
const title = 'esbuild';
|
|
6
|
+
const enablers = ['esbuild'];
|
|
7
|
+
const isEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
|
|
8
|
+
const config = ['esbuild.config.{js,mjs,cjs,ts,mts,cts}', 'esbuild.{js,mjs,cjs,ts,mts,cts}'];
|
|
9
|
+
const resolveFromAST = (program, { configFileDir }) => [...collectPropertyValues(program, 'entryPoints')].map(id => toProductionEntry(join(configFileDir, id)));
|
|
10
|
+
const plugin = {
|
|
11
|
+
title,
|
|
12
|
+
enablers,
|
|
13
|
+
isEnabled,
|
|
14
|
+
config,
|
|
15
|
+
resolveFromAST,
|
|
16
|
+
};
|
|
17
|
+
export default plugin;
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Visitor } from 'oxc-parser';
|
|
2
2
|
import { toDeferResolve } from '../../util/input.js';
|
|
3
|
-
import { findProperty } from '../../typescript/ast-helpers.js';
|
|
3
|
+
import { findProperty, getPropertyKey } from '../../typescript/ast-helpers.js';
|
|
4
|
+
import { getStringValue } from '../../typescript/ast-nodes.js';
|
|
4
5
|
import { isInternal } from '../../util/path.js';
|
|
5
6
|
export const getInputsFromFlatConfigAST = (program) => {
|
|
6
7
|
const inputs = [];
|
|
8
|
+
const addResolver = (key, resolver) => {
|
|
9
|
+
if (!resolver || resolver === 'node' || isInternal(resolver))
|
|
10
|
+
return;
|
|
11
|
+
const dep = key === 'import/resolver' ? `eslint-import-resolver-${resolver}` : resolver;
|
|
12
|
+
inputs.push(toDeferResolve(dep, { optional: true }));
|
|
13
|
+
};
|
|
7
14
|
const visitor = new Visitor({
|
|
8
15
|
ObjectExpression(node) {
|
|
9
16
|
const settingsNode = findProperty(node, 'settings');
|
|
@@ -12,28 +19,18 @@ export const getInputsFromFlatConfigAST = (program) => {
|
|
|
12
19
|
for (const prop of settingsNode.properties ?? []) {
|
|
13
20
|
if (prop.type !== 'Property')
|
|
14
21
|
continue;
|
|
15
|
-
const key = prop
|
|
16
|
-
if (key
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (resolver && resolver !== 'node' && !isInternal(resolver)) {
|
|
23
|
-
const dep = key === 'import/resolver' ? `eslint-import-resolver-${resolver}` : resolver;
|
|
24
|
-
inputs.push(toDeferResolve(dep, { optional: true }));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
else if (prop.value?.type === 'StringLiteral' ||
|
|
29
|
-
(prop.value?.type === 'Literal' && typeof prop.value.value === 'string')) {
|
|
30
|
-
const resolver = prop.value.value;
|
|
31
|
-
if (resolver && resolver !== 'node' && !isInternal(resolver)) {
|
|
32
|
-
const dep = key === 'import/resolver' ? `eslint-import-resolver-${resolver}` : resolver;
|
|
33
|
-
inputs.push(toDeferResolve(dep, { optional: true }));
|
|
34
|
-
}
|
|
22
|
+
const key = getPropertyKey(prop);
|
|
23
|
+
if (key !== 'import/resolver' && key !== 'import/parsers')
|
|
24
|
+
continue;
|
|
25
|
+
if (prop.value?.type === 'ObjectExpression') {
|
|
26
|
+
for (const p of prop.value.properties ?? []) {
|
|
27
|
+
if (p.type === 'Property')
|
|
28
|
+
addResolver(key, getPropertyKey(p));
|
|
35
29
|
}
|
|
36
30
|
}
|
|
31
|
+
else {
|
|
32
|
+
addResolver(key, getStringValue(prop.value));
|
|
33
|
+
}
|
|
37
34
|
}
|
|
38
35
|
},
|
|
39
36
|
});
|