@batijs/core 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.
@@ -0,0 +1,28 @@
1
+ import { ASTNode, ProxifiedModule } from 'magicast';
2
+ export { generateCode, loadFile, parseModule } from 'magicast';
3
+ export { addVitePlugin } from 'magicast/helpers';
4
+
5
+ declare const features: readonly ["framework:solid", "auth:authjs", "rpc:telefunc", "server:hattip", "server:express"];
6
+ declare const flags: Map<string | undefined, "framework:solid" | "auth:authjs" | "rpc:telefunc" | "server:hattip" | "server:express">;
7
+
8
+ type MaybeContentGetter = (() => string | Promise<string>) | undefined;
9
+ interface VikeMeta {
10
+ VIKE_MODULES?: (typeof features)[number][];
11
+ }
12
+
13
+ declare function transformAndGenerate(tree: ASTNode, meta: VikeMeta): Promise<string>;
14
+
15
+ declare function loadAsJson(getter: MaybeContentGetter): Promise<any>;
16
+ declare function loadAsMagicast<Exports extends object>(getter: MaybeContentGetter): Promise<ProxifiedModule<Exports>>;
17
+ declare function loadRelativeFileAsMagicast<Exports extends object>(relativePath: string, meta: Pick<ImportMeta, "url">): Promise<ProxifiedModule<Exports>>;
18
+
19
+ interface PackageJsonDeps {
20
+ dependencies?: Record<string, string>;
21
+ devDependencies?: Record<string, string>;
22
+ }
23
+ declare function addDependency<T extends PackageJsonDeps, U extends PackageJsonDeps>(packageJson: T, scopedPackageJson: U, keys: {
24
+ devDependencies?: (keyof U["dependencies"] | keyof U["devDependencies"])[];
25
+ dependencies?: (keyof U["dependencies"] | keyof U["devDependencies"])[];
26
+ }): T;
27
+
28
+ export { MaybeContentGetter, PackageJsonDeps, VikeMeta, addDependency, features, flags, loadAsJson, loadAsMagicast, loadRelativeFileAsMagicast, transformAndGenerate };
package/dist/index.js ADDED
@@ -0,0 +1,180 @@
1
+ // src/features.ts
2
+ var features = [
3
+ "framework:solid",
4
+ // "framework:react", "framework:vue",
5
+ // "db:edgedb", "db:prisma",
6
+ "auth:authjs",
7
+ // "error:logrocket", "error:sentry",
8
+ "rpc:telefunc",
9
+ // "rpc:trpc",
10
+ "server:hattip",
11
+ "server:express"
12
+ // "clientRouting", "prerendering",
13
+ ];
14
+ var flags = new Map(features.map((f) => [f.split(":").at(-1), f]));
15
+
16
+ // src/parse.ts
17
+ import { namedTypes, visit } from "ast-types";
18
+ import { generateCode } from "magicast";
19
+
20
+ // src/cleanup.ts
21
+ import { ESLint } from "eslint";
22
+ var eslint = new ESLint({
23
+ useEslintrc: false,
24
+ fix: true,
25
+ overrideConfig: {
26
+ parser: "@typescript-eslint/parser",
27
+ plugins: ["@typescript-eslint", "unused-imports"],
28
+ rules: {
29
+ "unused-imports/no-unused-imports": "error"
30
+ }
31
+ }
32
+ });
33
+
34
+ // src/parse.ts
35
+ function evalCondition(code, meta = {}) {
36
+ code = code.replaceAll("import.meta", "VIKE_META");
37
+ code = `var VIKE_META = ${JSON.stringify(meta)};(${code})`;
38
+ return (0, eval)(code);
39
+ }
40
+ function transformAst(tree, meta) {
41
+ visit(tree, {
42
+ visitIdentifier(path) {
43
+ if (path.value.name === "VIKE_REMOVE") {
44
+ if (!path.parent) {
45
+ throw new Error("VIKE_REMOVE cannot appear at top level");
46
+ }
47
+ if (!namedTypes.ArrayExpression.check(path.parent?.parent?.value)) {
48
+ throw new Error("VIKE_REMOVE can only be an array element for now");
49
+ }
50
+ path.parent.prune();
51
+ this.traverse(path);
52
+ return;
53
+ }
54
+ this.traverse(path);
55
+ },
56
+ visitConditionalExpression(path) {
57
+ this.visitIfStatement(path);
58
+ },
59
+ visitIfStatement(path) {
60
+ let found = false;
61
+ this.traverse(path.get("test"), {
62
+ visitMemberExpression(path2) {
63
+ if (generateCode(path2.value).code.startsWith("import.meta.VIKE_")) {
64
+ found = true;
65
+ }
66
+ this.traverse(path2);
67
+ }
68
+ });
69
+ this.traverse(path);
70
+ if (!found)
71
+ return;
72
+ if (!evalCondition(generateCode(path.value.test).code, meta)) {
73
+ if (path.value.alternate) {
74
+ if (namedTypes.BlockStatement.check(path.value.alternate)) {
75
+ path.replace(...path.value.alternate.body);
76
+ } else {
77
+ path.replace(path.value.alternate);
78
+ }
79
+ } else {
80
+ path.prune();
81
+ }
82
+ } else {
83
+ if (namedTypes.BlockStatement.check(path.value.consequent)) {
84
+ path.replace(...path.value.consequent.body);
85
+ } else {
86
+ path.replace(path.value.consequent);
87
+ }
88
+ }
89
+ }
90
+ });
91
+ return tree;
92
+ }
93
+ async function transformAndGenerate(tree, meta) {
94
+ const ast = transformAst(tree, meta);
95
+ const code = generateCode(ast).code;
96
+ const linted = await eslint.lintText(code);
97
+ return linted[0].output ?? code;
98
+ }
99
+
100
+ // src/loaders.ts
101
+ import { loadFile, parseModule } from "magicast";
102
+ import { fileURLToPath } from "url";
103
+ import { dirname, join } from "path";
104
+
105
+ // src/assert.ts
106
+ function assert(condition, message) {
107
+ if (condition) {
108
+ return;
109
+ }
110
+ throw new Error(message);
111
+ }
112
+
113
+ // src/loaders.ts
114
+ async function loadAsJson(getter) {
115
+ const content = await getter?.();
116
+ assert(typeof content === "string", "Unable to load previous JSON module");
117
+ return JSON.parse(content);
118
+ }
119
+ async function loadAsMagicast(getter) {
120
+ const content = await getter?.();
121
+ assert(typeof content === "string", "Unable to load previous module");
122
+ return parseModule(content);
123
+ }
124
+ async function loadRelativeFileAsMagicast(relativePath, meta) {
125
+ const __filename = fileURLToPath(meta.url);
126
+ const __dirname = dirname(__filename);
127
+ return loadFile(join(__dirname, relativePath));
128
+ }
129
+
130
+ // src/magicast.ts
131
+ import { loadFile as loadFile2, parseModule as parseModule2, generateCode as generateCode2 } from "magicast";
132
+ import { addVitePlugin } from "magicast/helpers";
133
+
134
+ // src/utils/package.ts
135
+ function* deps(obj) {
136
+ if (obj.devDependencies) {
137
+ for (const key in obj.devDependencies) {
138
+ yield [key, obj.devDependencies[key]];
139
+ }
140
+ }
141
+ if (obj.dependencies) {
142
+ for (const key in obj.dependencies) {
143
+ yield [key, obj.dependencies[key]];
144
+ }
145
+ }
146
+ }
147
+ function* findKey(depsMap, keys) {
148
+ for (const key of keys) {
149
+ const value = depsMap.get(key);
150
+ if (!value) {
151
+ throw new Error(`key '${value}' not found in package.json`);
152
+ }
153
+ yield [key, value];
154
+ }
155
+ }
156
+ function addDependency(packageJson, scopedPackageJson, keys) {
157
+ packageJson.devDependencies ?? (packageJson.devDependencies = {});
158
+ packageJson.dependencies ?? (packageJson.dependencies = {});
159
+ const depsMap = new Map(deps(scopedPackageJson));
160
+ for (const [key, value] of findKey(depsMap, keys.devDependencies ?? [])) {
161
+ packageJson.devDependencies[key] = value;
162
+ }
163
+ for (const [key, value] of findKey(depsMap, keys.dependencies ?? [])) {
164
+ packageJson.dependencies[key] = value;
165
+ }
166
+ return packageJson;
167
+ }
168
+ export {
169
+ addDependency,
170
+ addVitePlugin,
171
+ features,
172
+ flags,
173
+ generateCode2 as generateCode,
174
+ loadAsJson,
175
+ loadAsMagicast,
176
+ loadFile2 as loadFile,
177
+ loadRelativeFileAsMagicast,
178
+ parseModule2 as parseModule,
179
+ transformAndGenerate
180
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@batijs/core",
3
+ "version": "0.0.3",
4
+ "description": "",
5
+ "type": "module",
6
+ "types": "./dist/index.d.ts",
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "MIT",
10
+ "devDependencies": {
11
+ "@types/eslint": "^8.37.0",
12
+ "@types/node": "^16.18.24",
13
+ "tsx": "^3.12.6",
14
+ "typescript": "^5.0.4",
15
+ "uvu": "^0.5.6",
16
+ "@batijs/tsup": "0.0.3"
17
+ },
18
+ "dependencies": {
19
+ "@typescript-eslint/eslint-plugin": "^5.59.2",
20
+ "@typescript-eslint/parser": "^5.59.2",
21
+ "ast-types": "^0.14.2",
22
+ "eslint": "^8.39.0",
23
+ "eslint-plugin-unused-imports": "^2.0.0",
24
+ "magicast": "^0.2.4"
25
+ },
26
+ "exports": {
27
+ ".": "./dist/index.js"
28
+ },
29
+ "typesVersions": {
30
+ "*": {
31
+ ".": [
32
+ "./dist/index.d.ts"
33
+ ],
34
+ "types": [
35
+ "./global.d.ts"
36
+ ]
37
+ }
38
+ },
39
+ "files": [
40
+ "dist/"
41
+ ],
42
+ "scripts": {
43
+ "test": "NODE_OPTIONS='--loader tsx' uvu",
44
+ "build": "tsup"
45
+ }
46
+ }