detype 2.0.3 → 2.0.4
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/detype.js +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +104 -230
- package/dist/index.cjs +199 -254
- package/dist/index.d.cts +34 -33
- package/dist/index.d.ts +34 -33
- package/dist/index.js +3 -12
- package/dist/transformFile-CkCVsmzB.js +193 -0
- package/package.json +11 -8
- package/dist/chunk-VYED23Y7.js +0 -250
package/dist/index.cjs
CHANGED
|
@@ -1,281 +1,226 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
9
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
19
22
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
+
value: mod,
|
|
25
|
+
enumerable: true
|
|
26
|
+
}) : target, mod));
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
//#endregion
|
|
29
|
+
let _babel_core = require("@babel/core");
|
|
30
|
+
let prettier = require("prettier");
|
|
31
|
+
let _vuedx_compiler_sfc = require("@vuedx/compiler-sfc");
|
|
32
|
+
let _vue_compiler_sfc = require("@vue/compiler-sfc");
|
|
33
|
+
let _vuedx_template_ast_types = require("@vuedx/template-ast-types");
|
|
34
|
+
let _babel_preset_typescript = require("@babel/preset-typescript");
|
|
35
|
+
_babel_preset_typescript = __toESM(_babel_preset_typescript);
|
|
36
|
+
let node_fs = require("node:fs");
|
|
37
|
+
node_fs = __toESM(node_fs);
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
var import_core = require("@babel/core");
|
|
42
|
-
var import_prettier = require("prettier");
|
|
43
|
-
var import_compiler_sfc = require("@vuedx/compiler-sfc");
|
|
44
|
-
var import_compiler_sfc2 = require("@vue/compiler-sfc");
|
|
45
|
-
var import_template_ast_types = require("@vuedx/template-ast-types");
|
|
46
|
-
var import_preset_typescript = __toESM(require("@babel/preset-typescript"), 1);
|
|
39
|
+
//#region src/transform.ts
|
|
47
40
|
function getDefinePropsObject(content) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return content.substring(startContentIndex, endContentIndex);
|
|
62
|
-
}
|
|
63
|
-
return "";
|
|
41
|
+
const matched = /\sprops:\s*\{/m.exec(content);
|
|
42
|
+
if (matched) {
|
|
43
|
+
const startContentIndex = matched.index + matched[0].length - 1;
|
|
44
|
+
let leftBracketCount = 1;
|
|
45
|
+
let endContentIndex = startContentIndex + 1;
|
|
46
|
+
while (leftBracketCount) {
|
|
47
|
+
if (content.charAt(endContentIndex) === "{") leftBracketCount++;
|
|
48
|
+
else if (content.charAt(endContentIndex) === "}") leftBracketCount--;
|
|
49
|
+
endContentIndex++;
|
|
50
|
+
}
|
|
51
|
+
return content.substring(startContentIndex, endContentIndex);
|
|
52
|
+
}
|
|
53
|
+
return "";
|
|
64
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Transform TypeScript code into vanilla JavaScript without affecting the formatting
|
|
57
|
+
* @param code Source coude
|
|
58
|
+
* @param fileName File name for the source
|
|
59
|
+
* @param options Options
|
|
60
|
+
*/
|
|
65
61
|
async function transform(code, fileName, options = {}) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
code = await removeTypesFromVueSfcScript(
|
|
95
|
-
code,
|
|
96
|
-
fileName,
|
|
97
|
-
script1,
|
|
98
|
-
parsedVue.descriptor.template?.ast,
|
|
99
|
-
removeTypeOptions
|
|
100
|
-
);
|
|
101
|
-
code = await removeTypesFromVueSfcScript(
|
|
102
|
-
code,
|
|
103
|
-
fileName,
|
|
104
|
-
script2,
|
|
105
|
-
parsedVue.descriptor.template?.ast,
|
|
106
|
-
removeTypeOptions
|
|
107
|
-
);
|
|
108
|
-
} else {
|
|
109
|
-
code = await removeTypes(code, fileName, removeTypeOptions);
|
|
110
|
-
}
|
|
111
|
-
if (propsContent) {
|
|
112
|
-
code = code.replace("defineProps(", (str) => `${str}${propsContent}`);
|
|
113
|
-
}
|
|
114
|
-
if (emitsContent) {
|
|
115
|
-
code = code.replace("defineEmits(", (str) => `${str}${emitsContent}`);
|
|
116
|
-
}
|
|
117
|
-
code = await (0, import_prettier.format)(code, {
|
|
118
|
-
...prettierOptions,
|
|
119
|
-
filepath: originalFileName
|
|
120
|
-
});
|
|
121
|
-
return code;
|
|
62
|
+
const { prettierOptions, ...removeTypeOptions } = options;
|
|
63
|
+
const originalCode = code;
|
|
64
|
+
const originalFileName = fileName;
|
|
65
|
+
let propsContent = "";
|
|
66
|
+
let emitsContent = "";
|
|
67
|
+
code = code.replaceAll("\r\n", "\n");
|
|
68
|
+
if (fileName.endsWith(".vue")) {
|
|
69
|
+
const parsedVue = (0, _vuedx_compiler_sfc.parse)(code);
|
|
70
|
+
if (parsedVue.descriptor.script?.lang !== "ts" && parsedVue.descriptor.scriptSetup?.lang !== "ts") return originalCode;
|
|
71
|
+
let { script: script1, scriptSetup: script2 } = parsedVue.descriptor;
|
|
72
|
+
const isContainsDefinePropsType = script2?.content.match(/defineProps\s*</m);
|
|
73
|
+
const isContainsDefineEmitType = script2?.content.match(/defineEmits\s*</m);
|
|
74
|
+
if (isContainsDefinePropsType || isContainsDefineEmitType) {
|
|
75
|
+
const { content } = (0, _vue_compiler_sfc.compileScript)(parsedVue.descriptor, { id: "xxxxxxx" });
|
|
76
|
+
if (isContainsDefinePropsType) propsContent = getDefinePropsObject(content);
|
|
77
|
+
if (isContainsDefineEmitType) emitsContent = content.match(/\semits:\s(\[.*\]?)/m)?.[1] || "";
|
|
78
|
+
}
|
|
79
|
+
if (script1 && script2 && script1.loc.start.offset < script2.loc.start.offset) [script2, script1] = [script1, script2];
|
|
80
|
+
code = await removeTypesFromVueSfcScript(code, fileName, script1, parsedVue.descriptor.template?.ast, removeTypeOptions);
|
|
81
|
+
code = await removeTypesFromVueSfcScript(code, fileName, script2, parsedVue.descriptor.template?.ast, removeTypeOptions);
|
|
82
|
+
} else code = await removeTypes(code, fileName, removeTypeOptions);
|
|
83
|
+
if (propsContent) code = code.replace("defineProps(", (str) => `${str}${propsContent}`);
|
|
84
|
+
if (emitsContent) code = code.replace("defineEmits(", (str) => `${str}${emitsContent}`);
|
|
85
|
+
code = await (0, prettier.format)(code, {
|
|
86
|
+
...prettierOptions,
|
|
87
|
+
filepath: originalFileName
|
|
88
|
+
});
|
|
89
|
+
return code;
|
|
122
90
|
}
|
|
123
91
|
async function removeTypes(code, fileName, options) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
TSDeclareMethod: removeComments,
|
|
155
|
-
TSImportType: removeComments
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
].filter(Boolean),
|
|
159
|
-
presets: [import_preset_typescript.default],
|
|
160
|
-
generatorOpts: {
|
|
161
|
-
shouldPrintComment: (comment) => comment !== "@detype: remove-me" && (!options.removeTsComments || !comment.match(/^\s*(@ts-ignore|@ts-expect-error)/))
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
if (options.customizeBabelConfig) {
|
|
165
|
-
options.customizeBabelConfig(babelConfig);
|
|
166
|
-
}
|
|
167
|
-
const babelOutput = await (0, import_core.transformAsync)(code, babelConfig);
|
|
168
|
-
if (!babelOutput || babelOutput.code === void 0 || babelOutput.code === null) {
|
|
169
|
-
throw new Error("Babel error");
|
|
170
|
-
}
|
|
171
|
-
return babelOutput.code.replaceAll(/\n\n*/g, "\n").replace(
|
|
172
|
-
/\/\* @detype: empty-line=([0-9]+) \*\//g,
|
|
173
|
-
(_match, p1) => `
|
|
174
|
-
`.repeat(p1 - 2)
|
|
175
|
-
);
|
|
92
|
+
code = code.replace(/\n\n+/g, (match) => `\n/* @detype: empty-line=${match.length} */\n`);
|
|
93
|
+
code = processMagicComments(code);
|
|
94
|
+
const removeComments = { enter(p) {
|
|
95
|
+
if (!p.node.leadingComments) return;
|
|
96
|
+
for (let i = p.node.leadingComments.length - 1; i >= 0; i--) {
|
|
97
|
+
const comment = p.node.leadingComments[i];
|
|
98
|
+
if (code.slice(comment.end).match(/^\s*\n\s*\n/) || comment.value.includes("@detype: empty-line")) break;
|
|
99
|
+
comment.value = "@detype: remove-me";
|
|
100
|
+
}
|
|
101
|
+
} };
|
|
102
|
+
const babelConfig = {
|
|
103
|
+
filename: fileName,
|
|
104
|
+
retainLines: true,
|
|
105
|
+
plugins: [{
|
|
106
|
+
name: "detype-comment-remover",
|
|
107
|
+
visitor: {
|
|
108
|
+
TSTypeAliasDeclaration: removeComments,
|
|
109
|
+
TSInterfaceDeclaration: removeComments,
|
|
110
|
+
TSDeclareFunction: removeComments,
|
|
111
|
+
TSDeclareMethod: removeComments,
|
|
112
|
+
TSImportType: removeComments
|
|
113
|
+
}
|
|
114
|
+
}].filter(Boolean),
|
|
115
|
+
presets: [_babel_preset_typescript.default],
|
|
116
|
+
generatorOpts: { shouldPrintComment: (comment) => comment !== "@detype: remove-me" && (!options.removeTsComments || !comment.match(/^\s*(@ts-ignore|@ts-expect-error)/)) }
|
|
117
|
+
};
|
|
118
|
+
if (options.customizeBabelConfig) options.customizeBabelConfig(babelConfig);
|
|
119
|
+
const babelOutput = await (0, _babel_core.transformAsync)(code, babelConfig);
|
|
120
|
+
if (!babelOutput || babelOutput.code === void 0 || babelOutput.code === null) throw new Error("Babel error");
|
|
121
|
+
return babelOutput.code.replaceAll(/\n\n*/g, "\n").replace(/\/\* @detype: empty-line=([0-9]+) \*\//g, (_match, p1) => `\n`.repeat(p1 - 2));
|
|
176
122
|
}
|
|
177
123
|
async function removeTypesFromVueSfcScript(code, fileName, script, templateAst, options) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
let before = code.slice(0, script.loc.start.offset);
|
|
200
|
-
const after = code.slice(script.loc.end.offset);
|
|
201
|
-
const matches = before.match(/\blang\s*=\s*["']ts["']/);
|
|
202
|
-
if (matches) {
|
|
203
|
-
const lastMatch = matches[matches.length - 1];
|
|
204
|
-
const lastMatchIndex = before.lastIndexOf(lastMatch);
|
|
205
|
-
before = before.slice(0, lastMatchIndex) + before.slice(lastMatchIndex + lastMatch.length);
|
|
206
|
-
}
|
|
207
|
-
return before + scriptCode + after;
|
|
124
|
+
if (script === null || script.lang !== "ts") return code;
|
|
125
|
+
if (script.setup && templateAst) {
|
|
126
|
+
const expressions = /* @__PURE__ */ new Set();
|
|
127
|
+
(0, _vuedx_template_ast_types.traverse)(templateAst, { enter(node) {
|
|
128
|
+
if ((0, _vuedx_template_ast_types.isSimpleExpressionNode)(node) && !node.isStatic) expressions.add(`[${node.content}]`);
|
|
129
|
+
else if ((0, _vuedx_template_ast_types.isComponentNode)(node)) expressions.add(`[${node.tag}]`);
|
|
130
|
+
} });
|
|
131
|
+
script.content += "/* @detype: remove-after-this */" + [...expressions].join(";");
|
|
132
|
+
}
|
|
133
|
+
let scriptCode = await removeTypes(script.content, fileName + ".ts", options);
|
|
134
|
+
const removeAfterIndex = scriptCode.indexOf("/* @detype: remove-after-this */");
|
|
135
|
+
if (removeAfterIndex >= 0) scriptCode = scriptCode.slice(0, removeAfterIndex);
|
|
136
|
+
let before = code.slice(0, script.loc.start.offset);
|
|
137
|
+
const after = code.slice(script.loc.end.offset);
|
|
138
|
+
const matches = before.match(/\blang\s*=\s*["']ts["']/);
|
|
139
|
+
if (matches) {
|
|
140
|
+
const lastMatch = matches[matches.length - 1];
|
|
141
|
+
const lastMatchIndex = before.lastIndexOf(lastMatch);
|
|
142
|
+
before = before.slice(0, lastMatchIndex) + before.slice(lastMatchIndex + lastMatch.length);
|
|
143
|
+
}
|
|
144
|
+
return before + scriptCode + after;
|
|
208
145
|
}
|
|
209
146
|
function processMagicComments(input) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
147
|
+
const REPLACE_COMMENT = "// @detype: replace\n";
|
|
148
|
+
const WITH_COMMENT = "// @detype: with\n";
|
|
149
|
+
const END_COMMENT = "// @detype: end\n";
|
|
150
|
+
let start = input.indexOf(REPLACE_COMMENT);
|
|
151
|
+
while (start >= 0) {
|
|
152
|
+
const middle = input.indexOf(WITH_COMMENT, start);
|
|
153
|
+
if (middle < 0) return input;
|
|
154
|
+
const middleEnd = middle + 17;
|
|
155
|
+
const end = input.indexOf(END_COMMENT, middleEnd);
|
|
156
|
+
if (end < 0) return input;
|
|
157
|
+
const endEnd = end + 16;
|
|
158
|
+
const before = input.slice(0, start);
|
|
159
|
+
const newText = input.slice(middleEnd, end).replaceAll(/^\s*\/\//gm, "");
|
|
160
|
+
const after = input.slice(endEnd);
|
|
161
|
+
input = before + newText + after;
|
|
162
|
+
start = input.indexOf(REPLACE_COMMENT, before.length + newText.length);
|
|
163
|
+
}
|
|
164
|
+
return input;
|
|
228
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Removes magic comments without performing the TS to JS transform
|
|
168
|
+
* @param code Source coude
|
|
169
|
+
* @param fileName File name for the source
|
|
170
|
+
* @param prettierOptions Options to pass to prettier
|
|
171
|
+
*/
|
|
229
172
|
async function removeMagicComments(code, fileName, prettierOptions) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
173
|
+
const REPLACE_COMMENT = "// @detype: replace\n";
|
|
174
|
+
const WITH_COMMENT = "// @detype: with\n";
|
|
175
|
+
const END_COMMENT = "// @detype: end\n";
|
|
176
|
+
let start = code.indexOf(REPLACE_COMMENT);
|
|
177
|
+
let startEnd = start + 20;
|
|
178
|
+
while (start >= 0) {
|
|
179
|
+
const middle = code.indexOf(WITH_COMMENT, start);
|
|
180
|
+
if (middle < 0) return code;
|
|
181
|
+
const middleEnd = middle + 17;
|
|
182
|
+
const end = code.indexOf(END_COMMENT, middleEnd);
|
|
183
|
+
if (end < 0) return code;
|
|
184
|
+
const endEnd = end + 16;
|
|
185
|
+
const before = code.slice(0, start);
|
|
186
|
+
const keptText = code.slice(startEnd, middle);
|
|
187
|
+
const after = code.slice(endEnd);
|
|
188
|
+
code = before + keptText + after;
|
|
189
|
+
start = code.indexOf(REPLACE_COMMENT, before.length + keptText.length);
|
|
190
|
+
startEnd = start + 20;
|
|
191
|
+
}
|
|
192
|
+
code = await (0, prettier.format)(code, {
|
|
193
|
+
...prettierOptions,
|
|
194
|
+
filepath: fileName
|
|
195
|
+
});
|
|
196
|
+
return code;
|
|
254
197
|
}
|
|
255
198
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/transformFile.ts
|
|
201
|
+
const { readFile, writeFile } = node_fs.default.promises;
|
|
202
|
+
/**
|
|
203
|
+
* Transform the input file and write the output to another file
|
|
204
|
+
* @param inputFileName
|
|
205
|
+
* @param outputFileName
|
|
206
|
+
*/
|
|
260
207
|
async function transformFile(inputFileName, outputFileName, options = {}) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
...options
|
|
266
|
-
});
|
|
267
|
-
await writeFile(outputFileName, output, "utf-8");
|
|
208
|
+
await writeFile(outputFileName, await transform(await readFile(inputFileName, "utf-8"), inputFileName, {
|
|
209
|
+
prettierOptions: await (0, prettier.resolveConfig)(inputFileName),
|
|
210
|
+
...options
|
|
211
|
+
}), "utf-8");
|
|
268
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Remove magic comments from the input file and write the output to another file
|
|
215
|
+
* @param inputFileName
|
|
216
|
+
* @param outputFileName
|
|
217
|
+
*/
|
|
269
218
|
async function removeMagicCommentsFromFile(inputFileName, outputFileName) {
|
|
270
|
-
|
|
271
|
-
const prettierConfig = await (0, import_prettier2.resolveConfig)(inputFileName);
|
|
272
|
-
const output = await removeMagicComments(code, inputFileName, prettierConfig);
|
|
273
|
-
await writeFile(outputFileName, output, "utf-8");
|
|
219
|
+
await writeFile(outputFileName, await removeMagicComments(await readFile(inputFileName, "utf-8"), inputFileName, await (0, prettier.resolveConfig)(inputFileName)), "utf-8");
|
|
274
220
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
});
|
|
221
|
+
|
|
222
|
+
//#endregion
|
|
223
|
+
exports.removeMagicComments = removeMagicComments;
|
|
224
|
+
exports.removeMagicCommentsFromFile = removeMagicCommentsFromFile;
|
|
225
|
+
exports.transform = transform;
|
|
226
|
+
exports.transformFile = transformFile;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,43 +1,44 @@
|
|
|
1
|
-
import { Options } from
|
|
2
|
-
|
|
3
|
-
import { TransformOptions as TransformOptions$1 } from '@babel/core';
|
|
1
|
+
import { Options as PrettierOptions } from "prettier";
|
|
2
|
+
import { TransformOptions } from "@babel/core";
|
|
4
3
|
|
|
4
|
+
//#region src/transform.d.ts
|
|
5
5
|
interface RemoveTypeOptions {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
/** Whether to remove ts-ignore and ts-expect-error comments */
|
|
7
|
+
removeTsComments?: boolean;
|
|
8
|
+
/** Escape hatch for customizing Babel configuration */
|
|
9
|
+
customizeBabelConfig?(config: TransformOptions): void;
|
|
10
10
|
}
|
|
11
|
-
interface TransformOptions extends RemoveTypeOptions {
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
interface TransformOptions$1 extends RemoveTypeOptions {
|
|
12
|
+
/** Prettier options */
|
|
13
|
+
prettierOptions?: PrettierOptions | null;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
declare function transform(code: string, fileName: string, options?: TransformOptions): Promise<string>;
|
|
16
|
+
* Transform TypeScript code into vanilla JavaScript without affecting the formatting
|
|
17
|
+
* @param code Source coude
|
|
18
|
+
* @param fileName File name for the source
|
|
19
|
+
* @param options Options
|
|
20
|
+
*/
|
|
21
|
+
declare function transform(code: string, fileName: string, options?: TransformOptions$1): Promise<string>;
|
|
22
22
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
declare function removeMagicComments(code: string, fileName: string, prettierOptions?:
|
|
29
|
-
|
|
23
|
+
* Removes magic comments without performing the TS to JS transform
|
|
24
|
+
* @param code Source coude
|
|
25
|
+
* @param fileName File name for the source
|
|
26
|
+
* @param prettierOptions Options to pass to prettier
|
|
27
|
+
*/
|
|
28
|
+
declare function removeMagicComments(code: string, fileName: string, prettierOptions?: PrettierOptions | null): Promise<string>;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/transformFile.d.ts
|
|
30
31
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
* Transform the input file and write the output to another file
|
|
33
|
+
* @param inputFileName
|
|
34
|
+
* @param outputFileName
|
|
35
|
+
*/
|
|
35
36
|
declare function transformFile(inputFileName: string, outputFileName: string, options?: RemoveTypeOptions): Promise<void>;
|
|
36
37
|
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
* Remove magic comments from the input file and write the output to another file
|
|
39
|
+
* @param inputFileName
|
|
40
|
+
* @param outputFileName
|
|
41
|
+
*/
|
|
41
42
|
declare function removeMagicCommentsFromFile(inputFileName: string, outputFileName: string): Promise<void>;
|
|
42
|
-
|
|
43
|
-
export { removeMagicComments, removeMagicCommentsFromFile, transform, transformFile };
|
|
43
|
+
//#endregion
|
|
44
|
+
export { type PrettierOptions, removeMagicComments, removeMagicCommentsFromFile, transform, transformFile };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,43 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { TransformOptions as TransformOptions$1 } from '@babel/core';
|
|
1
|
+
import { TransformOptions } from "@babel/core";
|
|
2
|
+
import { Options as PrettierOptions } from "prettier";
|
|
4
3
|
|
|
4
|
+
//#region src/transform.d.ts
|
|
5
5
|
interface RemoveTypeOptions {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
/** Whether to remove ts-ignore and ts-expect-error comments */
|
|
7
|
+
removeTsComments?: boolean;
|
|
8
|
+
/** Escape hatch for customizing Babel configuration */
|
|
9
|
+
customizeBabelConfig?(config: TransformOptions): void;
|
|
10
10
|
}
|
|
11
|
-
interface TransformOptions extends RemoveTypeOptions {
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
interface TransformOptions$1 extends RemoveTypeOptions {
|
|
12
|
+
/** Prettier options */
|
|
13
|
+
prettierOptions?: PrettierOptions | null;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
declare function transform(code: string, fileName: string, options?: TransformOptions): Promise<string>;
|
|
16
|
+
* Transform TypeScript code into vanilla JavaScript without affecting the formatting
|
|
17
|
+
* @param code Source coude
|
|
18
|
+
* @param fileName File name for the source
|
|
19
|
+
* @param options Options
|
|
20
|
+
*/
|
|
21
|
+
declare function transform(code: string, fileName: string, options?: TransformOptions$1): Promise<string>;
|
|
22
22
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
declare function removeMagicComments(code: string, fileName: string, prettierOptions?:
|
|
29
|
-
|
|
23
|
+
* Removes magic comments without performing the TS to JS transform
|
|
24
|
+
* @param code Source coude
|
|
25
|
+
* @param fileName File name for the source
|
|
26
|
+
* @param prettierOptions Options to pass to prettier
|
|
27
|
+
*/
|
|
28
|
+
declare function removeMagicComments(code: string, fileName: string, prettierOptions?: PrettierOptions | null): Promise<string>;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/transformFile.d.ts
|
|
30
31
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
* Transform the input file and write the output to another file
|
|
33
|
+
* @param inputFileName
|
|
34
|
+
* @param outputFileName
|
|
35
|
+
*/
|
|
35
36
|
declare function transformFile(inputFileName: string, outputFileName: string, options?: RemoveTypeOptions): Promise<void>;
|
|
36
37
|
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
* Remove magic comments from the input file and write the output to another file
|
|
39
|
+
* @param inputFileName
|
|
40
|
+
* @param outputFileName
|
|
41
|
+
*/
|
|
41
42
|
declare function removeMagicCommentsFromFile(inputFileName: string, outputFileName: string): Promise<void>;
|
|
42
|
-
|
|
43
|
-
export { removeMagicComments, removeMagicCommentsFromFile, transform, transformFile };
|
|
43
|
+
//#endregion
|
|
44
|
+
export { type PrettierOptions, removeMagicComments, removeMagicCommentsFromFile, transform, transformFile };
|