Skip to content

Commit

Permalink
fix: use wrapper for lazy-loaded functional components (#172)
Browse files Browse the repository at this point in the history
Co-authored-by: pooya parsa <[email protected]>
  • Loading branch information
danielroe and pi0 authored Mar 23, 2021
1 parent 92d7f40 commit 0210066
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
33 changes: 32 additions & 1 deletion templates/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,36 @@

<%= options.getComponents().map(c => {
const exp = c.export === 'default' ? `c.default || c` : `c['${c.export}']`
return `export const Lazy${c.pascalName} = import('../${relativeToBuild(c.filePath)}' /* webpackChunkName: "${c.chunkName}" */).then(c => ${exp})`
return `export const Lazy${c.pascalName} = import('../${relativeToBuild(c.filePath)}' /* webpackChunkName: "${c.chunkName}" */).then(c => wrapFunctional(${exp}))`
}).join('\n') %>

// nuxt/nuxt.js#8607
export function wrapFunctional(options) {
if (!options || !options.functional) {
return options
}

const propKeys = Array.isArray(options.props) ? options.props : Object.keys(options.props || {})

return {
render(h) {
const attrs = {}
const props = {}

for (const key in this.$attrs) {
if (propKeys.includes(key)) {
props[key] = this.$attrs[key]
} else {
attrs[key] = this.$attrs[key]
}
}

return h(options, {
on: this.$listeners,
attrs,
props,
scopedSlots: this.$scopedSlots,
}, this.$slots.default)
}
}
}
4 changes: 3 additions & 1 deletion templates/components/plugin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Vue from 'vue'
import { wrapFunctional } from './index'

<% const components = options.getComponents() %>

const components = {
<%= components.map(c => {
const exp = c.export === 'default' ? `c.default || c` : `c['${c.export}']`
return ` ${c.pascalName.replace(/^Lazy/, '')}: () => import('../${relativeToBuild(c.filePath)}' /* webpackChunkName: "${c.chunkName}" */).then(c => ${exp})`
return ` ${c.pascalName.replace(/^Lazy/, '')}: () => import('../${relativeToBuild(c.filePath)}' /* webpackChunkName: "${c.chunkName}" */).then(c => wrapFunctional(${exp}))`
}).join(',\n') %>
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixture/my-lib/components/MAwesome.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @vue/component
export const MAwesome = {
export const MMAwesome = {
render: h => h('div', 'M is Awesome!')
}

0 comments on commit 0210066

Please sign in to comment.