Skip to content

Commit

Permalink
use babel
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyyaaa committed Sep 2, 2024
1 parent df75fa3 commit ce56a67
Show file tree
Hide file tree
Showing 13 changed files with 669 additions and 369 deletions.
85 changes: 79 additions & 6 deletions compiler/plugins/vue.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
*/
module.exports = function vueCompilerPlugin() {
return {
json: {
// Happens after getting json
post: (json) => {
json = fixCleanupRefAst(json);
return json;
},
},
code: {
// Happens before formatting
pre: (codeStr) => {
return [fixCleanupRefIssues].reduce((acc, transform) => {
return [fixVueClassName].reduce((acc, transform) => {
acc = transform(codeStr);
return acc;
}, codeStr);
Expand All @@ -17,9 +24,75 @@ module.exports = function vueCompilerPlugin() {
};
};

function fixCleanupRefIssues(codeStr) {
return codeStr.replace(
/if \(typeof cleanupRef\.value === "function"\) \{\s*cleanupRef\(\);\s*\}/g,
'if (typeof cleanupRef.value === "function") {\n cleanupRef.value();\n }',
);
function fixCleanupRefAst(ast) {
if (ast.hooks && ast.hooks.onUnMount) {
console.log(ast.hooks);

const onUnMountCode = ast.hooks.onUnMount.code;

let updatedCode = onUnMountCode;

if (
onUnMountCode &&
/if\s*\(\s*typeof\s+cleanupRef\.value\s*===\s*["']function["']\s*\)/.test(
onUnMountCode,
)
) {
// Case 1: With braces
const { parse } = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generate = require("@babel/generator").default;
const t = require("@babel/types");

const ast = parse(onUnMountCode, {
sourceType: "module",
plugins: ["typescript"],
});

traverse(ast, {
IfStatement(path) {
const { test, consequent } = path.node;
if (
t.isBinaryExpression(test) &&
t.isUnaryExpression(test.left) &&
t.isMemberExpression(test.left.argument) &&
t.isIdentifier(test.left.argument.object) &&
test.left.argument.object.name === "cleanupRef" &&
t.isIdentifier(test.left.argument.property) &&
test.left.argument.property.name === "value" &&
t.isStringLiteral(test.right) &&
test.right.value === "function"
) {
const newConsequent = t.blockStatement([
t.expressionStatement(
t.callExpression(
t.memberExpression(
t.identifier("cleanupRef"),
t.identifier("value"),
),
[],
),
),
]);
path.node.consequent = newConsequent;
}
},
});

updatedCode = generate(ast).code;
}

ast.hooks.onUnMount.code = updatedCode;
}
return ast;
}

function fixVueClassName(codeStr) {
return codeStr
.replace(/\.className/g, ".class")
.replace(/\bclassName\b/g, "class")
.replace(
/:class="boxStyles\.class"/g,
':class="clsx(boxStyles.class, class)"',
);
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
},
"devDependencies": {
"@babel/core": "^7.24.7",
"@babel/generator": "^7.25.6",
"@babel/parser": "^7.25.6",
"@babel/plugin-proposal-decorators": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@babel/traverse": "^7.25.6",
"@babel/types": "^7.25.6",
"@builder.io/eslint-plugin-mitosis": "^0.0.16",
"@builder.io/mitosis": "0.3.11",
"@builder.io/mitosis-cli": "0.3.11",
Expand Down
55 changes: 0 additions & 55 deletions packages/vue/stories/Button.stories.ts

This file was deleted.

48 changes: 0 additions & 48 deletions packages/vue/stories/Button.vue

This file was deleted.

4 changes: 0 additions & 4 deletions packages/vue/stories/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@
</div>
<div>
<span class="welcome" v-if="user">Welcome, <b>{{ user.name }}</b>!</span>
<my-button size="small" @click="$emit('logout')" label="Log out" v-if="user" />
<my-button size="small" @click="$emit('login')" label="Log in" v-if="!user" />
<my-button primary size="small" @click="$emit('createAccount')" label="Sign up" v-if="!user" />
</div>
</div>
</header>
</template>

<script lang="ts" setup>
import './header.css';
import MyButton from './Button.vue';
defineProps<{ user: { name: string } | null }>();
Expand Down
30 changes: 0 additions & 30 deletions packages/vue/stories/button.css

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from "@storybook/vue3";
import Box from "../../../src/ui/box/box.vue";
import Box from "../../src/ui/box/box.vue";

const meta = {
title: "Core/Box",
Expand Down
Loading

0 comments on commit ce56a67

Please sign in to comment.