Skip to content

Commit b52a62f

Browse files
committed
fix: strip types option
1 parent 168d9a6 commit b52a62f

File tree

2 files changed

+70
-55
lines changed

2 files changed

+70
-55
lines changed

.changeset/legal-wasps-yell.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@marko/runtime-tags": patch
3+
"@marko/compiler": patch
4+
"marko": patch
5+
---
6+
7+
Ensure stripTypes compiler option is applied when targeting source output.

packages/compiler/src/babel-plugin/index.js

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ function getMarkoFile(code, fileOpts, markoOpts) {
225225
parseMarko(file);
226226

227227
if (isSource) {
228+
if (markoOpts.stripTypes) {
229+
stripTypes(file);
230+
}
228231
return file;
229232
}
230233

@@ -270,66 +273,15 @@ function getMarkoFile(code, fileOpts, markoOpts) {
270273
}
271274
}
272275

276+
if (markoOpts.stripTypes) {
277+
stripTypes(file);
278+
}
279+
273280
if (isMigrate) {
274281
return file;
275282
}
276283

277284
file.___compileStage = "transform";
278-
if (markoOpts.stripTypes) {
279-
const importScriptlets = new Map();
280-
for (const path of file.path.get("body")) {
281-
if (path.type === "MarkoScriptlet" && path.node.static) {
282-
for (const stmt of path.get("body")) {
283-
if (stmt.isImportDeclaration()) {
284-
// Hoist import declarations from scriptlets
285-
// temporarily so that they will be processed by
286-
// babel typescript transform.
287-
const importNode = stmt.node;
288-
importScriptlets.set(importNode, path.node);
289-
stmt.remove();
290-
path.insertBefore(importNode);
291-
}
292-
}
293-
}
294-
}
295-
296-
traverseAll(file, stripTypesVisitor);
297-
298-
for (const path of file.path.get("body")) {
299-
if (path.type === "ExportNamedDeclaration") {
300-
if (!(path.node.declaration || path.node.specifiers.length)) {
301-
// The babel typescript plugin will add an empty export declaration
302-
// if there are no other imports/exports in the file.
303-
// This is not needed for Marko file outputs since there is always
304-
// a default export.
305-
path.remove();
306-
}
307-
} else if (path.isImportDeclaration()) {
308-
const importNode = path.node;
309-
const scriptlet = importScriptlets.get(importNode);
310-
if (scriptlet) {
311-
let hasTypes = false;
312-
for (const specifier of path.get("specifiers")) {
313-
if (
314-
specifier.node.type === "ImportSpecifier" &&
315-
specifier.node.importKind === "type"
316-
) {
317-
hasTypes = true;
318-
specifier.remove();
319-
}
320-
}
321-
322-
path.remove();
323-
324-
// Add back imports from scriptlets that were
325-
// hoisted for the babel typescript transform.
326-
if (!hasTypes || importNode.specifiers.length) {
327-
scriptlet.body.unshift(importNode);
328-
}
329-
}
330-
}
331-
}
332-
}
333285

334286
const rootTransformers = [];
335287
for (const id in taglibLookup.taglibsById) {
@@ -455,6 +407,62 @@ function addPlugin(meta, arr, plugin) {
455407
}
456408
}
457409

410+
function stripTypes(file) {
411+
const importScriptlets = new Map();
412+
for (const path of file.path.get("body")) {
413+
if (path.type === "MarkoScriptlet" && path.node.static) {
414+
for (const stmt of path.get("body")) {
415+
if (stmt.isImportDeclaration()) {
416+
// Hoist import declarations from scriptlets
417+
// temporarily so that they will be processed by
418+
// babel typescript transform.
419+
const importNode = stmt.node;
420+
importScriptlets.set(importNode, path.node);
421+
stmt.remove();
422+
path.insertBefore(importNode);
423+
}
424+
}
425+
}
426+
}
427+
428+
traverseAll(file, stripTypesVisitor);
429+
430+
for (const path of file.path.get("body")) {
431+
if (path.type === "ExportNamedDeclaration") {
432+
if (!(path.node.declaration || path.node.specifiers.length)) {
433+
// The babel typescript plugin will add an empty export declaration
434+
// if there are no other imports/exports in the file.
435+
// This is not needed for Marko file outputs since there is always
436+
// a default export.
437+
path.remove();
438+
}
439+
} else if (path.isImportDeclaration()) {
440+
const importNode = path.node;
441+
const scriptlet = importScriptlets.get(importNode);
442+
if (scriptlet) {
443+
let hasTypes = false;
444+
for (const specifier of path.get("specifiers")) {
445+
if (
446+
specifier.node.type === "ImportSpecifier" &&
447+
specifier.node.importKind === "type"
448+
) {
449+
hasTypes = true;
450+
specifier.remove();
451+
}
452+
}
453+
454+
path.remove();
455+
456+
// Add back imports from scriptlets that were
457+
// hoisted for the babel typescript transform.
458+
if (!hasTypes || importNode.specifiers.length) {
459+
scriptlet.body.unshift(importNode);
460+
}
461+
}
462+
}
463+
}
464+
}
465+
458466
function isMarkoOutput(output) {
459467
return output === "source" || output === "migrate";
460468
}

0 commit comments

Comments
 (0)