@silgi/module-builder 0.7.5 → 0.8.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/cli.d.mts +1 -1
- package/dist/cli.mjs +19 -19
- package/dist/commands/build.d.mts +28 -0
- package/dist/commands/build.mjs +148 -0
- package/dist/index.d.mts +2 -27
- package/dist/index.mjs +3 -19
- package/dist/package.mjs +7 -0
- package/package.json +5 -7
- package/dist/chunks/build.mjs +0 -350
- package/dist/shared/silgi-module-builder.C81SXUbd.mjs +0 -5
package/dist/cli.d.mts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
export { };
|
package/dist/cli.mjs
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import {
|
2
|
+
import { description, name, version } from "./package.mjs";
|
3
|
+
import { defineCommand, runMain } from "citty";
|
4
|
+
import { consola } from "consola";
|
5
5
|
|
6
|
+
//#region src/cli.ts
|
6
7
|
const _rDefault = (r) => r && typeof r === "object" && "default" in r ? r.default : r;
|
7
8
|
const main = defineCommand({
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
context.rawArgs.unshift("build");
|
22
|
-
}
|
23
|
-
}
|
9
|
+
meta: {
|
10
|
+
name,
|
11
|
+
description,
|
12
|
+
version
|
13
|
+
},
|
14
|
+
subCommands: { build: () => import("./commands/build.mjs").then(_rDefault) },
|
15
|
+
setup(context) {
|
16
|
+
const firstArg = context.rawArgs[0];
|
17
|
+
if (context.cmd.subCommands && !(firstArg && firstArg in context.cmd.subCommands)) {
|
18
|
+
consola.warn("Please specify the `build` command explicitly. In a future version of `@silgi/module-builder`, the implicit default build command will be removed.");
|
19
|
+
context.rawArgs.unshift("build");
|
20
|
+
}
|
21
|
+
}
|
24
22
|
});
|
25
23
|
runMain(main);
|
24
|
+
|
25
|
+
//#endregion
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import * as citty0 from "citty";
|
2
|
+
|
3
|
+
//#region src/commands/build.d.ts
|
4
|
+
declare const _default: citty0.CommandDef<{
|
5
|
+
cwd: {
|
6
|
+
type: "string";
|
7
|
+
description: string;
|
8
|
+
};
|
9
|
+
rootDir: {
|
10
|
+
type: "positional";
|
11
|
+
description: string;
|
12
|
+
required: false;
|
13
|
+
};
|
14
|
+
outDir: {
|
15
|
+
type: "string";
|
16
|
+
};
|
17
|
+
sourcemap: {
|
18
|
+
type: "boolean";
|
19
|
+
};
|
20
|
+
stub: {
|
21
|
+
type: "boolean";
|
22
|
+
};
|
23
|
+
check: {
|
24
|
+
type: "boolean";
|
25
|
+
};
|
26
|
+
}>;
|
27
|
+
//#endregion
|
28
|
+
export { _default };
|
@@ -0,0 +1,148 @@
|
|
1
|
+
import { name, version } from "../package.mjs";
|
2
|
+
import { defineCommand } from "citty";
|
3
|
+
import { consola } from "consola";
|
4
|
+
import { existsSync, promises } from "node:fs";
|
5
|
+
import { join } from "node:path";
|
6
|
+
import { pathToFileURL } from "node:url";
|
7
|
+
import { createJiti } from "jiti";
|
8
|
+
import { findExports, findTypeExports } from "mlly";
|
9
|
+
import { resolve } from "pathe";
|
10
|
+
import { readPackageJSON } from "pkg-types";
|
11
|
+
import { pkgDir } from "silgi/runtime/meta";
|
12
|
+
import { build } from "tsdown";
|
13
|
+
import { scanExports } from "unimport";
|
14
|
+
|
15
|
+
//#region src/commands/build.ts
|
16
|
+
const subpaths = ["runtime", "types"];
|
17
|
+
var build_default = defineCommand({
|
18
|
+
meta: {
|
19
|
+
name: "build",
|
20
|
+
description: "Build module for distribution"
|
21
|
+
},
|
22
|
+
args: {
|
23
|
+
cwd: {
|
24
|
+
type: "string",
|
25
|
+
description: "Current working directory"
|
26
|
+
},
|
27
|
+
rootDir: {
|
28
|
+
type: "positional",
|
29
|
+
description: "Root directory",
|
30
|
+
required: false
|
31
|
+
},
|
32
|
+
outDir: { type: "string" },
|
33
|
+
sourcemap: { type: "boolean" },
|
34
|
+
stub: { type: "boolean" },
|
35
|
+
check: { type: "boolean" }
|
36
|
+
},
|
37
|
+
async run(context) {
|
38
|
+
const cwd = resolve(context.args.cwd || context.args.rootDir || ".");
|
39
|
+
const packageData = await readPackageJSON(cwd);
|
40
|
+
const jiti = createJiti(cwd, {
|
41
|
+
fsCache: false,
|
42
|
+
moduleCache: false,
|
43
|
+
alias: {
|
44
|
+
[`${packageData.name}/runtime/*`]: resolve(cwd, "src/runtime/*"),
|
45
|
+
...Object.fromEntries(subpaths.map((subpath) => [`${packageData.name}/${subpath}`, resolve(cwd, `src/${subpath}/index.ts`)]))
|
46
|
+
}
|
47
|
+
});
|
48
|
+
const outDir = context.args.outDir || "dist";
|
49
|
+
const rootPkg = await readPackageJSON(join(pkgDir, "package.json"));
|
50
|
+
await build({
|
51
|
+
outDir,
|
52
|
+
entry: [
|
53
|
+
"src/module.ts",
|
54
|
+
"src/runtime",
|
55
|
+
"src/types",
|
56
|
+
"src/data"
|
57
|
+
],
|
58
|
+
format: "esm",
|
59
|
+
treeshake: true,
|
60
|
+
fixedExtension: true,
|
61
|
+
target: "esnext",
|
62
|
+
dts: true,
|
63
|
+
unbundle: true,
|
64
|
+
external: [
|
65
|
+
/dist[\\/]runtime[\\/]/,
|
66
|
+
"silgi",
|
67
|
+
"silgi/kit",
|
68
|
+
"silgi/runtime",
|
69
|
+
"silgi/module",
|
70
|
+
...subpaths.map((subpath) => `${packageData.name}/${subpath}`),
|
71
|
+
...Object.keys(packageData.dependencies || {}),
|
72
|
+
...Object.keys(packageData.peerDependencies || {}),
|
73
|
+
...Object.keys(rootPkg.dependencies || {}),
|
74
|
+
...Object.keys(rootPkg.peerDependencies || {})
|
75
|
+
],
|
76
|
+
hooks: { "build:done": async function(ctx) {
|
77
|
+
const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
|
78
|
+
const moduleFn = await jiti.import(pathToFileURL(moduleEntryPath).toString(), { default: true }).catch((err) => {
|
79
|
+
consola.error(err);
|
80
|
+
consola.error("Cannot load module. Please check dist:", moduleEntryPath);
|
81
|
+
return null;
|
82
|
+
});
|
83
|
+
if (!moduleFn) return;
|
84
|
+
const moduleMeta = await moduleFn.getMeta?.() || {};
|
85
|
+
const moduleRawPath = resolve(ctx.options.cwd, "src/module.ts");
|
86
|
+
const _exports = await scanExports(moduleRawPath, true);
|
87
|
+
const cleanedExports = _exports.map(({ from,...rest }) => rest);
|
88
|
+
moduleMeta.exports = cleanedExports;
|
89
|
+
if (ctx.pkg) {
|
90
|
+
if (!moduleMeta.name) moduleMeta.name = ctx.pkg.name;
|
91
|
+
if (!moduleMeta.version) moduleMeta.version = ctx.pkg.version;
|
92
|
+
}
|
93
|
+
moduleMeta._packageName = ctx.pkg?.name;
|
94
|
+
if (ctx.pkg?.modules) moduleMeta._modules = ctx.pkg?.configmodules;
|
95
|
+
moduleMeta.builder = {
|
96
|
+
[name]: version,
|
97
|
+
unbuild: await readPackageJSON("unbuild").then((r) => r.version).catch(() => "unknown")
|
98
|
+
};
|
99
|
+
const metaFile = resolve(ctx.options.outDir, "module.json");
|
100
|
+
await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
|
101
|
+
await writeTypes(ctx, moduleMeta);
|
102
|
+
} }
|
103
|
+
});
|
104
|
+
}
|
105
|
+
});
|
106
|
+
async function writeTypes(ctx, _moduleMeta) {
|
107
|
+
const dtsFile = resolve(ctx.options.cwd, "types/index.d.mts");
|
108
|
+
const moduleReExports = [];
|
109
|
+
const moduleTypesFile = resolve(ctx.options.cwd, "src/types/index.ts");
|
110
|
+
const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
|
111
|
+
const normalisedModuleTypes = moduleTypes.replace(/export\s*\{.*?\}/gs, (match) => match.replace(/\b(type|interface)\b/g, ""));
|
112
|
+
for (const e of findExports(normalisedModuleTypes)) moduleReExports.push(e);
|
113
|
+
for (const i of findTypeExports(normalisedModuleTypes)) moduleReExports.push(i);
|
114
|
+
const moduleImports = [];
|
115
|
+
const hasTypeExport = (name$1) => moduleReExports.find((exp) => exp.names?.includes(name$1));
|
116
|
+
if (hasTypeExport("ModuleOptions")) moduleImports.push("ModuleOptions");
|
117
|
+
if (hasTypeExport("ModuleRuntimeOptions")) moduleImports.push("ModuleRuntimeOptions");
|
118
|
+
if (hasTypeExport("ModuleRuntimeShareds")) moduleImports.push("ModuleRuntimeShareds");
|
119
|
+
if (hasTypeExport("ModuleEvents")) moduleImports.push("ModuleEvents");
|
120
|
+
if (hasTypeExport("ModuleRuntimeContexts")) moduleImports.push("ModuleRuntimeContexts");
|
121
|
+
if (hasTypeExport("ModuleHooks")) moduleImports.push("ModuleHooks");
|
122
|
+
if (hasTypeExport("ModuleRuntimeHooks")) moduleImports.push("ModuleRuntimeHooks");
|
123
|
+
if (hasTypeExport("SetupModuleOption")) moduleImports.push("SetupModuleOption");
|
124
|
+
if (hasTypeExport("ModuleRuntimeMethods")) moduleImports.push("ModuleRuntimeMethods");
|
125
|
+
if (hasTypeExport("RouteRules")) moduleImports.push("RouteRules");
|
126
|
+
if (hasTypeExport("MetaData")) moduleImports.push("MetaData");
|
127
|
+
let fromPath;
|
128
|
+
let prevContent;
|
129
|
+
if (existsSync(dtsFile)) {
|
130
|
+
prevContent = await promises.readFile(dtsFile, "utf8").catch(() => "");
|
131
|
+
const match = prevContent.match(/export\s+\*\s+from\s+["'](.+)["']/);
|
132
|
+
if (match) fromPath = match[1];
|
133
|
+
}
|
134
|
+
let dtsContents;
|
135
|
+
const importFrom = fromPath || "./module.mjs";
|
136
|
+
dtsContents = `${`${moduleImports.length ? `import type { ${moduleImports.join(", ")} } from '${importFrom}'\n\nexport type { ${moduleImports.join(", ")} }` : ""}`.trim()}\n`;
|
137
|
+
let mergedContent = dtsContents;
|
138
|
+
if (prevContent) {
|
139
|
+
const prevLines = prevContent.split("\n").map((l) => l.trim());
|
140
|
+
const newLines = dtsContents.split("\n").map((l) => l.trim());
|
141
|
+
const uniqueLines = newLines.filter((line) => line && !prevLines.includes(line));
|
142
|
+
mergedContent = `${uniqueLines.join("\n")}\n${prevContent}`;
|
143
|
+
}
|
144
|
+
await promises.writeFile(dtsFile, mergedContent, "utf8");
|
145
|
+
}
|
146
|
+
|
147
|
+
//#endregion
|
148
|
+
export { build_default as default, subpaths };
|
package/dist/index.d.mts
CHANGED
@@ -1,27 +1,2 @@
|
|
1
|
-
import
|
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 };
|
1
|
+
import { _default } from "./commands/build.mjs";
|
2
|
+
export { _default as build };
|
package/dist/index.mjs
CHANGED
@@ -1,19 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
import 'node:path';
|
5
|
-
import 'node:url';
|
6
|
-
import 'citty';
|
7
|
-
import 'consola';
|
8
|
-
import 'defu';
|
9
|
-
import '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.C81SXUbd.mjs';
|
1
|
+
import build_default from "./commands/build.mjs";
|
2
|
+
|
3
|
+
export { build_default as build };
|
package/dist/package.mjs
ADDED
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@silgi/module-builder",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.8.0",
|
5
5
|
"private": false,
|
6
6
|
"description": "Complete solution for building Silgi modules",
|
7
7
|
"exports": {
|
@@ -19,13 +19,11 @@
|
|
19
19
|
"consola": "^3.4.2",
|
20
20
|
"defu": "^6.1.4",
|
21
21
|
"jiti": "^2.4.2",
|
22
|
-
"magic-regexp": "^0.10.0",
|
23
22
|
"mlly": "^1.7.4",
|
24
23
|
"pathe": "^2.0.3",
|
25
24
|
"pkg-types": "^2.1.0",
|
26
25
|
"silgi": "^0.41.40",
|
27
|
-
"
|
28
|
-
"unbuild": "^3.5.0",
|
26
|
+
"tsdown": "^0.12.4",
|
29
27
|
"unimport": "^5.0.1"
|
30
28
|
},
|
31
29
|
"devDependencies": {
|
@@ -41,9 +39,9 @@
|
|
41
39
|
"@silgi/module-builder": "link:."
|
42
40
|
},
|
43
41
|
"scripts": {
|
44
|
-
"build": "
|
45
|
-
"dev:prepare": "
|
46
|
-
"build:
|
42
|
+
"build": "tsdown",
|
43
|
+
"dev:prepare": "tsdown --watch && pnpm -r dev:prepare",
|
44
|
+
"build:watch": "tsdown --watch",
|
47
45
|
"release": "pnpm build && pnpm publish",
|
48
46
|
"lint": "eslint .",
|
49
47
|
"lint:fix": "eslint . --fix"
|
package/dist/chunks/build.mjs
DELETED
@@ -1,350 +0,0 @@
|
|
1
|
-
import { existsSync, promises } from 'node:fs';
|
2
|
-
import { mkdir, writeFile } from 'node:fs/promises';
|
3
|
-
import { join } from 'node:path';
|
4
|
-
import { pathToFileURL } from 'node:url';
|
5
|
-
import { defineCommand } from 'citty';
|
6
|
-
import { consola } from 'consola';
|
7
|
-
import defu from 'defu';
|
8
|
-
import { createJiti } from 'jiti';
|
9
|
-
import { createRegExp, anyOf } from 'magic-regexp';
|
10
|
-
import { resolvePath, findExports, findTypeExports } 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.C81SXUbd.mjs';
|
19
|
-
|
20
|
-
const subpaths = [
|
21
|
-
"runtime",
|
22
|
-
"types"
|
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
|
-
const packageData = await readPackageJSON(cwd);
|
56
|
-
const jiti = createJiti(cwd, {
|
57
|
-
fsCache: false,
|
58
|
-
moduleCache: false,
|
59
|
-
alias: {
|
60
|
-
[`${packageData.name}/runtime/*`]: resolve(cwd, "src/runtime/*"),
|
61
|
-
...Object.fromEntries(
|
62
|
-
subpaths.map((subpath) => [
|
63
|
-
`${packageData.name}/${subpath}`,
|
64
|
-
resolve(cwd, `src/${subpath}/index.ts`)
|
65
|
-
])
|
66
|
-
)
|
67
|
-
}
|
68
|
-
});
|
69
|
-
const outDir = context.args.outDir || "dist";
|
70
|
-
const rootPkg = await readPackageJSON(join(pkgDir, "package.json"));
|
71
|
-
const isDataFolder = existsSync(join(cwd, "src", "data"));
|
72
|
-
const isRuntimeFolder = existsSync(join(cwd, "src", "runtime"));
|
73
|
-
const isTypesFolder = existsSync(join(cwd, "src", "types"));
|
74
|
-
const entries = [];
|
75
|
-
if (isDataFolder) {
|
76
|
-
entries.push({
|
77
|
-
input: "src/data/",
|
78
|
-
outDir: `${outDir}/data`,
|
79
|
-
addRelativeDeclarationExtensions: true,
|
80
|
-
ext: "js",
|
81
|
-
pattern: [
|
82
|
-
"**",
|
83
|
-
"!**/*.stories.{js,cts,mts,ts,jsx,tsx}",
|
84
|
-
// ignore storybook files
|
85
|
-
"!**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}"
|
86
|
-
// ignore tests
|
87
|
-
]
|
88
|
-
});
|
89
|
-
}
|
90
|
-
if (isRuntimeFolder) {
|
91
|
-
entries.push({
|
92
|
-
input: "src/runtime/",
|
93
|
-
outDir: `${outDir}/runtime`,
|
94
|
-
addRelativeDeclarationExtensions: true,
|
95
|
-
ext: "js",
|
96
|
-
pattern: [
|
97
|
-
"**",
|
98
|
-
"!**/*.stories.{js,cts,mts,ts,jsx,tsx}",
|
99
|
-
// ignore storybook files
|
100
|
-
"!**/*.{spec,test}.{js,cts,mts,ts,jsx,tsx}"
|
101
|
-
// ignore tests
|
102
|
-
]
|
103
|
-
});
|
104
|
-
}
|
105
|
-
if (isTypesFolder) {
|
106
|
-
entries.push({
|
107
|
-
input: "src/types/index.ts",
|
108
|
-
outDir: `${outDir}`,
|
109
|
-
name: "types",
|
110
|
-
builder: "rollup"
|
111
|
-
});
|
112
|
-
}
|
113
|
-
await build(cwd, false, {
|
114
|
-
declaration: "node16",
|
115
|
-
sourcemap: context.args.sourcemap,
|
116
|
-
stub: context.args.stub,
|
117
|
-
outDir,
|
118
|
-
// eslint-disable-next-line ts/ban-ts-comment
|
119
|
-
// @ts-ignore
|
120
|
-
ignoreConditions: ["silgi", "silgiTypes", "pckSilgi", "pckSilgiTypes", ...packageData?.config?.ignoreConditions || []],
|
121
|
-
entries: [
|
122
|
-
"src/module",
|
123
|
-
...entries
|
124
|
-
],
|
125
|
-
rollup: {
|
126
|
-
esbuild: {
|
127
|
-
target: "esnext"
|
128
|
-
},
|
129
|
-
emitCJS: false,
|
130
|
-
cjsBridge: false
|
131
|
-
},
|
132
|
-
externals: [
|
133
|
-
/dist[\\/]runtime[\\/]/,
|
134
|
-
"silgi",
|
135
|
-
"silgi/kit",
|
136
|
-
"silgi/runtime",
|
137
|
-
"silgi/module",
|
138
|
-
...subpaths.map((subpath) => `${packageData.name}/${subpath}`),
|
139
|
-
...Object.keys(packageData.dependencies || {}),
|
140
|
-
...Object.keys(packageData.peerDependencies || {}),
|
141
|
-
...Object.keys(rootPkg.dependencies || {}),
|
142
|
-
...Object.keys(rootPkg.peerDependencies || {})
|
143
|
-
],
|
144
|
-
hooks: {
|
145
|
-
"mkdist:entry:options": async function(_ctx, entry, mkdistOptions) {
|
146
|
-
mkdistOptions.addRelativeDeclarationExtensions = true;
|
147
|
-
mkdistOptions.typescript = defu(mkdistOptions.typescript, {
|
148
|
-
compilerOptions: await loadTSCompilerOptions(entry.input)
|
149
|
-
});
|
150
|
-
},
|
151
|
-
"rollup:options": async function(ctx, options) {
|
152
|
-
const [entry] = ctx.buildEntries;
|
153
|
-
const mergedCompilerOptions = defu({
|
154
|
-
noEmit: false,
|
155
|
-
paths: {
|
156
|
-
"#app/silgi": ["./node_modules/silgi/dist/app/silgi"]
|
157
|
-
}
|
158
|
-
}, ctx.options.rollup.dts.compilerOptions, await loadTSCompilerOptions(entry.path));
|
159
|
-
ctx.options.rollup.dts.compilerOptions = convertCompilerOptionsFromJson(mergedCompilerOptions, entry.path).options;
|
160
|
-
options.plugins ||= [];
|
161
|
-
ctx.options.rollup.dts.compilerOptions.customConditions = [];
|
162
|
-
if (!Array.isArray(options.plugins))
|
163
|
-
options.plugins = [options.plugins];
|
164
|
-
const runtimeEntries = ctx.options.entries.filter((entry2) => entry2.builder === "mkdist");
|
165
|
-
const runtimeDirs = runtimeEntries.map((entry2) => basename(entry2.input));
|
166
|
-
const RUNTIME_RE = createRegExp(anyOf(...runtimeDirs).and(anyOf("/", "\\")));
|
167
|
-
options.plugins.unshift({
|
168
|
-
name: "silgi-module-builder:runtime-externals",
|
169
|
-
async resolveId(id, importer) {
|
170
|
-
if (!RUNTIME_RE.test(id))
|
171
|
-
return;
|
172
|
-
const resolved = await this.resolve(id, importer, { skipSelf: true });
|
173
|
-
if (!resolved)
|
174
|
-
return;
|
175
|
-
const normalizedId = normalize(resolved.id);
|
176
|
-
for (const entry2 of runtimeEntries) {
|
177
|
-
if (!normalizedId.includes(entry2.input))
|
178
|
-
continue;
|
179
|
-
const distFile = await resolvePath(join(dirname(pathToFileURL(normalizedId).href.replace(entry2.input, entry2.outDir)), filename(normalizedId)));
|
180
|
-
if (distFile) {
|
181
|
-
return {
|
182
|
-
external: true,
|
183
|
-
id: distFile
|
184
|
-
};
|
185
|
-
}
|
186
|
-
}
|
187
|
-
}
|
188
|
-
});
|
189
|
-
},
|
190
|
-
"rollup:build": async function(ctx) {
|
191
|
-
const runtimeDir = resolve(ctx.options.rootDir, "src", "runtime");
|
192
|
-
const runtimeIndexPath = resolve(runtimeDir, "index.ts");
|
193
|
-
if (!existsSync(runtimeDir)) {
|
194
|
-
await mkdir(runtimeDir, { recursive: true });
|
195
|
-
}
|
196
|
-
if (!existsSync(runtimeIndexPath)) {
|
197
|
-
await writeFile(runtimeIndexPath, "export default {}");
|
198
|
-
}
|
199
|
-
},
|
200
|
-
"rollup:done": async function(ctx) {
|
201
|
-
const moduleEntryPath = resolve(ctx.options.outDir, "module.mjs");
|
202
|
-
const moduleFn = await jiti.import(pathToFileURL(moduleEntryPath).toString(), { default: true }).catch((err) => {
|
203
|
-
consola.error(err);
|
204
|
-
consola.error("Cannot load module. Please check dist:", moduleEntryPath);
|
205
|
-
return null;
|
206
|
-
});
|
207
|
-
if (!moduleFn) {
|
208
|
-
return;
|
209
|
-
}
|
210
|
-
const moduleMeta = await moduleFn.getMeta?.() || {};
|
211
|
-
const moduleRawPath = resolve(ctx.options.rootDir, "src/module.ts");
|
212
|
-
const _exports = await scanExports(moduleRawPath, true);
|
213
|
-
const cleanedExports = _exports.map(({ from, ...rest }) => rest);
|
214
|
-
moduleMeta.exports = cleanedExports;
|
215
|
-
if (ctx.pkg) {
|
216
|
-
if (!moduleMeta.name) {
|
217
|
-
moduleMeta.name = ctx.pkg.name;
|
218
|
-
}
|
219
|
-
if (!moduleMeta.version) {
|
220
|
-
moduleMeta.version = ctx.pkg.version;
|
221
|
-
}
|
222
|
-
}
|
223
|
-
moduleMeta._packageName = ctx.pkg.name;
|
224
|
-
if (ctx.pkg.modules) {
|
225
|
-
moduleMeta._modules = ctx.pkg.configmodules;
|
226
|
-
}
|
227
|
-
moduleMeta.builder = {
|
228
|
-
[name]: version,
|
229
|
-
unbuild: await readPackageJSON("unbuild").then((r) => r.version).catch(() => "unknown")
|
230
|
-
};
|
231
|
-
const metaFile = resolve(ctx.options.outDir, "module.json");
|
232
|
-
await promises.writeFile(metaFile, JSON.stringify(moduleMeta, null, 2), "utf8");
|
233
|
-
await writeTypes(ctx.options);
|
234
|
-
},
|
235
|
-
"build:done": async function(ctx) {
|
236
|
-
const logs = [...ctx.warnings].filter((l) => l.startsWith("Potential missing package.json files:"));
|
237
|
-
if (logs.filter((l) => l.match(/\.d\.ts/)).length > 0) {
|
238
|
-
consola.warn(`@silgi/module-builder\` will no longer generate \`.d.ts\` declaration files. You can update these paths to use the \`.d.mts\` extension instead.`);
|
239
|
-
}
|
240
|
-
if (logs.filter((l) => l.match(/module\.cjs/)).length > 0) {
|
241
|
-
consola.warn(`@silgi/module-builder\` will no longer generate \`module.cjs\` as this is not required for Nuxt v3+. You can safely remove replace this with \`module.mjs\` in your \`package.json\`.`);
|
242
|
-
}
|
243
|
-
const pkg = await readPackageJSON(cwd);
|
244
|
-
if (pkg?.types && !existsSync(resolve(cwd, pkg.types))) {
|
245
|
-
consola.warn(`Please remove the \`types\` field from package.json as it is no longer required for Bundler TypeScript module resolution. Instead, you can use \`typesVersions\` to support subpath export types for Node10, if required.`);
|
246
|
-
}
|
247
|
-
}
|
248
|
-
},
|
249
|
-
stubOptions: {
|
250
|
-
jiti: {
|
251
|
-
alias: {
|
252
|
-
[`${packageData.name}/runtime/*`]: resolve(cwd, "src/runtime/*"),
|
253
|
-
...Object.fromEntries(
|
254
|
-
subpaths.map((subpath) => [
|
255
|
-
`${packageData.name}/${subpath}`,
|
256
|
-
resolve(cwd, `src/${subpath}/index.ts`)
|
257
|
-
])
|
258
|
-
)
|
259
|
-
}
|
260
|
-
}
|
261
|
-
},
|
262
|
-
failOnWarn: false
|
263
|
-
});
|
264
|
-
}
|
265
|
-
});
|
266
|
-
async function writeTypes(options, _moduleMeta) {
|
267
|
-
if (!options.stub) {
|
268
|
-
return;
|
269
|
-
}
|
270
|
-
const dtsFile = resolve(options.outDir, "types.d.mts");
|
271
|
-
const moduleReExports = [];
|
272
|
-
const moduleTypesFile = resolve(options.rootDir, "src/types/index.ts");
|
273
|
-
const moduleTypes = await promises.readFile(moduleTypesFile, "utf8").catch(() => "");
|
274
|
-
const normalisedModuleTypes = moduleTypes.replace(/export\s*\{.*?\}/gs, (match) => match.replace(/\b(type|interface)\b/g, ""));
|
275
|
-
for (const e of findExports(normalisedModuleTypes)) {
|
276
|
-
moduleReExports.push(e);
|
277
|
-
}
|
278
|
-
for (const i of findTypeExports(normalisedModuleTypes)) {
|
279
|
-
moduleReExports.push(i);
|
280
|
-
}
|
281
|
-
const moduleImports = [];
|
282
|
-
const hasTypeExport = (name2) => moduleReExports.find((exp) => exp.names?.includes(name2));
|
283
|
-
if (hasTypeExport("ModuleOptions")) {
|
284
|
-
moduleImports.push("ModuleOptions");
|
285
|
-
}
|
286
|
-
if (hasTypeExport("ModuleRuntimeOptions")) {
|
287
|
-
moduleImports.push("ModuleRuntimeOptions");
|
288
|
-
}
|
289
|
-
if (hasTypeExport("ModuleRuntimeShareds")) {
|
290
|
-
moduleImports.push("ModuleRuntimeShareds");
|
291
|
-
}
|
292
|
-
if (hasTypeExport("ModuleEvents")) {
|
293
|
-
moduleImports.push("ModuleEvents");
|
294
|
-
}
|
295
|
-
if (hasTypeExport("ModuleRuntimeContexts")) {
|
296
|
-
moduleImports.push("ModuleRuntimeContexts");
|
297
|
-
}
|
298
|
-
if (hasTypeExport("ModuleHooks")) {
|
299
|
-
moduleImports.push("ModuleHooks");
|
300
|
-
}
|
301
|
-
if (hasTypeExport("ModuleRuntimeHooks")) {
|
302
|
-
moduleImports.push("ModuleRuntimeHooks");
|
303
|
-
}
|
304
|
-
if (hasTypeExport("SetupModuleOption")) {
|
305
|
-
moduleImports.push("SetupModuleOption");
|
306
|
-
}
|
307
|
-
if (hasTypeExport("ModuleRuntimeMethods")) {
|
308
|
-
moduleImports.push("ModuleRuntimeMethods");
|
309
|
-
}
|
310
|
-
if (hasTypeExport("RouteRules")) {
|
311
|
-
moduleImports.push("RouteRules");
|
312
|
-
}
|
313
|
-
if (hasTypeExport("MetaData")) {
|
314
|
-
moduleImports.push("MetaData");
|
315
|
-
}
|
316
|
-
let fromPath;
|
317
|
-
if (existsSync(dtsFile)) {
|
318
|
-
const prevContent = await promises.readFile(dtsFile, "utf8").catch(() => "");
|
319
|
-
const match = prevContent.match(/export\s+\*\s+from\s+["'](.+)["']/);
|
320
|
-
if (match) {
|
321
|
-
fromPath = match[1];
|
322
|
-
}
|
323
|
-
}
|
324
|
-
let dtsContents;
|
325
|
-
const importFrom = fromPath || "./module.mjs";
|
326
|
-
dtsContents = `${`
|
327
|
-
${moduleImports.length ? `import type { ${moduleImports.join(", ")} } from '${importFrom}'` : ""}
|
328
|
-
|
329
|
-
${moduleImports.length ? `
|
330
|
-
export type { ${moduleImports.join(", ")} }` : ""}
|
331
|
-
`.trim().replace(/[\n\r]{3,}/g, "\n\n")}
|
332
|
-
`;
|
333
|
-
await promises.writeFile(dtsFile, dtsContents, "utf8");
|
334
|
-
}
|
335
|
-
async function loadTSCompilerOptions(path) {
|
336
|
-
const config = await parse(path);
|
337
|
-
const resolvedCompilerOptions = config?.tsconfig.compilerOptions || {};
|
338
|
-
for (const { tsconfig, tsconfigFile } of config.extended || []) {
|
339
|
-
for (const alias in tsconfig.compilerOptions?.paths || {}) {
|
340
|
-
resolvedCompilerOptions.paths[alias] = resolvedCompilerOptions.paths[alias].map((p) => {
|
341
|
-
if (!/^\.{1,2}(?:\/|$)/.test(p))
|
342
|
-
return p;
|
343
|
-
return resolve(dirname(tsconfigFile), p);
|
344
|
-
});
|
345
|
-
}
|
346
|
-
}
|
347
|
-
return resolvedCompilerOptions;
|
348
|
-
}
|
349
|
-
|
350
|
-
export { build as default, subpaths };
|