@silgi/module-builder 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Silgi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,351 @@
1
+ import { promises, existsSync, writeFileSync } from 'node:fs';
2
+ import { writeFile, mkdir } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { fileURLToPath, pathToFileURL } from 'node:url';
5
+ import { defineCommand } from 'citty';
6
+ import { consola } from 'consola';
7
+ import defu from 'defu';
8
+ import { createJiti } from 'dev-jiti';
9
+ import { createRegExp, anyOf } from 'magic-regexp';
10
+ import { resolvePath, findExports } from 'mlly';
11
+ import { resolve, basename, normalize, dirname } from 'pathe';
12
+ import { filename } from 'pathe/utils';
13
+ import { readPackageJSON } from 'pkg-types';
14
+ import { pkgDir } from 'silgi/runtime/meta';
15
+ import { parse } from 'tsconfck';
16
+ import { convertCompilerOptionsFromJson } from 'typescript';
17
+ import { scanExports } from 'unimport';
18
+ import { v as version, n as name } from '../shared/silgi-module-builder.NYdklG1f.mjs';
19
+
20
+ const srcDir = fileURLToPath(new URL("src", import.meta.url));
21
+ const subpaths = [
22
+ "runtime"
23
+ ];
24
+ const build = defineCommand({
25
+ meta: {
26
+ name: "build",
27
+ description: "Build module for distribution"
28
+ },
29
+ args: {
30
+ cwd: {
31
+ type: "string",
32
+ description: "Current working directory"
33
+ },
34
+ rootDir: {
35
+ type: "positional",
36
+ description: "Root directory",
37
+ required: false
38
+ },
39
+ outDir: {
40
+ type: "string"
41
+ },
42
+ sourcemap: {
43
+ type: "boolean"
44
+ },
45
+ stub: {
46
+ type: "boolean"
47
+ },
48
+ check: {
49
+ type: "boolean"
50
+ }
51
+ },
52
+ async run(context) {
53
+ const { build } = await import('unbuild');
54
+ const cwd = resolve(context.args.cwd || context.args.rootDir || ".");
55
+ let packageData = await readPackageJSON(cwd);
56
+ const jiti = createJiti(cwd);
57
+ const outDir = context.args.outDir || "dist";
58
+ function check() {
59
+ if (packageData) {
60
+ const expectedExports = {
61
+ ".": {
62
+ import: {
63
+ types: "./dist/module.d.ts",
64
+ default: "./dist/module.mjs"
65
+ }
66
+ },
67
+ "./module": {
68
+ types: "./dist/module.d.ts",
69
+ import: "./dist/module.mjs"
70
+ },
71
+ "./runtime": {
72
+ silgi: "./dist/runtime/index.ts",
73
+ silgiTypes: "./dist/runtime/index.ts",
74
+ types: "./runtime.d.ts",
75
+ import: "./dist/runtime/index.mjs"
76
+ },
77
+ "./runtime/*": {
78
+ silgi: "./dist/runtime/*.ts",
79
+ silgiTypes: "./dist/runtime/*.ts",
80
+ types: "./dist/runtime/*.d.ts",
81
+ import: "./dist/runtime/*.mjs"
82
+ }
83
+ };
84
+ packageData.exports = defu(packageData.exports, expectedExports);
85
+ packageData = defu(packageData, {
86
+ main: "./dist/module.mjs",
87
+ types: "./dist/module.d.ts",
88
+ scripts: {
89
+ "build": "pnpm silgi-module-build build",
90
+ "build:stub": "pnpm silgi-module-build build --stub",
91
+ "test": "vitest",
92
+ "test:coverage": "vitest run --coverage"
93
+ }
94
+ });
95
+ }
96
+ writeFileSync(resolve(cwd, "package.json"), JSON.stringify(packageData, null, 2));
97
+ }
98
+ if (context.args.check) {
99
+ check();
100
+ return;
101
+ }
102
+ const rootPkg = await readPackageJSON(join(pkgDir, "package.json"));
103
+ await build(cwd, false, {
104
+ declaration: true,
105
+ sourcemap: context.args.sourcemap,
106
+ stub: context.args.stub,
107
+ outDir,
108
+ // eslint-disable-next-line ts/ban-ts-comment
109
+ // @ts-ignore
110
+ ignoreConditions: ["silgi", "silgiTypes"],
111
+ entries: [
112
+ "src/module",
113
+ {
114
+ input: "src/runtime/",
115
+ outDir: `${outDir}/runtime`,
116
+ builder: "mkdist",
117
+ format: "esm",
118
+ // addRelativeDeclarationExtensions: true,
119
+ // ext: 'js',
120
+ pattern: [
121
+ "**",
122
+ "!**/*.stories.{js,cts,mts,ts,jsx,tsx}",
123
+ // ignore storybook files
124
+ "!**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}"
125
+ // ignore tests
126
+ ]
127
+ // esbuild: {
128
+ // jsxImportSource: 'vue',
129
+ // jsx: 'automatic',
130
+ // jsxFactory: 'h',
131
+ // },
132
+ }
133
+ ],
134
+ rollup: {
135
+ esbuild: {
136
+ target: "esnext"
137
+ },
138
+ emitCJS: false,
139
+ cjsBridge: false
140
+ },
141
+ externals: [
142
+ /dist[\\/]runtime[\\/]/,
143
+ "silgi",
144
+ "silgi/kit",
145
+ "silgi/runtime",
146
+ "silgi/module",
147
+ ...Object.keys(packageData.dependencies || {}),
148
+ ...Object.keys(packageData.peerDependencies || {}),
149
+ ...Object.keys(rootPkg.dependencies || {}),
150
+ ...Object.keys(rootPkg.peerDependencies || {})
151
+ ],
152
+ hooks: {
153
+ "mkdist:entry:options": async function(_ctx, entry, options) {
154
+ options.typescript = defu(options.typescript, {
155
+ compilerOptions: await loadTSCompilerOptions(entry.input)
156
+ });
157
+ },
158
+ "rollup:options": async function(ctx, options) {
159
+ const [entry] = ctx.buildEntries;
160
+ const mergedCompilerOptions = defu({
161
+ noEmit: false,
162
+ paths: {
163
+ "#app/silgi": ["./node_modules/silgi/dist/app/silgi"]
164
+ }
165
+ }, ctx.options.rollup.dts.compilerOptions, await loadTSCompilerOptions(entry.path));
166
+ ctx.options.rollup.dts.compilerOptions = convertCompilerOptionsFromJson(mergedCompilerOptions, entry.path).options;
167
+ options.plugins ||= [];
168
+ if (!Array.isArray(options.plugins))
169
+ options.plugins = [options.plugins];
170
+ const runtimeEntries = ctx.options.entries.filter((entry2) => entry2.builder === "mkdist");
171
+ const runtimeDirs = runtimeEntries.map((entry2) => basename(entry2.input));
172
+ const RUNTIME_RE = createRegExp(anyOf(...runtimeDirs).and(anyOf("/", "\\")));
173
+ options.plugins.unshift({
174
+ name: "silgi-module-builder:runtime-externals",
175
+ async resolveId(id, importer) {
176
+ if (!RUNTIME_RE.test(id))
177
+ return;
178
+ const resolved = await this.resolve(id, importer, { skipSelf: true });
179
+ if (!resolved)
180
+ return;
181
+ const normalizedId = normalize(resolved.id);
182
+ for (const entry2 of runtimeEntries) {
183
+ if (!normalizedId.includes(entry2.input))
184
+ continue;
185
+ const distFile = await resolvePath(join(dirname(pathToFileURL(normalizedId).href.replace(entry2.input, entry2.outDir)), filename(normalizedId)));
186
+ if (distFile) {
187
+ return {
188
+ external: true,
189
+ id: distFile
190
+ };
191
+ }
192
+ }
193
+ }
194
+ });
195
+ },
196
+ "rollup:build": async function(ctx) {
197
+ const runtimeDir = resolve(ctx.options.rootDir, "src", "runtime");
198
+ const runtimeIndexPath = resolve(runtimeDir, "index.ts");
199
+ if (!existsSync(runtimeDir)) {
200
+ await mkdir(runtimeDir, { recursive: true });
201
+ }
202
+ if (!existsSync(runtimeIndexPath)) {
203
+ await writeFile(runtimeIndexPath, "export default {}");
204
+ }
205
+ },
206
+ "rollup:done": async function(ctx) {
207
+ const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
208
+ const moduleFn = await jiti.import(pathToFileURL(moduleEntryPath).toString(), { default: true }).catch((err) => {
209
+ consola.error(err);
210
+ consola.error("Cannot load module. Please check dist:", moduleEntryPath);
211
+ return null;
212
+ });
213
+ if (!moduleFn) {
214
+ return;
215
+ }
216
+ const moduleMeta = await moduleFn.getMeta?.() || {};
217
+ const moduleRawPath = resolve(ctx.options.rootDir, "src/module.ts");
218
+ const _exports = await scanExports(moduleRawPath, true);
219
+ const cleanedExports = _exports.map(({ from, ...rest }) => rest);
220
+ moduleMeta.exports = cleanedExports;
221
+ if (ctx.pkg) {
222
+ if (!moduleMeta.name) {
223
+ moduleMeta.name = ctx.pkg.name;
224
+ }
225
+ if (!moduleMeta.version) {
226
+ moduleMeta.version = ctx.pkg.version;
227
+ }
228
+ }
229
+ moduleMeta._packageName = ctx.pkg.name;
230
+ if (ctx.pkg.modules) {
231
+ moduleMeta._modules = ctx.pkg.configmodules;
232
+ }
233
+ moduleMeta.builder = {
234
+ [name]: version,
235
+ unbuild: await readPackageJSON("unbuild").then((r) => r.version).catch(() => "unknown")
236
+ };
237
+ const metaFile = resolve(ctx.options.outDir, "module.json");
238
+ await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
239
+ await writeTypes(ctx.options, ctx.options.stub, moduleMeta);
240
+ },
241
+ "build:prepare": async function(_ctx) {
242
+ for (const subpath of subpaths) {
243
+ await writeFile(
244
+ `./${subpath}.d.ts`,
245
+ `export * from './dist/${subpath}/index';`
246
+ );
247
+ }
248
+ }
249
+ },
250
+ stubOptions: {
251
+ jiti: {
252
+ alias: {
253
+ [`${packageData.name}/runtime/*`]: resolve(srcDir, "../runtime/*")
254
+ }
255
+ }
256
+ }
257
+ });
258
+ }
259
+ });
260
+ async function writeTypes(options, isStub, moduleMeta) {
261
+ const dtsFile = resolve(options.outDir, "types.d.ts");
262
+ if (existsSync(dtsFile)) {
263
+ return;
264
+ }
265
+ const moduleReExports = [];
266
+ if (!isStub) {
267
+ const moduleTypesFile = resolve(options.outDir, "module.d.mts");
268
+ const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
269
+ const normalisedModuleTypes = moduleTypes.replace(/export\s*\{.*?\}/gs, (match) => match.replace(/\b(type|interface)\b/g, ""));
270
+ for (const e of findExports(normalisedModuleTypes)) {
271
+ moduleReExports.push(e);
272
+ }
273
+ }
274
+ const schemaShims = [];
275
+ const moduleImports = [];
276
+ const schemaImports = [];
277
+ const moduleExports = [];
278
+ const hasTypeExport = (name2) => isStub || moduleReExports.find((exp) => exp.names?.includes(name2));
279
+ if (!hasTypeExport("ModuleOptions")) {
280
+ schemaImports.push("SilgiModule");
281
+ moduleImports.push("default as Module");
282
+ moduleExports.push(`export type ModuleOptions = typeof Module extends SilgiModule<infer O> ? Partial<O> : Record<string, any>`);
283
+ }
284
+ if (hasTypeExport("ModuleOptions")) {
285
+ moduleImports.push("ModuleOptions");
286
+ schemaShims.push(` interface SilgiModuleOptions { ${moduleMeta.configKey}: ModuleOptions }`);
287
+ }
288
+ if (hasTypeExport("ModuleRuntimeOptions")) {
289
+ moduleImports.push("ModuleRuntimeOptions");
290
+ schemaShims.push(` interface SilgiRuntimeOptions { ${moduleMeta.configKey}: ModuleRuntimeOptions }`);
291
+ }
292
+ if (hasTypeExport("ModuleShareds")) {
293
+ moduleImports.push("ModuleShareds");
294
+ schemaShims.push(" interface SilgiShared extends ModuleShareds {}");
295
+ }
296
+ if (hasTypeExport("ModuleEvents")) {
297
+ moduleImports.push("ModuleEvents");
298
+ schemaShims.push(" interface SilgiEvent extends ModuleEvents {}");
299
+ }
300
+ if (hasTypeExport("ModuleRuntimeContexts")) {
301
+ moduleImports.push("ModuleRuntimeContexts");
302
+ schemaShims.push(" interface SilgiRuntimeContext extends ModuleRuntimeContexts {}");
303
+ }
304
+ if (hasTypeExport("ModuleHooks")) {
305
+ moduleImports.push("ModuleHooks");
306
+ schemaShims.push(" interface SilgiHooks extends ModuleHooks {}");
307
+ }
308
+ if (hasTypeExport("ModuleRuntimeHooks")) {
309
+ moduleImports.push("ModuleRuntimeHooks");
310
+ schemaShims.push(" interface SilgiRuntimeHooks extends ModuleRuntimeHooks {}");
311
+ }
312
+ if (hasTypeExport("ModuleRuntimeActions")) {
313
+ moduleImports.push("ModuleRuntimeActions");
314
+ schemaShims.push(` interface SilgiRuntimeActions { ${moduleMeta.configKey}: ModuleRuntimeActions }`);
315
+ }
316
+ const dtsContents = `${`
317
+ ${schemaImports.length ? `import type { ${schemaImports.join(", ")} } from 'silgi/types'` : ""}
318
+
319
+ ${moduleImports.length ? `import type { ${moduleImports.join(", ")} } from './module.mjs'` : ""}
320
+
321
+ ${schemaShims.length ? `declare module 'silgi/types' {
322
+ ${schemaShims.join("\n")}
323
+ }
324
+ ` : ""}
325
+ ${moduleExports.length ? `
326
+ ${moduleExports.join("\n")}` : ""}
327
+ ${isStub ? 'export * from "./module.mjs"' : ""}
328
+ ${moduleReExports[0] ? `
329
+ export { ${moduleReExports[0].names.map((n) => (n === "default" ? "" : "type ") + n).join(", ")} } from './module.mjs'` : ""}
330
+ `.trim().replace(/[\n\r]{3,}/g, "\n\n")}
331
+ `;
332
+ await promises.writeFile(dtsFile, dtsContents, "utf8");
333
+ await promises.writeFile(resolve(options.rootDir, "types.d.ts"), `export * from './dist/types';
334
+ `, "utf8");
335
+ }
336
+ async function loadTSCompilerOptions(path) {
337
+ const config = await parse(path);
338
+ const resolvedCompilerOptions = config?.tsconfig.compilerOptions || {};
339
+ for (const { tsconfig, tsconfigFile } of config.extended || []) {
340
+ for (const alias in tsconfig.compilerOptions?.paths || {}) {
341
+ resolvedCompilerOptions.paths[alias] = resolvedCompilerOptions.paths[alias].map((p) => {
342
+ if (!/^\.{1,2}(?:\/|$)/.test(p))
343
+ return p;
344
+ return resolve(dirname(tsconfigFile), p);
345
+ });
346
+ }
347
+ }
348
+ return resolvedCompilerOptions;
349
+ }
350
+
351
+ export { build as default, subpaths };
package/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+
package/dist/cli.mjs ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ import { defineCommand, runMain } from 'citty';
3
+ import { consola } from 'consola';
4
+ import { v as version, d as description, n as name } from './shared/silgi-module-builder.NYdklG1f.mjs';
5
+
6
+ const _rDefault = (r) => r && typeof r === "object" && "default" in r ? r.default : r;
7
+ const main = defineCommand({
8
+ meta: {
9
+ name,
10
+ description,
11
+ version
12
+ },
13
+ subCommands: {
14
+ // prepare: () => import('./commands/prepare').then(_rDefault),
15
+ build: () => import('./chunks/build.mjs').then(_rDefault)
16
+ },
17
+ setup(context) {
18
+ const firstArg = context.rawArgs[0];
19
+ if (context.cmd.subCommands && !(firstArg && firstArg in context.cmd.subCommands)) {
20
+ consola.warn("Please specify the `build` command explicitly. In a future version of `@silgi/module-builder`, the implicit default build command will be removed.");
21
+ context.rawArgs.unshift("build");
22
+ }
23
+ }
24
+ });
25
+ runMain(main);
@@ -0,0 +1,27 @@
1
+ import * as citty from 'citty';
2
+
3
+ declare const _default: citty.CommandDef<{
4
+ cwd: {
5
+ type: "string";
6
+ description: string;
7
+ };
8
+ rootDir: {
9
+ type: "positional";
10
+ description: string;
11
+ required: false;
12
+ };
13
+ outDir: {
14
+ type: "string";
15
+ };
16
+ sourcemap: {
17
+ type: "boolean";
18
+ };
19
+ stub: {
20
+ type: "boolean";
21
+ };
22
+ check: {
23
+ type: "boolean";
24
+ };
25
+ }>;
26
+
27
+ export { _default as build };
@@ -0,0 +1,27 @@
1
+ import * as citty from 'citty';
2
+
3
+ declare const _default: citty.CommandDef<{
4
+ cwd: {
5
+ type: "string";
6
+ description: string;
7
+ };
8
+ rootDir: {
9
+ type: "positional";
10
+ description: string;
11
+ required: false;
12
+ };
13
+ outDir: {
14
+ type: "string";
15
+ };
16
+ sourcemap: {
17
+ type: "boolean";
18
+ };
19
+ stub: {
20
+ type: "boolean";
21
+ };
22
+ check: {
23
+ type: "boolean";
24
+ };
25
+ }>;
26
+
27
+ export { _default as build };
package/dist/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ export { default as build } from './chunks/build.mjs';
2
+ import 'node:fs';
3
+ import 'node:fs/promises';
4
+ import 'node:path';
5
+ import 'node:url';
6
+ import 'citty';
7
+ import 'consola';
8
+ import 'defu';
9
+ import 'dev-jiti';
10
+ import 'magic-regexp';
11
+ import 'mlly';
12
+ import 'pathe';
13
+ import 'pathe/utils';
14
+ import 'pkg-types';
15
+ import 'silgi/runtime/meta';
16
+ import 'tsconfck';
17
+ import 'typescript';
18
+ import 'unimport';
19
+ import './shared/silgi-module-builder.NYdklG1f.mjs';
@@ -0,0 +1,5 @@
1
+ const name = "@silgi/module-builder";
2
+ const version = "0.0.1";
3
+ const description = "Complete solution for building Silgi modules";
4
+
5
+ export { description as d, name as n, version as v };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@silgi/module-builder",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "private": false,
6
+ "description": "Complete solution for building Silgi modules",
7
+ "exports": {
8
+ ".": "./dist/index.mjs",
9
+ "./package.json": "./package.json"
10
+ },
11
+ "bin": {
12
+ "silgi-module-build": "./dist/cli.mjs"
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "peerDependencies": {
18
+ "silgi": "^0.7.6",
19
+ "typescript": "^5.7.3"
20
+ },
21
+ "dependencies": {
22
+ "citty": "^0.1.6",
23
+ "consola": "^3.4.0",
24
+ "defu": "^6.1.4",
25
+ "dev-jiti": "^2.4.2",
26
+ "magic-regexp": "^0.8.0",
27
+ "mlly": "^1.7.4",
28
+ "pathe": "^2.0.2",
29
+ "pkg-types": "^1.3.1",
30
+ "silgi": "^0.7.6",
31
+ "tsconfck": "^3.1.5",
32
+ "unbuild": "^3.3.1",
33
+ "unimport": "^4.1.1"
34
+ },
35
+ "devDependencies": {
36
+ "@antfu/eslint-config": "^4.2.0",
37
+ "@types/node": "^22.13.1",
38
+ "eslint": "^9.20.1",
39
+ "typescript": "^5.7.3"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "scripts": {
45
+ "build": "unbuild",
46
+ "dev:prepare": "unbuild --stub && pnpm -r dev:prepare",
47
+ "build:stub": "unbuild --stub",
48
+ "release": "pnpm build && pnpm publish",
49
+ "lint": "eslint .",
50
+ "lint:fix": "eslint . --fix"
51
+ }
52
+ }