@savvy-web/rslib-builder 0.2.2 → 0.3.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/index.d.ts +466 -412
- package/index.js +48 -204
- package/package.json +1 -5
package/index.js
CHANGED
|
@@ -3,10 +3,10 @@ import * as __rspack_external_node_path_c5b9b54f from "node:path";
|
|
|
3
3
|
import { __webpack_require__ } from "./rslib-runtime.js";
|
|
4
4
|
import { constants, existsSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { defineConfig } from "@rslib/core";
|
|
6
|
-
import { access, copyFile, mkdir, readFile
|
|
7
|
-
import { getWorkspaceRoot } from "workspace-tools";
|
|
6
|
+
import { access, copyFile, mkdir, readFile, readdir, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
8
7
|
import { logger as core_logger } from "@rsbuild/core";
|
|
9
8
|
import picocolors from "picocolors";
|
|
9
|
+
import { getWorkspaceRoot } from "workspace-tools";
|
|
10
10
|
import { spawn } from "node:child_process";
|
|
11
11
|
import { StandardTags, Standardization, TSDocTagSyntaxKind } from "@microsoft/tsdoc";
|
|
12
12
|
import { createCompilerHost, findConfigFile, formatDiagnostic, parseJsonConfigFileContent, readConfigFile, sys } from "typescript";
|
|
@@ -20,38 +20,6 @@ __webpack_require__.add({
|
|
|
20
20
|
module.exports = __rspack_external_node_path_c5b9b54f;
|
|
21
21
|
}
|
|
22
22
|
});
|
|
23
|
-
const external_node_path_ = __webpack_require__("node:path");
|
|
24
|
-
async function fileExistAsync(assetName) {
|
|
25
|
-
const assetPath = (0, external_node_path_.join)(process.cwd(), assetName);
|
|
26
|
-
const assetExists = !!await promises_stat(assetPath).catch(()=>false);
|
|
27
|
-
return {
|
|
28
|
-
assetName,
|
|
29
|
-
assetPath,
|
|
30
|
-
assetExists
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
async function packageJsonVersion() {
|
|
34
|
-
const { assetExists, assetPath } = await fileExistAsync("package.json");
|
|
35
|
-
if (!assetExists) throw new Error("package.json not found in project root");
|
|
36
|
-
try {
|
|
37
|
-
const json = await promises_readFile(assetPath, "utf-8");
|
|
38
|
-
const { version } = JSON.parse(json);
|
|
39
|
-
return version;
|
|
40
|
-
} catch {
|
|
41
|
-
throw new Error("Failed to read version from package.json");
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function getApiExtractorPath() {
|
|
45
|
-
const cwd = process.cwd();
|
|
46
|
-
const localApiExtractor = (0, external_node_path_.join)(cwd, "node_modules", "@microsoft", "api-extractor");
|
|
47
|
-
if (existsSync(localApiExtractor)) return localApiExtractor;
|
|
48
|
-
const workspaceRoot = getWorkspaceRoot(cwd);
|
|
49
|
-
if (workspaceRoot) {
|
|
50
|
-
const workspaceApiExtractor = (0, external_node_path_.join)(workspaceRoot, "node_modules", "@microsoft", "api-extractor");
|
|
51
|
-
if (existsSync(workspaceApiExtractor)) return workspaceApiExtractor;
|
|
52
|
-
}
|
|
53
|
-
throw new Error("API Extractor bundling requires @microsoft/api-extractor to be installed.\nInstall it with: pnpm add -D @microsoft/api-extractor");
|
|
54
|
-
}
|
|
55
23
|
const { cyan: cyan, dim: dim, bold: bold } = picocolors;
|
|
56
24
|
function formatTime(ms) {
|
|
57
25
|
if (ms < 1000) return `${ms}ms`;
|
|
@@ -182,6 +150,38 @@ function extractEntriesFromPackageJson(packageJson, options) {
|
|
|
182
150
|
const extractor = new EntryExtractor(options);
|
|
183
151
|
return extractor.extract(packageJson);
|
|
184
152
|
}
|
|
153
|
+
const external_node_path_ = __webpack_require__("node:path");
|
|
154
|
+
async function fileExistAsync(assetName) {
|
|
155
|
+
const assetPath = (0, external_node_path_.join)(process.cwd(), assetName);
|
|
156
|
+
const assetExists = !!await stat(assetPath).catch(()=>false);
|
|
157
|
+
return {
|
|
158
|
+
assetName,
|
|
159
|
+
assetPath,
|
|
160
|
+
assetExists
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
async function packageJsonVersion() {
|
|
164
|
+
const { assetExists, assetPath } = await fileExistAsync("package.json");
|
|
165
|
+
if (!assetExists) throw new Error("package.json not found in project root");
|
|
166
|
+
try {
|
|
167
|
+
const json = await readFile(assetPath, "utf-8");
|
|
168
|
+
const { version } = JSON.parse(json);
|
|
169
|
+
return version;
|
|
170
|
+
} catch {
|
|
171
|
+
throw new Error("Failed to read version from package.json");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function getApiExtractorPath() {
|
|
175
|
+
const cwd = process.cwd();
|
|
176
|
+
const localApiExtractor = (0, external_node_path_.join)(cwd, "node_modules", "@microsoft", "api-extractor");
|
|
177
|
+
if (existsSync(localApiExtractor)) return localApiExtractor;
|
|
178
|
+
const workspaceRoot = getWorkspaceRoot(cwd);
|
|
179
|
+
if (workspaceRoot) {
|
|
180
|
+
const workspaceApiExtractor = (0, external_node_path_.join)(workspaceRoot, "node_modules", "@microsoft", "api-extractor");
|
|
181
|
+
if (existsSync(workspaceApiExtractor)) return workspaceApiExtractor;
|
|
182
|
+
}
|
|
183
|
+
throw new Error("API Extractor bundling requires @microsoft/api-extractor to be installed.\nInstall it with: pnpm add -D @microsoft/api-extractor");
|
|
184
|
+
}
|
|
185
185
|
const AutoEntryPlugin = (options)=>{
|
|
186
186
|
const buildStateMap = new WeakMap();
|
|
187
187
|
return {
|
|
@@ -212,7 +212,7 @@ const AutoEntryPlugin = (options)=>{
|
|
|
212
212
|
throw new Error("package.json not found in project root");
|
|
213
213
|
}
|
|
214
214
|
try {
|
|
215
|
-
const packageJsonContent = await
|
|
215
|
+
const packageJsonContent = await readFile(assetPath, "utf-8");
|
|
216
216
|
const packageJson = JSON.parse(packageJsonContent);
|
|
217
217
|
const { entries } = extractEntriesFromPackageJson(packageJson, {
|
|
218
218
|
exportsAsIndexes: options?.exportsAsIndexes
|
|
@@ -265,16 +265,11 @@ const AutoEntryPlugin = (options)=>{
|
|
|
265
265
|
};
|
|
266
266
|
};
|
|
267
267
|
var lib_namespaceObject = JSON.parse('{"$schema":"https://json.schemastore.org/tsconfig.json","compilerOptions":{"allowSyntheticDefaultImports":true,"composite":true,"declaration":true,"declarationDir":"${configDir}/dist","declarationMap":false,"emitDeclarationOnly":false,"esModuleInterop":true,"explainFiles":false,"forceConsistentCasingInFileNames":true,"incremental":true,"isolatedDeclarations":true,"isolatedModules":true,"jsx":"preserve","lib":["esnext"],"module":"nodenext","moduleResolution":"nodenext","outDir":"${configDir}/dist","resolveJsonModule":true,"rootDir":"${configDir}","skipLibCheck":true,"sourceMap":false,"strict":true,"strictNullChecks":true,"target":"es2023","tsBuildInfoFile":"${configDir}/dist/.tsbuildinfo.lib","typeRoots":["${configDir}/node_modules/@types","${configDir}/types"],"verbatimModuleSyntax":true},"exclude":["${configDir}/node_modules","${configDir}/dist/**/*"],"include":["${configDir}/types/*.ts","${configDir}/package.json","${configDir}/*.ts","${configDir}/*.cts","${configDir}/*.mts","${configDir}/src/**/*.ts","${configDir}/src/**/*.tsx","${configDir}/src/**/*.cts","${configDir}/src/**/*.mts","${configDir}/lib/**/*.ts","${configDir}/lib/**/*.tsx","${configDir}/lib/**/*.cts","${configDir}/lib/**/*.mts","${configDir}/public/**/*.json"]}');
|
|
268
|
-
var root_namespaceObject = JSON.parse('{"$schema":"https://json.schemastore.org/tsconfig","compilerOptions":{"composite":true,"declaration":true,"module":"node20","outDir":"dist","resolveJsonModule":true,"strict":true,"strictNullChecks":true,"target":"es2023"}}');
|
|
269
268
|
const requireCJS = createRequire(import.meta.url);
|
|
270
269
|
const jsonImports = new Map([
|
|
271
270
|
[
|
|
272
271
|
(0, external_node_path_.join)(import.meta.dirname, "../public/tsconfig/ecma/lib.json"),
|
|
273
272
|
lib_namespaceObject
|
|
274
|
-
],
|
|
275
|
-
[
|
|
276
|
-
(0, external_node_path_.join)(import.meta.dirname, "../public/tsconfig/root.json"),
|
|
277
|
-
root_namespaceObject
|
|
278
273
|
]
|
|
279
274
|
]);
|
|
280
275
|
function transformStringsDeep(value, transform) {
|
|
@@ -306,26 +301,6 @@ class TSConfigFile {
|
|
|
306
301
|
constructor(description, pathname){
|
|
307
302
|
this.pathname = pathname;
|
|
308
303
|
this.description = description;
|
|
309
|
-
Object.defineProperty(this, "path", {
|
|
310
|
-
enumerable: true,
|
|
311
|
-
get: ()=>`./${(0, external_node_path_.relative)(process.cwd(), this.pathname)}`
|
|
312
|
-
});
|
|
313
|
-
Object.defineProperty(this, "config", {
|
|
314
|
-
enumerable: true,
|
|
315
|
-
get: ()=>{
|
|
316
|
-
const imported = jsonImports.get(this.pathname);
|
|
317
|
-
if (!imported) throw new Error(`Config file not found in imports: ${this.pathname}`);
|
|
318
|
-
return imported;
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
Object.defineProperty(this, "bundled", {
|
|
322
|
-
enumerable: true,
|
|
323
|
-
get: ()=>{
|
|
324
|
-
const imported = jsonImports.get(this.pathname);
|
|
325
|
-
if (!imported) throw new Error(`Config file not found in imports: ${this.pathname}`);
|
|
326
|
-
return transformStringsDeep(imported, (str)=>str.replace("${configDir}", "../../../../../.."));
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
304
|
Object.defineProperty(this, inspect.custom, {
|
|
330
305
|
value: (_depth, options)=>inspect({
|
|
331
306
|
description: this.description,
|
|
@@ -385,10 +360,8 @@ class LibraryTSConfigFile extends TSConfigFile {
|
|
|
385
360
|
return tmpFile.name;
|
|
386
361
|
}
|
|
387
362
|
}
|
|
388
|
-
const Root = new TSConfigFile("Root configuration for workspace setup", (0, external_node_path_.join)(import.meta.dirname, "../public/tsconfig/root.json"));
|
|
389
363
|
const NodeEcmaLib = new LibraryTSConfigFile("ECMAScript library build configuration", (0, external_node_path_.join)(import.meta.dirname, "../public/tsconfig/ecma/lib.json"));
|
|
390
364
|
const TSConfigs = {
|
|
391
|
-
root: Root,
|
|
392
365
|
node: {
|
|
393
366
|
ecma: {
|
|
394
367
|
lib: NodeEcmaLib
|
|
@@ -647,7 +620,7 @@ async function bundleDtsFiles(options) {
|
|
|
647
620
|
if (generateApiModel && tempApiModelPath) apiModelPath = tempApiModelPath;
|
|
648
621
|
if (generateTsdocMetadata && tempTsdocMetadataPath) tsdocMetadataPath = tempTsdocMetadataPath;
|
|
649
622
|
if (banner || footer) {
|
|
650
|
-
let content = await
|
|
623
|
+
let content = await readFile(tempBundledPath, "utf-8");
|
|
651
624
|
if (banner) content = `${banner}\n${content}`;
|
|
652
625
|
if (footer) content = `${content}\n${footer}`;
|
|
653
626
|
await writeFile(tempBundledPath, content, "utf-8");
|
|
@@ -838,7 +811,7 @@ function runTsgo(options) {
|
|
|
838
811
|
packageJson = exposedPackageJson;
|
|
839
812
|
} else {
|
|
840
813
|
const packageJsonPath = (0, external_node_path_.join)(cwd, "package.json");
|
|
841
|
-
const packageJsonContent = await
|
|
814
|
+
const packageJsonContent = await readFile(packageJsonPath, "utf-8");
|
|
842
815
|
packageJson = JSON.parse(packageJsonContent);
|
|
843
816
|
}
|
|
844
817
|
const entryPoints = new Map();
|
|
@@ -884,7 +857,7 @@ function runTsgo(options) {
|
|
|
884
857
|
let emittedCount = 0;
|
|
885
858
|
for (const [entryName, tempBundledPath] of bundledFiles){
|
|
886
859
|
const bundledFileName = `${entryName}.d.ts`;
|
|
887
|
-
let content = await
|
|
860
|
+
let content = await readFile(tempBundledPath, "utf-8");
|
|
888
861
|
content = stripSourceMapComment(content);
|
|
889
862
|
const source = new context.sources.OriginalSource(content, bundledFileName);
|
|
890
863
|
context.compilation.emitAsset(bundledFileName, source);
|
|
@@ -895,7 +868,7 @@ function runTsgo(options) {
|
|
|
895
868
|
if (apiModelPath) {
|
|
896
869
|
const defaultApiModelFilename = packageJson.name ? `${getUnscopedPackageName(packageJson.name)}.api.json` : "api.json";
|
|
897
870
|
const apiModelFilename = "object" == typeof options.apiModel && options.apiModel.filename ? options.apiModel.filename : defaultApiModelFilename;
|
|
898
|
-
const apiModelContent = await
|
|
871
|
+
const apiModelContent = await readFile(apiModelPath, "utf-8");
|
|
899
872
|
const apiModelSource = new context.sources.OriginalSource(apiModelContent, apiModelFilename);
|
|
900
873
|
context.compilation.emitAsset(apiModelFilename, apiModelSource);
|
|
901
874
|
if (filesArray) filesArray.add(`!${apiModelFilename}`);
|
|
@@ -918,14 +891,14 @@ function runTsgo(options) {
|
|
|
918
891
|
if (tsdocMetadataPath) {
|
|
919
892
|
const tsdocMetadataOption = "object" == typeof options.apiModel ? options.apiModel.tsdocMetadata : void 0;
|
|
920
893
|
const tsdocMetadataFilename = "object" == typeof tsdocMetadataOption && tsdocMetadataOption.filename ? tsdocMetadataOption.filename : "tsdoc-metadata.json";
|
|
921
|
-
const tsdocMetadataContent = await
|
|
894
|
+
const tsdocMetadataContent = await readFile(tsdocMetadataPath, "utf-8");
|
|
922
895
|
const tsdocMetadataSource = new context.sources.OriginalSource(tsdocMetadataContent, tsdocMetadataFilename);
|
|
923
896
|
context.compilation.emitAsset(tsdocMetadataFilename, tsdocMetadataSource);
|
|
924
897
|
if (filesArray) filesArray.add(tsdocMetadataFilename);
|
|
925
898
|
core_logger.info(`${picocolors.dim(`[${envId}]`)} Emitted TSDoc metadata: ${tsdocMetadataFilename}`);
|
|
926
899
|
}
|
|
927
900
|
if (tsdocConfigPath) {
|
|
928
|
-
const tsdocConfigContent = await
|
|
901
|
+
const tsdocConfigContent = await readFile(tsdocConfigPath, "utf-8");
|
|
929
902
|
const tsdocConfigSource = new context.sources.OriginalSource(tsdocConfigContent, "tsdoc.json");
|
|
930
903
|
context.compilation.emitAsset("tsdoc.json", tsdocConfigSource);
|
|
931
904
|
if (filesArray) filesArray.add("!tsdoc.json");
|
|
@@ -953,7 +926,7 @@ function runTsgo(options) {
|
|
|
953
926
|
let emittedCount = 0;
|
|
954
927
|
for (const file of dtsFiles){
|
|
955
928
|
if (file.relativePath.endsWith(".d.ts.map")) continue;
|
|
956
|
-
let content = await
|
|
929
|
+
let content = await readFile(file.path, "utf-8");
|
|
957
930
|
let outputPath = file.relativePath;
|
|
958
931
|
if (outputPath.startsWith("src/")) outputPath = outputPath.slice(4);
|
|
959
932
|
if (".d.ts" !== dtsExtension && outputPath.endsWith(".d.ts")) outputPath = outputPath.replace(/\.d\.ts$/, dtsExtension);
|
|
@@ -1022,7 +995,7 @@ function runTsgo(options) {
|
|
|
1022
995
|
recursive: true
|
|
1023
996
|
});
|
|
1024
997
|
for (const file of filesToCopy){
|
|
1025
|
-
const content = await
|
|
998
|
+
const content = await readFile(file.src, "utf-8");
|
|
1026
999
|
await writeFile(file.dest, content, "utf-8");
|
|
1027
1000
|
}
|
|
1028
1001
|
const fileNames = filesToCopy.map((f)=>f.name).join(", ");
|
|
@@ -1055,7 +1028,7 @@ class TextAsset {
|
|
|
1055
1028
|
if (asset) return new TextAsset(context, fileName);
|
|
1056
1029
|
try {
|
|
1057
1030
|
const filePath = (0, external_node_path_.join)(process.cwd(), fileName);
|
|
1058
|
-
const content = await
|
|
1031
|
+
const content = await readFile(filePath, "utf-8");
|
|
1059
1032
|
const source = new context.sources.RawSource(content);
|
|
1060
1033
|
context.compilation.emitAsset(fileName, source);
|
|
1061
1034
|
asset = source;
|
|
@@ -1085,7 +1058,7 @@ class JsonAsset extends TextAsset {
|
|
|
1085
1058
|
if (asset) return new JsonAsset(context, fileName);
|
|
1086
1059
|
try {
|
|
1087
1060
|
const filePath = (0, external_node_path_.join)(process.cwd(), fileName);
|
|
1088
|
-
const content = await
|
|
1061
|
+
const content = await readFile(filePath, "utf-8");
|
|
1089
1062
|
const source = new context.sources.RawSource(content);
|
|
1090
1063
|
context.compilation.emitAsset(fileName, source);
|
|
1091
1064
|
asset = source;
|
|
@@ -1166,10 +1139,10 @@ class PnpmCatalog {
|
|
|
1166
1139
|
if (!this.cachedWorkspaceRoot) throw new Error("Could not find workspace root - ensure you're in a workspace");
|
|
1167
1140
|
}
|
|
1168
1141
|
const workspaceFile = (0, external_node_path_.resolve)(this.cachedWorkspaceRoot, "pnpm-workspace.yaml");
|
|
1169
|
-
const stats = await
|
|
1142
|
+
const stats = await stat(workspaceFile);
|
|
1170
1143
|
const currentMtime = stats.mtime.getTime();
|
|
1171
1144
|
if (null !== this.catalogCache && this.catalogCacheMtime === currentMtime) return this.catalogCache;
|
|
1172
|
-
const content = await
|
|
1145
|
+
const content = await readFile(workspaceFile, "utf-8");
|
|
1173
1146
|
const workspace = parse(content);
|
|
1174
1147
|
this.catalogCache = workspace.catalog ?? {};
|
|
1175
1148
|
this.catalogCacheMtime = currentMtime;
|
|
@@ -1300,135 +1273,6 @@ function getDefaultPnpmCatalog() {
|
|
|
1300
1273
|
if (!defaultInstance) defaultInstance = new PnpmCatalog();
|
|
1301
1274
|
return defaultInstance;
|
|
1302
1275
|
}
|
|
1303
|
-
class PackageJsonTransformer {
|
|
1304
|
-
options;
|
|
1305
|
-
pnpmCatalog;
|
|
1306
|
-
constructor(options = {}){
|
|
1307
|
-
this.options = {
|
|
1308
|
-
processTSExports: options.processTSExports ?? true,
|
|
1309
|
-
collapseIndex: options.collapseIndex ?? false,
|
|
1310
|
-
entrypoints: options.entrypoints ?? new Map(),
|
|
1311
|
-
exportToOutputMap: options.exportToOutputMap ?? new Map()
|
|
1312
|
-
};
|
|
1313
|
-
this.pnpmCatalog = new PnpmCatalog();
|
|
1314
|
-
}
|
|
1315
|
-
transformExportPath(path) {
|
|
1316
|
-
let transformedPath = path;
|
|
1317
|
-
if (transformedPath.startsWith("./src/")) transformedPath = `./${transformedPath.slice(6)}`;
|
|
1318
|
-
if (transformedPath.startsWith("./exports/")) transformedPath = `./${transformedPath.slice(10)}`;
|
|
1319
|
-
if (transformedPath.startsWith("./public/")) transformedPath = `./${transformedPath.slice(9)}`;
|
|
1320
|
-
if (this.options.processTSExports) {
|
|
1321
|
-
const { collapseIndex } = this.options;
|
|
1322
|
-
if (collapseIndex && transformedPath.endsWith("/index.ts") && "./index.ts" !== transformedPath) transformedPath = `${transformedPath.slice(0, -9)}.js`;
|
|
1323
|
-
else if (collapseIndex && transformedPath.endsWith("/index.tsx") && "./index.tsx" !== transformedPath) transformedPath = `${transformedPath.slice(0, -10)}.js`;
|
|
1324
|
-
else if (transformedPath.endsWith(".tsx")) transformedPath = `${transformedPath.slice(0, -4)}.js`;
|
|
1325
|
-
else if (transformedPath.endsWith(".ts") && !transformedPath.endsWith(".d.ts")) transformedPath = `${transformedPath.slice(0, -3)}.js`;
|
|
1326
|
-
}
|
|
1327
|
-
return transformedPath;
|
|
1328
|
-
}
|
|
1329
|
-
createTypePath(jsPath) {
|
|
1330
|
-
const { collapseIndex } = this.options;
|
|
1331
|
-
if (collapseIndex && jsPath.endsWith("/index.js") && "./index.js" !== jsPath) return `${jsPath.slice(0, -9)}.d.ts`;
|
|
1332
|
-
if (jsPath.endsWith(".js")) return `${jsPath.slice(0, -3)}.d.ts`;
|
|
1333
|
-
return `${jsPath}.d.ts`;
|
|
1334
|
-
}
|
|
1335
|
-
transformExports(exports, exportKey) {
|
|
1336
|
-
if ("string" == typeof exports) return this.transformStringExport(exports, exportKey);
|
|
1337
|
-
if (Array.isArray(exports)) return exports.map((item)=>{
|
|
1338
|
-
const transformed = this.transformExports(item, exportKey);
|
|
1339
|
-
return transformed ?? item;
|
|
1340
|
-
});
|
|
1341
|
-
if (exports && "object" == typeof exports) return this.transformObjectExports(exports, exportKey);
|
|
1342
|
-
return exports;
|
|
1343
|
-
}
|
|
1344
|
-
transformBin(bin) {
|
|
1345
|
-
if ("string" == typeof bin) {
|
|
1346
|
-
if (bin.endsWith(".ts") || bin.endsWith(".tsx")) return "./bin/cli.js";
|
|
1347
|
-
return bin;
|
|
1348
|
-
}
|
|
1349
|
-
if (bin && "object" == typeof bin) {
|
|
1350
|
-
const transformed = {};
|
|
1351
|
-
for (const [command, path] of Object.entries(bin))if (void 0 !== path) if (path.endsWith(".ts") || path.endsWith(".tsx")) transformed[command] = `./bin/${command}.js`;
|
|
1352
|
-
else transformed[command] = path;
|
|
1353
|
-
return transformed;
|
|
1354
|
-
}
|
|
1355
|
-
return bin;
|
|
1356
|
-
}
|
|
1357
|
-
async transform(packageJson, context = {}) {
|
|
1358
|
-
const { isProduction = false, customTransform } = context;
|
|
1359
|
-
let result;
|
|
1360
|
-
if (isProduction) {
|
|
1361
|
-
const pnpmTransformed = await this.applyPnpmTransformations(packageJson);
|
|
1362
|
-
result = this.applyRslibTransformations(pnpmTransformed, packageJson);
|
|
1363
|
-
} else result = this.applyRslibTransformations(packageJson, packageJson);
|
|
1364
|
-
if (customTransform) result = customTransform(result);
|
|
1365
|
-
return result;
|
|
1366
|
-
}
|
|
1367
|
-
async applyPnpmTransformations(packageJson) {
|
|
1368
|
-
return this.pnpmCatalog.resolvePackageJson(packageJson);
|
|
1369
|
-
}
|
|
1370
|
-
applyRslibTransformations(packageJson, originalPackageJson) {
|
|
1371
|
-
const { publishConfig, scripts, ...rest } = packageJson;
|
|
1372
|
-
let isPrivate = true;
|
|
1373
|
-
if (originalPackageJson.publishConfig?.access === "public") isPrivate = false;
|
|
1374
|
-
const processedManifest = {
|
|
1375
|
-
...rest,
|
|
1376
|
-
private: isPrivate
|
|
1377
|
-
};
|
|
1378
|
-
if (processedManifest.exports) processedManifest.exports = this.transformExports(processedManifest.exports);
|
|
1379
|
-
if (processedManifest.bin) processedManifest.bin = this.transformBin(processedManifest.bin);
|
|
1380
|
-
if (originalPackageJson.typesVersions) {
|
|
1381
|
-
const transformedTypesVersions = {};
|
|
1382
|
-
for (const [version, paths] of Object.entries(originalPackageJson.typesVersions)){
|
|
1383
|
-
const transformedPaths = {};
|
|
1384
|
-
for (const [key, value] of Object.entries(paths))transformedPaths[key] = value.map((path)=>this.transformExportPath(path));
|
|
1385
|
-
transformedTypesVersions[version] = transformedPaths;
|
|
1386
|
-
}
|
|
1387
|
-
processedManifest.typesVersions = transformedTypesVersions;
|
|
1388
|
-
}
|
|
1389
|
-
if (originalPackageJson.files) processedManifest.files = originalPackageJson.files.map((file)=>{
|
|
1390
|
-
let transformedFile = file.startsWith("./") ? file.slice(2) : file;
|
|
1391
|
-
if (transformedFile.startsWith("public/")) transformedFile = transformedFile.slice(7);
|
|
1392
|
-
return transformedFile;
|
|
1393
|
-
});
|
|
1394
|
-
return sort_package_json(processedManifest);
|
|
1395
|
-
}
|
|
1396
|
-
transformStringExport(exportString, exportKey) {
|
|
1397
|
-
const { entrypoints, exportToOutputMap, processTSExports } = this.options;
|
|
1398
|
-
let transformedPath;
|
|
1399
|
-
if (exportToOutputMap.size > 0 && exportKey && exportToOutputMap.has(exportKey)) {
|
|
1400
|
-
const mappedPath = exportToOutputMap.get(exportKey);
|
|
1401
|
-
if (!mappedPath) throw new Error(`Export key "${exportKey}" has no mapped path`);
|
|
1402
|
-
transformedPath = mappedPath;
|
|
1403
|
-
} else if (entrypoints.size > 0 && exportKey) {
|
|
1404
|
-
const keyWithoutPrefix = exportKey.startsWith("./") ? exportKey.slice(2) : exportKey;
|
|
1405
|
-
transformedPath = entrypoints.has(exportKey) ? entrypoints.get(exportKey) ?? exportString : entrypoints.has(keyWithoutPrefix) ? entrypoints.get(keyWithoutPrefix) ?? exportString : this.transformExportPath(exportString);
|
|
1406
|
-
} else transformedPath = this.transformExportPath(exportString);
|
|
1407
|
-
if (processTSExports && (exportString.endsWith(".ts") || exportString.endsWith(".tsx")) && !exportString.endsWith(".d.ts")) return {
|
|
1408
|
-
types: this.createTypePath(transformedPath),
|
|
1409
|
-
import: transformedPath
|
|
1410
|
-
};
|
|
1411
|
-
return transformedPath;
|
|
1412
|
-
}
|
|
1413
|
-
transformObjectExports(exportsObject, exportKey) {
|
|
1414
|
-
const transformed = {};
|
|
1415
|
-
const isConditions = this.isConditionsObject(exportsObject);
|
|
1416
|
-
for (const [key, value] of Object.entries(exportsObject))transformed[key] = this.transformExportEntry(key, value, isConditions, exportKey);
|
|
1417
|
-
return transformed;
|
|
1418
|
-
}
|
|
1419
|
-
isConditionsObject(exports) {
|
|
1420
|
-
return Object.keys(exports).some((key)=>"import" === key || "require" === key || "types" === key || "default" === key);
|
|
1421
|
-
}
|
|
1422
|
-
transformExportEntry(key, value, isConditions, exportKey) {
|
|
1423
|
-
if (isConditions && ("import" === key || "require" === key || "types" === key || "default" === key)) {
|
|
1424
|
-
if ("string" == typeof value) return this.transformExportPath(value);
|
|
1425
|
-
if (null != value) return this.transformExports(value, exportKey);
|
|
1426
|
-
return value;
|
|
1427
|
-
}
|
|
1428
|
-
if (null != value) return this.transformExports(value, key);
|
|
1429
|
-
return value;
|
|
1430
|
-
}
|
|
1431
|
-
}
|
|
1432
1276
|
function transformExportPath(path, processTSExports = true, collapseIndex = false) {
|
|
1433
1277
|
let transformedPath = path;
|
|
1434
1278
|
if (transformedPath.startsWith("./src/")) transformedPath = `./${transformedPath.slice(6)}`;
|
|
@@ -1721,4 +1565,4 @@ const PackageJsonTransformPlugin = (options = {})=>{
|
|
|
1721
1565
|
});
|
|
1722
1566
|
}
|
|
1723
1567
|
}
|
|
1724
|
-
export { AutoEntryPlugin, DtsPlugin,
|
|
1568
|
+
export { AutoEntryPlugin, DtsPlugin, FilesArrayPlugin, NodeLibraryBuilder, PackageJsonTransformPlugin, TsDocConfigBuilder };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@savvy-web/rslib-builder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "RSlib-based build system for Node.js libraries with automatic package.json transformation, TypeScript declaration bundling, and multi-target support",
|
|
6
6
|
"homepage": "https://github.com/savvy-web/rslib-builder",
|
|
@@ -15,10 +15,6 @@
|
|
|
15
15
|
"url": "https://savvyweb.systems"
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
|
18
|
-
"imports": {
|
|
19
|
-
"#utils/*": "./src/rslib/plugins/utils/*",
|
|
20
|
-
"#types/*": "./src/types/*"
|
|
21
|
-
},
|
|
22
18
|
"exports": {
|
|
23
19
|
".": {
|
|
24
20
|
"types": "./index.d.ts",
|