diff --git a/generator/index.js b/generator/index.js index a884247..9c014d9 100644 --- a/generator/index.js +++ b/generator/index.js @@ -21,6 +21,12 @@ module.exports = (api, _options = {}, rootOptions = {}) => { api.render('./template') if (isVue3) { + api.injectImports( + api.entryFile, + `import VueRouterLayout from 'vue-router-layout'` + ) + api.transformScript(api.entryFile, require('./inject-use-plugin')) + api.render('./template-vue3') } diff --git a/generator/inject-use-plugin.js b/generator/inject-use-plugin.js new file mode 100644 index 0000000..2c79a88 --- /dev/null +++ b/generator/inject-use-plugin.js @@ -0,0 +1,29 @@ +module.exports = (file, api) => { + const j = api.jscodeshift + const root = j(file.source) + + const appRoots = root.find(j.CallExpression, (node) => { + if (j.Identifier.check(node.callee) && node.callee.name === 'createApp') { + return true + } + + if ( + j.MemberExpression.check(node.callee) && + j.Identifier.check(node.callee.object) && + node.callee.object.name === 'Vue' && + j.Identifier.check(node.callee.property) && + node.callee.property.name === 'createApp' + ) { + return true + } + }) + + appRoots.replaceWith(({ node: createAppCall }) => { + return j.callExpression( + j.memberExpression(createAppCall, j.identifier('use')), + [j.identifier('VueRouterLayout')] + ) + }) + + return root.toSource() +}