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

feat: toVue3 #1

Merged
merged 10 commits into from
Nov 25, 2020
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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
'\\.js$': 'babel-jest',
},
moduleNameMapper: {
'^vue$': 'vue/dist/vue.common',
'vue-2-3': '<rootDir>/src/vue-2-3',
},
};
18 changes: 15 additions & 3 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 @@ -42,6 +42,7 @@
"husky": "^4.3.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.1",
"outdent": "^0.7.1",
"rollup": "^2.33.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-filesize": "^9.0.2",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const rollupConfig = {
name: 'vue23',
exports: 'named',
globals: {
// Check gloabls
vue: 'Vue',
vue3: 'Vue3',
},
},
{
Expand Down
169 changes: 169 additions & 0 deletions src/to-vue-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import Vue from 'vue';
import frag from 'vue-frag';
import {createApp, shallowReactive, h} from 'vue3';
import {vue3ProxyNode, privateState} from './utils';

const camelizeRE = /-(\w)/g;

function normalizeEventName(eventName) {
if (eventName[0] === '&') {
eventName = eventName.slice(1) + 'Passive';
}

if (eventName[0] === '~') {
eventName = eventName.slice(1) + 'Once';
}

if (eventName[0] === '!') {
eventName = eventName.slice(1) + 'Capture';
}

return `on${eventName[0].toUpperCase() + eventName.slice(1).replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))}`;
}

function mergeAttrsListeners({
$attrs,
$listeners,
$vnode,
}) {
const {data} = $vnode;
const attrs = Object.assign({}, $attrs);

if (data.class || data.staticClass) {
attrs.class = [data.class, data.staticClass];
}

if (data.style || data.staticStyle) {
attrs.style = [data.style, data.staticStyle];
}

for (const listener in $listeners) {
attrs[normalizeEventName(listener)] = $listeners[listener];
}

return attrs;
}

const renderVue2Vnode = /* Vue 3 component */ {
props: ['parent', 'vnode'],

created() {
this.state = Vue.observable({
vnode: null,
});
},

mounted() {
const vm = this;
this.vue2App = (new Vue({
beforeCreate() {
this.$parent = vm.parent;
},
directives: {
frag,
},
render: h => h(
'div',
{
directives: [
{name: 'frag'},
],
},
[this.state.vnode()],
),
}));

this.vue2App.$mount(this.$el);
},

beforeUnmount() {
this.vue2App.$destroy();
},

render() {
this.state.vnode = this.vnode;
return h('div');
},
};

function interopSlots(ctx) {
const scopedSlots = {};
for (const slotName in ctx.$scopedSlots) {
scopedSlots[slotName] = () => h(renderVue2Vnode, {
parent: ctx,
vnode: ctx.$scopedSlots[slotName],
});
}

return scopedSlots;
}

function resolveInjection(vm, key) {
let source = vm;
while (source) {
if (source._provided && source._provided[key]) {
return source._provided[key];
}

source = source.$parent;
}
}

const vue2WrapperBase = {
inheritAttrs: false,

created() {
this[privateState] = shallowReactive({
attrs: null,
slots: null,
});
},

provide() {
return {};
},

// Delay until mounted for SSR
mounted() {
const vm = this;
this.v3app = createApp({
render: () => h(this.$options.component, this[privateState].attrs, this[privateState].slots),
mounted() {
vm.v3 = this._.subTree.component.proxy;
},
});

// Proxy provide-inject
this.v3app._context.provides = new Proxy({}, {
has: (_, key) => resolveInjection(this, key),
get: (_, key) => resolveInjection(this, key),
set: (_, key, value) => {
this._provided[key] = value;
return true;
},
});

const {$el} = this;
const root = this.v3app.mount(vue3ProxyNode($el));
this.$el = root.$el;
$el.remove();
},

beforeDestroy() {
this.v3app.unmount();
},

render(h) {
this[privateState].attrs = mergeAttrsListeners(this);
this[privateState].slots = interopSlots(this);
return h('div');
},
};

const toVue2 = vue3Component => {
const vue2Wrapper = Object.create(vue2WrapperBase);
vue2Wrapper.component = vue3Component;
return vue2Wrapper;
};

export default toVue2;
Loading