dtsroll 1.8.0 → 1.8.2
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/README.md +10 -1
- package/dist/cli.mjs +2 -2
- package/dist/{index-BsSgZDr9.mjs → index-BkaN1nHE.mjs} +402 -59
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +4 -6
package/README.md
CHANGED
|
@@ -177,11 +177,20 @@ export default defineConfig({
|
|
|
177
177
|
```ts
|
|
178
178
|
import { dtsroll } from 'dtsroll'
|
|
179
179
|
|
|
180
|
-
await dtsroll({
|
|
180
|
+
const result = await dtsroll({
|
|
181
181
|
cwd: process.cwd(),
|
|
182
182
|
dryRun: false,
|
|
183
183
|
sourcemap: true // generates .d.ts.map files
|
|
184
184
|
})
|
|
185
|
+
|
|
186
|
+
if ('error' in result) {
|
|
187
|
+
console.error(result.error)
|
|
188
|
+
} else {
|
|
189
|
+
// Warnings about non-portable shared chunk types (TS2742)
|
|
190
|
+
for (const warning of result.warnings) {
|
|
191
|
+
console.warn(warning)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
185
194
|
```
|
|
186
195
|
|
|
187
196
|
## Related
|
package/dist/cli.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { cli } from 'cleye';
|
|
3
|
-
import { b as bgYellow, a as black, d as dtsroll, l as logOutput } from './index-
|
|
3
|
+
import { b as bgYellow, a as black, d as dtsroll, l as logOutput } from './index-BkaN1nHE.mjs';
|
|
4
4
|
import { patchErrorWithTrace } from 'rollup-plugin-import-trace';
|
|
5
5
|
import 'node:path';
|
|
6
6
|
import 'node:fs/promises';
|
|
@@ -14,7 +14,7 @@ import 'resolve-pkg-maps';
|
|
|
14
14
|
import 'byte-size';
|
|
15
15
|
|
|
16
16
|
var name = "dtsroll";
|
|
17
|
-
var version = "1.8.
|
|
17
|
+
var version = "1.8.2";
|
|
18
18
|
var description = "Bundle dts files";
|
|
19
19
|
|
|
20
20
|
const argv = cli({
|
|
@@ -75,10 +75,10 @@ const bgYellow = kolorist(43, 49);
|
|
|
75
75
|
const cwd = process.cwd();
|
|
76
76
|
|
|
77
77
|
const isPath = (filePath) => filePath[0] === "." || path__default.isAbsolute(filePath);
|
|
78
|
-
const normalizePath$
|
|
78
|
+
const normalizePath$2 = (filepath) => filepath.replaceAll("\\", "/");
|
|
79
79
|
const getDisplayPath = (fullPath) => {
|
|
80
80
|
const relativePath = path__default.relative(cwd, fullPath);
|
|
81
|
-
return normalizePath$
|
|
81
|
+
return normalizePath$2(
|
|
82
82
|
relativePath.length < fullPath.length ? relativePath : fullPath
|
|
83
83
|
);
|
|
84
84
|
};
|
|
@@ -276,9 +276,9 @@ const getAllFiles = async (directoryPath, dontShortenPath) => {
|
|
|
276
276
|
const filePath = path__default.join(directoryPath, entry.name);
|
|
277
277
|
if (entry.isDirectory()) {
|
|
278
278
|
const files = await getAllFiles(filePath, true);
|
|
279
|
-
return dontShortenPath ? files : files.map((file) => `./${normalizePath$
|
|
279
|
+
return dontShortenPath ? files : files.map((file) => `./${normalizePath$2(path__default.relative(directoryPath, file))}`);
|
|
280
280
|
}
|
|
281
|
-
return dontShortenPath ? filePath : `./${normalizePath$
|
|
281
|
+
return dontShortenPath ? filePath : `./${normalizePath$2(path__default.relative(directoryPath, filePath))}`;
|
|
282
282
|
})
|
|
283
283
|
);
|
|
284
284
|
return fileTree.flat();
|
|
@@ -652,14 +652,14 @@ function stripPathFilename(path) {
|
|
|
652
652
|
return path.slice(0, index + 1);
|
|
653
653
|
}
|
|
654
654
|
function mergePaths(url, base) {
|
|
655
|
-
normalizePath(base, base.type);
|
|
655
|
+
normalizePath$1(base, base.type);
|
|
656
656
|
if (url.path === "/") {
|
|
657
657
|
url.path = base.path;
|
|
658
658
|
} else {
|
|
659
659
|
url.path = stripPathFilename(base.path) + url.path;
|
|
660
660
|
}
|
|
661
661
|
}
|
|
662
|
-
function normalizePath(url, type) {
|
|
662
|
+
function normalizePath$1(url, type) {
|
|
663
663
|
const rel = type <= 4;
|
|
664
664
|
const pieces = url.path.split("/");
|
|
665
665
|
let pointer = 1;
|
|
@@ -726,7 +726,7 @@ function resolve(input, base) {
|
|
|
726
726
|
if (baseType > inputType)
|
|
727
727
|
inputType = baseType;
|
|
728
728
|
}
|
|
729
|
-
normalizePath(url, inputType);
|
|
729
|
+
normalizePath$1(url, inputType);
|
|
730
730
|
const queryHash = url.query + url.hash;
|
|
731
731
|
switch (inputType) {
|
|
732
732
|
// This is impossible, because of the empty checks at the start of the function.
|
|
@@ -2750,7 +2750,6 @@ class LanguageService {
|
|
|
2750
2750
|
}
|
|
2751
2751
|
class TypeOnlyFixer {
|
|
2752
2752
|
constructor(fileName, rawCode) {
|
|
2753
|
-
this.DEBUG = !!process.env.DTS_EXPORTS_FIXER_DEBUG;
|
|
2754
2753
|
this.types = /* @__PURE__ */ new Set();
|
|
2755
2754
|
this.values = /* @__PURE__ */ new Set();
|
|
2756
2755
|
this.typeHints = /* @__PURE__ */ new Map();
|
|
@@ -2853,7 +2852,7 @@ ${getNodeIndent(node)}`));
|
|
|
2853
2852
|
const name = element.propertyName?.text || element.name.text;
|
|
2854
2853
|
const isType = node.moduleSpecifier ? this.isReExportTypeOnly(element.name.text) : this.isTypeOnly(name);
|
|
2855
2854
|
if (isType) {
|
|
2856
|
-
typeNames.push(element
|
|
2855
|
+
typeNames.push(getExportSpecifierBinding(element));
|
|
2857
2856
|
} else {
|
|
2858
2857
|
valueNames.push(element.getText());
|
|
2859
2858
|
}
|
|
@@ -2872,7 +2871,6 @@ ${getNodeIndent(node)}`));
|
|
|
2872
2871
|
}
|
|
2873
2872
|
analyze(nodes) {
|
|
2874
2873
|
for (const node of nodes) {
|
|
2875
|
-
this.DEBUG && console.log(node.getText(), node.kind);
|
|
2876
2874
|
if (ts.isImportDeclaration(node) && node.importClause) {
|
|
2877
2875
|
this.importNodes.push(node);
|
|
2878
2876
|
continue;
|
|
@@ -2882,13 +2880,11 @@ ${getNodeIndent(node)}`));
|
|
|
2882
2880
|
continue;
|
|
2883
2881
|
}
|
|
2884
2882
|
if (ts.isInterfaceDeclaration(node)) {
|
|
2885
|
-
this.DEBUG && console.log(`${node.name.getFullText()} is a type`);
|
|
2886
2883
|
this.types.add(node.name.text);
|
|
2887
2884
|
continue;
|
|
2888
2885
|
}
|
|
2889
2886
|
if (ts.isTypeAliasDeclaration(node)) {
|
|
2890
2887
|
const alias = node.name.text;
|
|
2891
|
-
this.DEBUG && console.log(`${node.name.getFullText()} is a type`);
|
|
2892
2888
|
this.types.add(alias);
|
|
2893
2889
|
continue;
|
|
2894
2890
|
}
|
|
@@ -2896,13 +2892,11 @@ ${getNodeIndent(node)}`));
|
|
|
2896
2892
|
if (ts.isVariableStatement(node)) {
|
|
2897
2893
|
for (const declaration of node.declarationList.declarations) {
|
|
2898
2894
|
if (ts.isIdentifier(declaration.name)) {
|
|
2899
|
-
this.DEBUG && console.log(`${declaration.name.getFullText()} is a value (from var statement)`);
|
|
2900
2895
|
this.values.add(declaration.name.text);
|
|
2901
2896
|
}
|
|
2902
2897
|
}
|
|
2903
2898
|
} else {
|
|
2904
2899
|
if (node.name) {
|
|
2905
|
-
this.DEBUG && console.log(`${node.name.getFullText()} is a value (from declaration)`);
|
|
2906
2900
|
this.values.add(node.name.text);
|
|
2907
2901
|
}
|
|
2908
2902
|
}
|
|
@@ -2914,13 +2908,11 @@ ${getNodeIndent(node)}`));
|
|
|
2914
2908
|
}
|
|
2915
2909
|
if (ts.isModuleDeclaration(node)) {
|
|
2916
2910
|
if (node.name && ts.isIdentifier(node.name)) {
|
|
2917
|
-
this.DEBUG && console.log(`${node.name.getFullText()} is a value (from module declaration)`);
|
|
2918
2911
|
this.values.add(node.name.text);
|
|
2919
2912
|
}
|
|
2920
2913
|
this.analyze(node.getChildren());
|
|
2921
2914
|
continue;
|
|
2922
2915
|
}
|
|
2923
|
-
this.DEBUG && console.log("unhandled statement", node.getFullText(), node.kind);
|
|
2924
2916
|
}
|
|
2925
2917
|
}
|
|
2926
2918
|
// The type-hint statements may lead to redundant import statements.
|
|
@@ -2944,6 +2936,91 @@ function getNodeIndent(node) {
|
|
|
2944
2936
|
const match = node.getFullText().match(/^(?:\n*)([ ]*)/);
|
|
2945
2937
|
return " ".repeat(match?.[1]?.length || 0);
|
|
2946
2938
|
}
|
|
2939
|
+
function getExportSpecifierBinding(element) {
|
|
2940
|
+
if (element.propertyName) {
|
|
2941
|
+
return `${element.propertyName.getText()} as ${element.name.getText()}`;
|
|
2942
|
+
}
|
|
2943
|
+
return element.name.getText();
|
|
2944
|
+
}
|
|
2945
|
+
const RESOLVED_MODULE_PREFIX = "dts-resolved:";
|
|
2946
|
+
const RESOLVED_MODULE_COMMENT = new RegExp(`\\/\\*${RESOLVED_MODULE_PREFIX}(.+?)\\*\\/`);
|
|
2947
|
+
function encodeResolvedModule(absolutePath) {
|
|
2948
|
+
return `/*${RESOLVED_MODULE_PREFIX}${absolutePath}*/`;
|
|
2949
|
+
}
|
|
2950
|
+
function decodeResolvedModule(text) {
|
|
2951
|
+
return text.match(RESOLVED_MODULE_COMMENT)?.[1] ?? null;
|
|
2952
|
+
}
|
|
2953
|
+
function stripResolvedModuleComment(text) {
|
|
2954
|
+
return text.replace(RESOLVED_MODULE_COMMENT, "");
|
|
2955
|
+
}
|
|
2956
|
+
function normalizePath(p) {
|
|
2957
|
+
return p.split("\\").join("/");
|
|
2958
|
+
}
|
|
2959
|
+
class ModuleDeclarationFixer {
|
|
2960
|
+
constructor(chunk, code, sourcemap, moduleToChunk, warn) {
|
|
2961
|
+
this.code = code;
|
|
2962
|
+
this.sourcemap = sourcemap;
|
|
2963
|
+
this.source = parse(chunk.fileName, code.toString());
|
|
2964
|
+
this.chunkFileName = chunk.fileName;
|
|
2965
|
+
this.moduleToChunk = moduleToChunk;
|
|
2966
|
+
this.warn = warn;
|
|
2967
|
+
}
|
|
2968
|
+
fix() {
|
|
2969
|
+
let modified = false;
|
|
2970
|
+
for (const node of this.source.statements) {
|
|
2971
|
+
if (!ts.isModuleDeclaration(node) || !node.body || !ts.isModuleBlock(node.body)) {
|
|
2972
|
+
continue;
|
|
2973
|
+
}
|
|
2974
|
+
const sourceText = this.source.getFullText();
|
|
2975
|
+
const textBetween = sourceText.slice(node.name.getEnd(), node.body.getStart());
|
|
2976
|
+
const absolutePath = decodeResolvedModule(textBetween);
|
|
2977
|
+
if (!absolutePath) {
|
|
2978
|
+
continue;
|
|
2979
|
+
}
|
|
2980
|
+
const targetChunkName = this.getTargetChunkName(absolutePath);
|
|
2981
|
+
const quote = node.name.kind === ts.SyntaxKind.StringLiteral && "singleQuote" in node.name && node.name.singleQuote ? "'" : '"';
|
|
2982
|
+
const cleanedBetween = stripResolvedModuleComment(textBetween);
|
|
2983
|
+
this.code.overwrite(node.name.getStart(), node.body.getStart(), `${quote}${targetChunkName}${quote}${cleanedBetween}`);
|
|
2984
|
+
modified = true;
|
|
2985
|
+
}
|
|
2986
|
+
return {
|
|
2987
|
+
code: this.code.toString(),
|
|
2988
|
+
map: modified && this.sourcemap ? this.code.generateMap() : null
|
|
2989
|
+
};
|
|
2990
|
+
}
|
|
2991
|
+
/**
|
|
2992
|
+
* Get the output chunk name for an absolute module path.
|
|
2993
|
+
*/
|
|
2994
|
+
getTargetChunkName(absolutePath) {
|
|
2995
|
+
const jsExtMatch = absolutePath.match(/\.[cm]?js$/);
|
|
2996
|
+
const basePath = jsExtMatch ? absolutePath.slice(0, -jsExtMatch[0].length) : absolutePath;
|
|
2997
|
+
const extensions = ["", ".d.ts", ".d.mts", ".d.cts", ".ts", ".mts", ".cts", ".js", ".mjs", ".cjs"];
|
|
2998
|
+
const possiblePaths = extensions.map((ext) => basePath + ext);
|
|
2999
|
+
for (const possiblePath of possiblePaths) {
|
|
3000
|
+
const chunkFileName = this.moduleToChunk.get(normalizePath(possiblePath));
|
|
3001
|
+
if (chunkFileName) {
|
|
3002
|
+
return this.formatChunkReference(chunkFileName, jsExtMatch?.[0]);
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
this.warn(`declare module "${absolutePath}" could not be resolved to any output chunk, falling back to current chunk "${this.chunkFileName}"`);
|
|
3006
|
+
return this.formatChunkReference(this.chunkFileName, jsExtMatch?.[0]);
|
|
3007
|
+
}
|
|
3008
|
+
/**
|
|
3009
|
+
* Format a chunk filename as a relative path from the current chunk.
|
|
3010
|
+
*/
|
|
3011
|
+
formatChunkReference(chunkFileName, jsExt) {
|
|
3012
|
+
const chunkDir = path.dirname(this.chunkFileName);
|
|
3013
|
+
let relativePath = normalizePath(path.relative(chunkDir, chunkFileName));
|
|
3014
|
+
relativePath = relativePath.replace(/\.d\.[cm]?tsx?$/, "");
|
|
3015
|
+
if (jsExt) {
|
|
3016
|
+
relativePath += jsExt;
|
|
3017
|
+
}
|
|
3018
|
+
if (!relativePath.startsWith(".")) {
|
|
3019
|
+
relativePath = "./" + relativePath;
|
|
3020
|
+
}
|
|
3021
|
+
return relativePath;
|
|
3022
|
+
}
|
|
3023
|
+
}
|
|
2947
3024
|
function preProcessNamespaceBody(body, code, sourceFile) {
|
|
2948
3025
|
if (ts.isModuleDeclaration(body)) {
|
|
2949
3026
|
if (body.body && (ts.isModuleBlock(body.body) || ts.isModuleDeclaration(body.body))) {
|
|
@@ -3146,6 +3223,13 @@ declare const ${defaultExport}: {`,
|
|
|
3146
3223
|
}
|
|
3147
3224
|
code.remove(start, end);
|
|
3148
3225
|
}
|
|
3226
|
+
const sourceDir = path.dirname(sourceFile.fileName);
|
|
3227
|
+
for (const node of sourceFile.statements) {
|
|
3228
|
+
if (ts.isModuleDeclaration(node) && node.body && ts.isModuleBlock(node.body) && ts.isStringLiteral(node.name) && /^\.\.?\//.test(node.name.text)) {
|
|
3229
|
+
const resolvedPath = path.resolve(sourceDir, node.name.text);
|
|
3230
|
+
code.appendRight(node.name.getEnd(), encodeResolvedModule(resolvedPath));
|
|
3231
|
+
}
|
|
3232
|
+
}
|
|
3149
3233
|
const fullText = sourceFile.getFullText();
|
|
3150
3234
|
const eofTrivia = ts.getLeadingCommentRanges(fullText, sourceFile.endOfFileToken.getFullStart());
|
|
3151
3235
|
const lastStatement = sourceFile.statements[sourceFile.statements.length - 1];
|
|
@@ -3154,7 +3238,7 @@ declare const ${defaultExport}: {`,
|
|
|
3154
3238
|
if (comment.kind !== ts.SyntaxKind.SingleLineCommentTrivia)
|
|
3155
3239
|
continue;
|
|
3156
3240
|
const text = fullText.slice(comment.pos, comment.end);
|
|
3157
|
-
if (
|
|
3241
|
+
if (!/^\/\/[#@]\s*sourceMappingURL=/.test(text))
|
|
3158
3242
|
continue;
|
|
3159
3243
|
let start = comment.pos;
|
|
3160
3244
|
if (start > 0 && fullText[start - 1] === "\n") {
|
|
@@ -3928,40 +4012,6 @@ class Transformer {
|
|
|
3928
4012
|
};
|
|
3929
4013
|
}
|
|
3930
4014
|
}
|
|
3931
|
-
class RelativeModuleDeclarationFixer {
|
|
3932
|
-
constructor(fileName, code, sourcemap, name) {
|
|
3933
|
-
this.sourcemap = sourcemap;
|
|
3934
|
-
this.DEBUG = !!process.env.DTS_EXPORTS_FIXER_DEBUG;
|
|
3935
|
-
this.relativeModuleDeclarations = [];
|
|
3936
|
-
this.source = parse(fileName, code.toString());
|
|
3937
|
-
this.code = code;
|
|
3938
|
-
this.name = name || "./index";
|
|
3939
|
-
}
|
|
3940
|
-
fix() {
|
|
3941
|
-
this.analyze(this.source.statements);
|
|
3942
|
-
for (const node of this.relativeModuleDeclarations) {
|
|
3943
|
-
const start = node.getStart();
|
|
3944
|
-
const end = node.getEnd();
|
|
3945
|
-
const quote = node.name.kind === ts.SyntaxKind.StringLiteral && "singleQuote" in node.name && node.name.singleQuote ? "'" : '"';
|
|
3946
|
-
const code = `declare module ${quote}${this.name}${quote} ${node.body.getText()}`;
|
|
3947
|
-
this.code.overwrite(start, end, code);
|
|
3948
|
-
}
|
|
3949
|
-
return {
|
|
3950
|
-
code: this.code.toString(),
|
|
3951
|
-
map: this.relativeModuleDeclarations.length && this.sourcemap ? this.code.generateMap() : null
|
|
3952
|
-
};
|
|
3953
|
-
}
|
|
3954
|
-
analyze(nodes) {
|
|
3955
|
-
for (const node of nodes) {
|
|
3956
|
-
if (ts.isModuleDeclaration(node) && node.body && ts.isModuleBlock(node.body) && /^\.\.?\//.test(node.name.text)) {
|
|
3957
|
-
if (this.DEBUG) {
|
|
3958
|
-
console.log(`Found relative module declaration: ${node.name.text} in ${this.source.fileName}`);
|
|
3959
|
-
}
|
|
3960
|
-
this.relativeModuleDeclarations.push(node);
|
|
3961
|
-
}
|
|
3962
|
-
}
|
|
3963
|
-
}
|
|
3964
|
-
}
|
|
3965
4015
|
function hydrateSourcemap(sparseMappings, inputMap, outputCode) {
|
|
3966
4016
|
const sparseDecoded = decode(sparseMappings);
|
|
3967
4017
|
const inputDecoded = decode(inputMap.mappings);
|
|
@@ -4042,6 +4092,281 @@ async function loadInputSourcemap(info) {
|
|
|
4042
4092
|
return null;
|
|
4043
4093
|
}
|
|
4044
4094
|
}
|
|
4095
|
+
function rewritePortableSharedChunkImportsInBundle(bundle, warn) {
|
|
4096
|
+
const chunks = Object.values(bundle).filter(isOutputChunk);
|
|
4097
|
+
const sharedChunks = chunks.filter((chunk) => !chunk.isEntry);
|
|
4098
|
+
if (sharedChunks.length === 0)
|
|
4099
|
+
return;
|
|
4100
|
+
const entryChunks = chunks.filter((chunk) => chunk.isEntry);
|
|
4101
|
+
const chunkGraph = new Map(chunks.map((chunk) => [
|
|
4102
|
+
chunk.fileName,
|
|
4103
|
+
{
|
|
4104
|
+
exports: chunk.exports,
|
|
4105
|
+
isEntry: chunk.isEntry
|
|
4106
|
+
}
|
|
4107
|
+
]));
|
|
4108
|
+
const analyses = new Map(chunks.map((chunk) => [chunk.fileName, analyzeChunk(chunk, chunkGraph)]));
|
|
4109
|
+
for (const chunk of entryChunks) {
|
|
4110
|
+
const analysis = analyses.get(chunk.fileName);
|
|
4111
|
+
const magicCode = new MagicString(chunk.code);
|
|
4112
|
+
let hasChanges = false;
|
|
4113
|
+
const unresolvedSymbols = /* @__PURE__ */ new Set();
|
|
4114
|
+
for (const statement of analysis.importStatements) {
|
|
4115
|
+
const sharedChunk = sharedChunks.find((candidate) => getChunkImportSpecifier(chunk.fileName, candidate.fileName) === statement.moduleSpecifier);
|
|
4116
|
+
if (!sharedChunk) {
|
|
4117
|
+
continue;
|
|
4118
|
+
}
|
|
4119
|
+
const keptSpecifiers = [];
|
|
4120
|
+
const rewrittenSpecifiers = /* @__PURE__ */ new Map();
|
|
4121
|
+
for (const specifier of statement.specifiers) {
|
|
4122
|
+
if (hasPublicHostRoute(analysis, statement.moduleSpecifier, specifier.importedName)) {
|
|
4123
|
+
keptSpecifiers.push(specifier);
|
|
4124
|
+
continue;
|
|
4125
|
+
}
|
|
4126
|
+
const hostCandidate = pickHostCandidate(entryChunks, analyses, chunk.fileName, sharedChunk.fileName, specifier);
|
|
4127
|
+
if (!hostCandidate) {
|
|
4128
|
+
keptSpecifiers.push(specifier);
|
|
4129
|
+
unresolvedSymbols.add(specifier.localName);
|
|
4130
|
+
continue;
|
|
4131
|
+
}
|
|
4132
|
+
const hostSpecifier = getChunkImportSpecifier(chunk.fileName, hostCandidate.chunk.fileName);
|
|
4133
|
+
const hostImportSpecifiers = rewrittenSpecifiers.get(hostSpecifier) || [];
|
|
4134
|
+
hostImportSpecifiers.push({
|
|
4135
|
+
importedName: hostCandidate.exportedName,
|
|
4136
|
+
isTypeOnly: specifier.isTypeOnly || hostCandidate.isTypeOnly,
|
|
4137
|
+
localName: specifier.localName
|
|
4138
|
+
});
|
|
4139
|
+
rewrittenSpecifiers.set(hostSpecifier, hostImportSpecifiers);
|
|
4140
|
+
}
|
|
4141
|
+
if (!rewrittenSpecifiers.size) {
|
|
4142
|
+
continue;
|
|
4143
|
+
}
|
|
4144
|
+
const replacementStatements = [];
|
|
4145
|
+
if (keptSpecifiers.length) {
|
|
4146
|
+
replacementStatements.push(buildImportStatement(statement, keptSpecifiers, statement.moduleSpecifier, statement.quote));
|
|
4147
|
+
}
|
|
4148
|
+
const hostSpecifiers = Array.from(rewrittenSpecifiers.keys()).sort(compareStrings);
|
|
4149
|
+
for (const hostSpecifier of hostSpecifiers) {
|
|
4150
|
+
replacementStatements.push(buildImportStatement(statement, rewrittenSpecifiers.get(hostSpecifier), hostSpecifier, statement.quote));
|
|
4151
|
+
}
|
|
4152
|
+
magicCode.overwrite(statement.statement.getStart(), statement.statement.getEnd(), replacementStatements.join("\n"));
|
|
4153
|
+
hasChanges = true;
|
|
4154
|
+
}
|
|
4155
|
+
if (hasChanges) {
|
|
4156
|
+
applyChunkEdits(chunk, magicCode);
|
|
4157
|
+
}
|
|
4158
|
+
if (unresolvedSymbols.size) {
|
|
4159
|
+
warn(formatUnresolvedSharedTypeWarning(chunk.fileName, unresolvedSymbols));
|
|
4160
|
+
}
|
|
4161
|
+
}
|
|
4162
|
+
}
|
|
4163
|
+
function analyzeChunk(chunk, chunkGraph) {
|
|
4164
|
+
const source = parse(chunk.fileName, chunk.code);
|
|
4165
|
+
const importStatements = [];
|
|
4166
|
+
const importedBindings = /* @__PURE__ */ new Map();
|
|
4167
|
+
const hostRoutesByModule = /* @__PURE__ */ new Map();
|
|
4168
|
+
const starExports = [];
|
|
4169
|
+
for (const statement of source.statements) {
|
|
4170
|
+
if (ts.isImportDeclaration(statement) && !statement.importClause?.name && ts.isStringLiteral(statement.moduleSpecifier) && statement.importClause?.namedBindings && ts.isNamedImports(statement.importClause.namedBindings)) {
|
|
4171
|
+
const moduleSpecifier = statement.moduleSpecifier.text;
|
|
4172
|
+
const quote = statement.moduleSpecifier.getText(source).startsWith('"') ? '"' : "'";
|
|
4173
|
+
const specifiers = statement.importClause.namedBindings.elements.map((element) => {
|
|
4174
|
+
const specifier = {
|
|
4175
|
+
importedName: element.propertyName?.text || element.name.text,
|
|
4176
|
+
isTypeOnly: element.isTypeOnly || statement.importClause?.isTypeOnly || false,
|
|
4177
|
+
localName: element.name.text
|
|
4178
|
+
};
|
|
4179
|
+
importedBindings.set(specifier.localName, {
|
|
4180
|
+
importedName: specifier.importedName,
|
|
4181
|
+
isTypeOnly: specifier.isTypeOnly,
|
|
4182
|
+
moduleSpecifier
|
|
4183
|
+
});
|
|
4184
|
+
return specifier;
|
|
4185
|
+
});
|
|
4186
|
+
importStatements.push({
|
|
4187
|
+
isTypeOnly: statement.importClause.isTypeOnly,
|
|
4188
|
+
moduleSpecifier,
|
|
4189
|
+
quote,
|
|
4190
|
+
specifiers,
|
|
4191
|
+
statement
|
|
4192
|
+
});
|
|
4193
|
+
continue;
|
|
4194
|
+
}
|
|
4195
|
+
if (!ts.isExportDeclaration(statement) || !statement.moduleSpecifier || !ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
4196
|
+
continue;
|
|
4197
|
+
}
|
|
4198
|
+
if (!statement.exportClause) {
|
|
4199
|
+
starExports.push(statement.moduleSpecifier.text);
|
|
4200
|
+
continue;
|
|
4201
|
+
}
|
|
4202
|
+
if (!ts.isNamedExports(statement.exportClause)) {
|
|
4203
|
+
continue;
|
|
4204
|
+
}
|
|
4205
|
+
for (const element of statement.exportClause.elements) {
|
|
4206
|
+
addHostExportRoute(hostRoutesByModule, statement.moduleSpecifier.text, element.propertyName?.text || element.name.text, {
|
|
4207
|
+
exportedName: element.name.text,
|
|
4208
|
+
isTypeOnly: statement.isTypeOnly || element.isTypeOnly
|
|
4209
|
+
});
|
|
4210
|
+
}
|
|
4211
|
+
}
|
|
4212
|
+
for (const statement of source.statements) {
|
|
4213
|
+
if (!ts.isExportDeclaration(statement) || statement.moduleSpecifier || !statement.exportClause || !ts.isNamedExports(statement.exportClause)) {
|
|
4214
|
+
continue;
|
|
4215
|
+
}
|
|
4216
|
+
for (const element of statement.exportClause.elements) {
|
|
4217
|
+
const localName = element.propertyName?.text || element.name.text;
|
|
4218
|
+
const binding = importedBindings.get(localName);
|
|
4219
|
+
if (!binding) {
|
|
4220
|
+
continue;
|
|
4221
|
+
}
|
|
4222
|
+
addHostExportRoute(hostRoutesByModule, binding.moduleSpecifier, binding.importedName, {
|
|
4223
|
+
exportedName: element.name.text,
|
|
4224
|
+
isTypeOnly: binding.isTypeOnly || statement.isTypeOnly || element.isTypeOnly
|
|
4225
|
+
});
|
|
4226
|
+
}
|
|
4227
|
+
}
|
|
4228
|
+
for (const moduleSpecifier of starExports) {
|
|
4229
|
+
const sharedChunk = Array.from(chunkGraph.entries()).find(([candidateFileName, candidate]) => !candidate.isEntry && getChunkImportSpecifier(chunk.fileName, candidateFileName) === moduleSpecifier);
|
|
4230
|
+
if (!sharedChunk) {
|
|
4231
|
+
continue;
|
|
4232
|
+
}
|
|
4233
|
+
for (const exportedName of sharedChunk[1].exports) {
|
|
4234
|
+
if (exportedName === "default") {
|
|
4235
|
+
continue;
|
|
4236
|
+
}
|
|
4237
|
+
addHostExportRoute(hostRoutesByModule, moduleSpecifier, exportedName, {
|
|
4238
|
+
exportedName,
|
|
4239
|
+
isTypeOnly: false
|
|
4240
|
+
});
|
|
4241
|
+
}
|
|
4242
|
+
}
|
|
4243
|
+
return {
|
|
4244
|
+
hostRoutesByModule,
|
|
4245
|
+
importStatements
|
|
4246
|
+
};
|
|
4247
|
+
}
|
|
4248
|
+
const isOutputChunk = (chunk) => chunk.type === "chunk";
|
|
4249
|
+
function pickHostCandidate(entryChunks, analyses, currentChunkFileName, sharedChunkFileName, specifier) {
|
|
4250
|
+
const candidates = [];
|
|
4251
|
+
for (const hostChunk of entryChunks) {
|
|
4252
|
+
if (hostChunk.fileName === currentChunkFileName) {
|
|
4253
|
+
continue;
|
|
4254
|
+
}
|
|
4255
|
+
const hostAnalysis = analyses.get(hostChunk.fileName);
|
|
4256
|
+
const sharedSpecifier = getChunkImportSpecifier(hostChunk.fileName, sharedChunkFileName);
|
|
4257
|
+
const routes = getHostExportRoutes(hostAnalysis, sharedSpecifier, specifier.importedName);
|
|
4258
|
+
for (const route of routes) {
|
|
4259
|
+
candidates.push({
|
|
4260
|
+
...route,
|
|
4261
|
+
chunk: hostChunk
|
|
4262
|
+
});
|
|
4263
|
+
}
|
|
4264
|
+
}
|
|
4265
|
+
candidates.sort((left, right) => {
|
|
4266
|
+
const leftMatchesLocal = left.exportedName === specifier.localName ? 0 : 1;
|
|
4267
|
+
const rightMatchesLocal = right.exportedName === specifier.localName ? 0 : 1;
|
|
4268
|
+
if (leftMatchesLocal !== rightMatchesLocal) {
|
|
4269
|
+
return leftMatchesLocal - rightMatchesLocal;
|
|
4270
|
+
}
|
|
4271
|
+
const fileNameOrder = compareStrings(left.chunk.fileName, right.chunk.fileName);
|
|
4272
|
+
if (fileNameOrder !== 0) {
|
|
4273
|
+
return fileNameOrder;
|
|
4274
|
+
}
|
|
4275
|
+
return compareStrings(left.exportedName, right.exportedName);
|
|
4276
|
+
});
|
|
4277
|
+
return candidates[0];
|
|
4278
|
+
}
|
|
4279
|
+
const getHostExportRoutes = (analysis, moduleSpecifier, importedName) => analysis.hostRoutesByModule.get(moduleSpecifier)?.get(importedName) || [];
|
|
4280
|
+
const hasPublicHostRoute = (analysis, moduleSpecifier, importedName) => getHostExportRoutes(analysis, moduleSpecifier, importedName).length > 0;
|
|
4281
|
+
const compareStrings = (left, right) => {
|
|
4282
|
+
if (left < right) {
|
|
4283
|
+
return -1;
|
|
4284
|
+
}
|
|
4285
|
+
if (left > right) {
|
|
4286
|
+
return 1;
|
|
4287
|
+
}
|
|
4288
|
+
return 0;
|
|
4289
|
+
};
|
|
4290
|
+
function addHostExportRoute(hostRoutesByModule, moduleSpecifier, importedName, route) {
|
|
4291
|
+
const routesByImportedName = hostRoutesByModule.get(moduleSpecifier) || /* @__PURE__ */ new Map();
|
|
4292
|
+
const routes = routesByImportedName.get(importedName) || [];
|
|
4293
|
+
if (!routes.some((existing) => existing.exportedName === route.exportedName && existing.isTypeOnly === route.isTypeOnly)) {
|
|
4294
|
+
routes.push(route);
|
|
4295
|
+
}
|
|
4296
|
+
routesByImportedName.set(importedName, routes);
|
|
4297
|
+
hostRoutesByModule.set(moduleSpecifier, routesByImportedName);
|
|
4298
|
+
}
|
|
4299
|
+
function buildImportStatement(statement, specifiers, moduleSpecifier, quote) {
|
|
4300
|
+
const useTypeOnlyImport = statement.isTypeOnly || specifiers.every((specifier) => specifier.isTypeOnly);
|
|
4301
|
+
const importKeyword = useTypeOnlyImport ? "import type" : "import";
|
|
4302
|
+
const namedImports = specifiers.map((specifier) => {
|
|
4303
|
+
const prefix = !useTypeOnlyImport && specifier.isTypeOnly ? "type " : "";
|
|
4304
|
+
if (specifier.importedName === specifier.localName) {
|
|
4305
|
+
return `${prefix}${specifier.importedName}`;
|
|
4306
|
+
}
|
|
4307
|
+
return `${prefix}${specifier.importedName} as ${specifier.localName}`;
|
|
4308
|
+
}).join(", ");
|
|
4309
|
+
return `${importKeyword} { ${namedImports} } from ${quote}${moduleSpecifier}${quote};`;
|
|
4310
|
+
}
|
|
4311
|
+
function applyChunkEdits(chunk, code) {
|
|
4312
|
+
const nextCode = code.toString();
|
|
4313
|
+
if (nextCode === chunk.code) {
|
|
4314
|
+
return;
|
|
4315
|
+
}
|
|
4316
|
+
if (chunk.map) {
|
|
4317
|
+
const chunkFileName = normalizeChunkPath(chunk.fileName);
|
|
4318
|
+
const rewriteMap = code.generateMap({
|
|
4319
|
+
file: chunkFileName,
|
|
4320
|
+
hires: true,
|
|
4321
|
+
includeContent: true,
|
|
4322
|
+
source: chunkFileName
|
|
4323
|
+
});
|
|
4324
|
+
const remapped = remapping(rewriteMap, (file) => {
|
|
4325
|
+
if (normalizeChunkPath(file) === chunkFileName) {
|
|
4326
|
+
return chunk.map;
|
|
4327
|
+
}
|
|
4328
|
+
return null;
|
|
4329
|
+
});
|
|
4330
|
+
chunk.map = {
|
|
4331
|
+
...chunk.map,
|
|
4332
|
+
mappings: typeof remapped.mappings === "string" ? remapped.mappings : "",
|
|
4333
|
+
names: remapped.names || [],
|
|
4334
|
+
sources: remapped.sources
|
|
4335
|
+
};
|
|
4336
|
+
delete chunk.map.sourcesContent;
|
|
4337
|
+
}
|
|
4338
|
+
chunk.code = nextCode;
|
|
4339
|
+
}
|
|
4340
|
+
const formatUnresolvedSharedTypeWarning = (chunkFileName, symbols) => {
|
|
4341
|
+
const symbolList = Array.from(symbols).sort().join(", ");
|
|
4342
|
+
return [
|
|
4343
|
+
`Entry "${chunkFileName}" still references private shared type exports with no public re-export: ${symbolList}.`,
|
|
4344
|
+
"rollup-plugin-dts will not invent new public exports for these types.",
|
|
4345
|
+
"Re-export them from a public entry to avoid downstream TS2742 errors."
|
|
4346
|
+
].join(" ");
|
|
4347
|
+
};
|
|
4348
|
+
const getChunkImportSpecifier = (fromChunkFileName, toChunkFileName) => {
|
|
4349
|
+
const fromDir = path.posix.dirname(normalizeChunkPath(fromChunkFileName));
|
|
4350
|
+
const toRuntimeFileName = getChunkRuntimeFileName(normalizeChunkPath(toChunkFileName));
|
|
4351
|
+
let relativePath = path.posix.relative(fromDir, toRuntimeFileName);
|
|
4352
|
+
if (!relativePath.startsWith(".")) {
|
|
4353
|
+
relativePath = `./${relativePath}`;
|
|
4354
|
+
}
|
|
4355
|
+
return relativePath;
|
|
4356
|
+
};
|
|
4357
|
+
const getChunkRuntimeFileName = (fileName) => {
|
|
4358
|
+
if (fileName.endsWith(".d.mts")) {
|
|
4359
|
+
return `${fileName.slice(0, -6)}.mjs`;
|
|
4360
|
+
}
|
|
4361
|
+
if (fileName.endsWith(".d.cts")) {
|
|
4362
|
+
return `${fileName.slice(0, -6)}.cjs`;
|
|
4363
|
+
}
|
|
4364
|
+
if (fileName.endsWith(".d.ts")) {
|
|
4365
|
+
return `${fileName.slice(0, -5)}.js`;
|
|
4366
|
+
}
|
|
4367
|
+
return `${fileName}.js`;
|
|
4368
|
+
};
|
|
4369
|
+
const normalizeChunkPath = (fileName) => fileName.replaceAll("\\", "/");
|
|
4045
4370
|
const transform = (enableSourcemap) => {
|
|
4046
4371
|
const allTypeReferences = /* @__PURE__ */ new Map();
|
|
4047
4372
|
const allFileReferences = /* @__PURE__ */ new Map();
|
|
@@ -4087,12 +4412,13 @@ const transform = (enableSourcemap) => {
|
|
|
4087
4412
|
strict: false
|
|
4088
4413
|
};
|
|
4089
4414
|
},
|
|
4090
|
-
transform(code, fileName,
|
|
4415
|
+
transform(code, fileName, inputMapTextOrOptions) {
|
|
4091
4416
|
const name = trimExtension(fileName);
|
|
4092
4417
|
const moduleIds = this.getModuleIds();
|
|
4093
4418
|
const moduleId = Array.from(moduleIds).find((id) => trimExtension(id) === name);
|
|
4094
4419
|
const isEntry = Boolean(moduleId && this.getModuleInfo(moduleId)?.isEntry);
|
|
4095
4420
|
const isJSON = Boolean(moduleId && JSON_EXTENSIONS.test(moduleId));
|
|
4421
|
+
const inputMapText = typeof inputMapTextOrOptions === "string" ? inputMapTextOrOptions : void 0;
|
|
4096
4422
|
const rawCode = code;
|
|
4097
4423
|
let sourceFile = parse(fileName, code);
|
|
4098
4424
|
const preprocessed = preProcess({ sourceFile, isEntry, isJSON });
|
|
@@ -4107,7 +4433,7 @@ const transform = (enableSourcemap) => {
|
|
|
4107
4433
|
console.log(JSON.stringify(converted.ast.body, void 0, 2));
|
|
4108
4434
|
}
|
|
4109
4435
|
if (!enableSourcemap) {
|
|
4110
|
-
return { code, ast: converted.ast };
|
|
4436
|
+
return { code, ast: converted.ast, map: null };
|
|
4111
4437
|
}
|
|
4112
4438
|
const map = preprocessed.code.generateMap({ hires: true, source: fileName });
|
|
4113
4439
|
if (DTS_EXTENSIONS.test(fileName)) {
|
|
@@ -4119,7 +4445,7 @@ const transform = (enableSourcemap) => {
|
|
|
4119
4445
|
}
|
|
4120
4446
|
return { code, ast: converted.ast, map };
|
|
4121
4447
|
},
|
|
4122
|
-
renderChunk(inputCode, chunk, options) {
|
|
4448
|
+
renderChunk(inputCode, chunk, options, meta) {
|
|
4123
4449
|
const source = parse(chunk.fileName, inputCode);
|
|
4124
4450
|
const fixer = new NamespaceFixer(source);
|
|
4125
4451
|
const typeReferences = /* @__PURE__ */ new Set();
|
|
@@ -4150,10 +4476,17 @@ const transform = (enableSourcemap) => {
|
|
|
4150
4476
|
}
|
|
4151
4477
|
const typeOnlyFixer = new TypeOnlyFixer(chunk.fileName, code);
|
|
4152
4478
|
const typesFixed = typeOnlyFixer.fix();
|
|
4153
|
-
const
|
|
4154
|
-
|
|
4479
|
+
const moduleToChunk = /* @__PURE__ */ new Map();
|
|
4480
|
+
for (const [chunkFileName, chunkInfo] of Object.entries(meta.chunks)) {
|
|
4481
|
+
for (const moduleId of Object.keys(chunkInfo.modules)) {
|
|
4482
|
+
moduleToChunk.set(moduleId.split("\\").join("/"), chunkFileName);
|
|
4483
|
+
}
|
|
4484
|
+
}
|
|
4485
|
+
const moduleDeclarationFixer = new ModuleDeclarationFixer(chunk, "magicCode" in typesFixed && typesFixed.magicCode ? typesFixed.magicCode : new MagicString(code), !!options.sourcemap, moduleToChunk, (message) => this.warn(message));
|
|
4486
|
+
return moduleDeclarationFixer.fix();
|
|
4155
4487
|
},
|
|
4156
4488
|
async generateBundle(options, bundle) {
|
|
4489
|
+
rewritePortableSharedChunkImportsInBundle(bundle, (message) => this.warn(message));
|
|
4157
4490
|
if (!options.sourcemap)
|
|
4158
4491
|
return;
|
|
4159
4492
|
const inputSourcemaps = /* @__PURE__ */ new Map();
|
|
@@ -4454,6 +4787,9 @@ const plugin = (options = {}) => {
|
|
|
4454
4787
|
resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig, ctx.resolvedOptions.sourcemap).compilerOptions;
|
|
4455
4788
|
}
|
|
4456
4789
|
const { resolvedModule } = ts.resolveModuleName(source, importer, {
|
|
4790
|
+
// Default moduleResolution to node10 if not explicitly set. TS6 changed
|
|
4791
|
+
// the default from node10 to bundler, and bundler resolution picks .js
|
|
4792
|
+
// files over directories with .d.ts index files, breaking .d.ts bundling.
|
|
4457
4793
|
moduleResolution: ts.ModuleResolutionKind.Node10,
|
|
4458
4794
|
...resolvedCompilerOptions
|
|
4459
4795
|
}, ts.sys);
|
|
@@ -4635,6 +4971,7 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
|
|
|
4635
4971
|
getPackageEntryPoint
|
|
4636
4972
|
} = createExternalizePlugin(externals);
|
|
4637
4973
|
const sizeRef = {};
|
|
4974
|
+
const warnings = [];
|
|
4638
4975
|
const rollupConfig = {
|
|
4639
4976
|
input: createInputMap(input, outputDirectory),
|
|
4640
4977
|
output: {
|
|
@@ -4643,6 +4980,9 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
|
|
|
4643
4980
|
entryFileNames: "[name]",
|
|
4644
4981
|
chunkFileNames: "_dtsroll-chunks/[hash]-[name].ts"
|
|
4645
4982
|
},
|
|
4983
|
+
onwarn(warning) {
|
|
4984
|
+
warnings.push(String(warning.message || warning));
|
|
4985
|
+
},
|
|
4646
4986
|
plugins: [
|
|
4647
4987
|
importTrace(),
|
|
4648
4988
|
externalizePlugin,
|
|
@@ -4665,7 +5005,8 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
|
|
|
4665
5005
|
built,
|
|
4666
5006
|
externalized,
|
|
4667
5007
|
getPackageEntryPoint,
|
|
4668
|
-
sourceSize: sizeRef.value ?? 0
|
|
5008
|
+
sourceSize: sizeRef.value ?? 0,
|
|
5009
|
+
warnings
|
|
4669
5010
|
};
|
|
4670
5011
|
};
|
|
4671
5012
|
|
|
@@ -4704,7 +5045,8 @@ const dtsroll = async ({
|
|
|
4704
5045
|
built,
|
|
4705
5046
|
externalized,
|
|
4706
5047
|
getPackageEntryPoint,
|
|
4707
|
-
sourceSize
|
|
5048
|
+
sourceSize,
|
|
5049
|
+
warnings
|
|
4708
5050
|
} = await build(
|
|
4709
5051
|
inputFiles,
|
|
4710
5052
|
outputDirectory,
|
|
@@ -4759,7 +5101,8 @@ const dtsroll = async ({
|
|
|
4759
5101
|
input: sourceSize,
|
|
4760
5102
|
output: outputSize
|
|
4761
5103
|
},
|
|
4762
|
-
externals: externalPackages
|
|
5104
|
+
externals: externalPackages,
|
|
5105
|
+
warnings
|
|
4763
5106
|
};
|
|
4764
5107
|
};
|
|
4765
5108
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
package/dist/vite.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dtsroll",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "Bundle dts files",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bundle",
|
|
@@ -31,8 +31,6 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"imports": {
|
|
34
|
-
"#dtsroll": "./src/index.ts",
|
|
35
|
-
"#dtsroll/vite": "./src/vite.ts",
|
|
36
34
|
"typescript": {
|
|
37
35
|
"typescript4": "typescript4",
|
|
38
36
|
"typescript5": "typescript5",
|
|
@@ -46,16 +44,16 @@
|
|
|
46
44
|
"dependencies": {
|
|
47
45
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
48
46
|
"byte-size": "^9.0.1",
|
|
49
|
-
"cleye": "^2.
|
|
47
|
+
"cleye": "^2.6.0",
|
|
50
48
|
"convert-source-map": "^2.0.0",
|
|
51
49
|
"empathic": "^2.0.0",
|
|
52
50
|
"resolve-pkg-maps": "^1.0.0",
|
|
53
|
-
"rollup": "^4.
|
|
51
|
+
"rollup": "^4.60.2",
|
|
54
52
|
"rollup-plugin-import-trace": "^1.0.1"
|
|
55
53
|
},
|
|
56
54
|
"peerDependencies": {
|
|
57
55
|
"typescript": "^4.5 || ^5.0 || ^6.0",
|
|
58
|
-
"vite": "5 || 6 || 7"
|
|
56
|
+
"vite": "5 || 6 || 7 || 8"
|
|
59
57
|
},
|
|
60
58
|
"peerDependenciesMeta": {
|
|
61
59
|
"vite": {
|