Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Vue Virtualizer with @vue/babel-plugin-jsx #374

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default process.env.STORYBOOK_VUE
const { default: vueJsx } = await import("@vitejs/plugin-vue-jsx");

return mergeConfig(config, {
plugins: [vueJsx()],
plugins: [vueJsx({ optimize: true })],
});
},
}
Expand Down
143 changes: 91 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@types/react-window": "1.8.8",
"@typescript-eslint/parser": "6.20.0",
"@vitejs/plugin-vue-jsx": "3.1.0",
"@vue/babel-plugin-jsx": "1.2.1",
"babel-preset-solid": "1.8.12",
"cmdk": "0.2.1",
"concurrently": "8.2.2",
Expand Down
21 changes: 19 additions & 2 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import terser from "@rollup/plugin-terser";
import { babel, getBabelOutputPlugin } from "@rollup/plugin-babel";
import banner from "rollup-plugin-banner2";
import pkg from "./package.json" assert { type: "json" };
import vueVNodePlugin from "./scripts/babel-plugin-annotate-vue-vnode.mjs";

const external = (id) =>
[
Expand All @@ -14,7 +15,12 @@ const terserPlugin = terser({
ecma: 2018,
module: true,
compress: { passes: 5, unsafe: true, keep_fargs: false },
mangle: { properties: { regex: "^_" } },
mangle: {
properties: {
// @vue/babel-plugin-jsx may generate _ field
regex: "^_.+",
},
},
format: {
preserve_annotations: true,
},
Expand Down Expand Up @@ -72,6 +78,18 @@ export default [
outDir: ".",
// declaration: true,
exclude: ["**/*.{spec,stories}.*"],
jsx: "preserve",
}),
babel({
babelrc: false,
configFile: false,
extensions: [".jsx", ".tsx"],
babelHelpers: "bundled",
plugins: [["@vue/babel-plugin-jsx", { optimize: true }]],
parserOpts: { sourceType: "module", plugins: ["jsx", "typescript"] },
}),
getBabelOutputPlugin({
plugins: [vueVNodePlugin],
}),
terserPlugin,
],
Expand Down Expand Up @@ -103,7 +121,6 @@ export default [
babel({
babelrc: false,
configFile: false,
filter: /\.(jsx|tsx)$/,
extensions: [".jsx", ".tsx"],
babelHelpers: "bundled",
presets: ["babel-preset-solid"],
Expand Down
33 changes: 33 additions & 0 deletions scripts/babel-plugin-annotate-vue-vnode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function isCreateVNodeCall(path) {
const calleePath = path.get("callee");
for (const method of ["createVNode"]) {
if (calleePath.referencesImport("vue", method)) {
return true;
}
}
return false;
}

/**
* annotate argument of createVNode for terser
*/
export default ({ types: t }) => {
return {
name: "babel-plugin-annotate-vue-vnode",
visitor: {
CallExpression(path) {
if (isCreateVNodeCall(path)) {
const [type, props, children, patchFlag, dynamicProps] =
path.get("arguments");
if (dynamicProps && t.isArrayExpression(dynamicProps)) {
dynamicProps.get("elements").forEach((e) => {
if (e.node.value.startsWith("_")) {
e.addComment("leading", "#__KEY__", false);
}
});
}
}
},
},
};
};
8 changes: 3 additions & 5 deletions src/vue/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const ListItem = /*#__PURE__*/ defineComponent({
_offset: { type: Number, required: true },
_hide: { type: Boolean },
_isHorizontal: { type: Boolean },
_element: { type: String, required: true },
},
setup(props) {
const elementRef = ref<HTMLDivElement>();
Expand All @@ -38,9 +37,8 @@ export const ListItem = /*#__PURE__*/ defineComponent({
_children: children,
_offset: offset,
_hide: hide,
_element: Element,
_isHorizontal: isHorizontal,
} = props as Omit<typeof props, "_element"> & { _element: "div" };
} = props;

const style: StyleValue = {
margin: 0,
Expand All @@ -57,9 +55,9 @@ export const ListItem = /*#__PURE__*/ defineComponent({
}

return (
<Element ref={elementRef} style={style}>
<div ref={elementRef} style={style}>
{children}
</Element>
</div>
);
};
},
Expand Down
1 change: 0 additions & 1 deletion src/vue/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ export const Virtualizer = /*#__PURE__*/ defineComponent({
_index={i}
_offset={store._getItemOffset(i)}
_hide={store._isUnmeasuredItem(i)}
_element="div"
_children={e}
_isHorizontal={isHorizontal}
/>
Expand Down
1 change: 0 additions & 1 deletion src/vue/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export const WindowVirtualizer = /*#__PURE__*/ defineComponent({
_index={i}
_offset={store._getItemOffset(i)}
_hide={store._isUnmeasuredItem(i)}
_element="div"
_children={e}
_isHorizontal={isHorizontal}
/>
Expand Down
Loading