Skip to content

feat: add importSource option #284

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

Merged
merged 1 commit into from
Jul 5, 2022
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
8 changes: 3 additions & 5 deletions packages/babel-sugar-composition-api-inject-h/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import syntaxJsx from '@babel/plugin-syntax-jsx'

const importSource = '@vue/composition-api'

/**
* Check if body contains JSX
* @param t
Expand Down Expand Up @@ -49,7 +47,7 @@ const remove$createElement = (t, path) => {
}

// auto import `h` from `@vue/composition-api`
const autoImportH = (t, path) => {
const autoImportH = (t, path, importSource) => {
if (hasJSX(t, path)) {
const importNodes = path
.get('body')
Expand All @@ -68,15 +66,15 @@ const autoImportH = (t, path) => {
}
}

export default babel => {
export default (babel, { importSource = '@vue/composition-api' } = {}) => {
const t = babel.types

return {
inherits: syntaxJsx,
visitor: {
Program(path) {
remove$createElement(t, path)
autoImportH(t, path)
autoImportH(t, path, importSource)
},
},
}
Expand Down
20 changes: 10 additions & 10 deletions packages/babel-sugar-composition-api-inject-h/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const transpile = src =>
transform(
src,
{
plugins: [plugin],
plugins: [[plugin, { importSource: 'source' }]],
},
(err, result) => {
if (err) {
Expand Down Expand Up @@ -35,13 +35,13 @@ const tests = [
},
{
name: "Don't re-inject",
from: `import { h } from "@vue/composition-api";
from: `import { h } from "source";
const obj = {
method () {
return <div>test</div>
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
method() {
return <div>test</div>;
Expand All @@ -56,7 +56,7 @@ const obj = {
return <div>test</div>
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
method() {
return <div>test</div>;
Expand All @@ -75,7 +75,7 @@ const obj = {
}}>test</div>
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
method() {
return <div foo={{
Expand All @@ -95,7 +95,7 @@ const obj = {
return <div>test</div>
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
get method() {
return <div>test</div>;
Expand All @@ -110,7 +110,7 @@ const obj = {
return <div>test</div>
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
method(hey) {
return <div>test</div>;
Expand All @@ -125,7 +125,7 @@ const obj = {
return <div>test</div>
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
render() {
return <div>test</div>;
Expand All @@ -145,7 +145,7 @@ const obj = {
}
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
setup() {
return () => {
Expand All @@ -166,7 +166,7 @@ const obj = {
}
}
}`,
to: `import { h } from "@vue/composition-api";
to: `import { h } from "source";
const obj = {
setup2() {
var h = this.$createElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import syntaxJsx from '@babel/plugin-syntax-jsx'

const autoImportGetCurrentInstance = (t, path) => {
const importSource = '@vue/composition-api'
const autoImportGetCurrentInstance = (t, path, importSource) => {
const importNodes = path
.get('body')
.filter(p => p.isImportDeclaration())
Expand All @@ -22,7 +21,7 @@ const autoImportGetCurrentInstance = (t, path) => {

const injectInstanceId = '__currentInstance'

export default ({ types: t }) => {
export default ({ types: t }, { importSource = '@vue/composition-api' } = {}) => {
return {
inherits: syntaxJsx,
visitor: {
Expand All @@ -35,8 +34,6 @@ export default ({ types: t }) => {

let instanceInjected = false



path1.traverse({
JSXAttribute(path2) {
const n = path2.get('name')
Expand All @@ -47,7 +44,7 @@ export default ({ types: t }) => {
const obj = path3.get('object')
const prop = path3.get('property')
if (t.isThisExpression(obj) && t.isIdentifier(prop) && ['$', '_'].includes(prop.node.name[0])) {
autoImportGetCurrentInstance(t, p)
autoImportGetCurrentInstance(t, p, importSource)
if (!instanceInjected) {
path1.node.value.body.body.unshift(
t.variableDeclaration('const', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const transpile = src =>
transform(
result.code,
{
plugins: [plugin],
plugins: [[plugin, { importSource: 'source' }]],
},
(err, result) => {
if (err) {
Expand Down Expand Up @@ -45,7 +45,7 @@ const a = {
name: 'Generic component vModel',
from: `const a = { setup: () => { return () => <MyComponent vModel={a.b} /> } }`,
to: `
import { getCurrentInstance } from "@vue/composition-api";
import { getCurrentInstance } from "source";
const a = {
setup: () => {
const __currentInstance = getCurrentInstance();
Expand All @@ -63,7 +63,7 @@ const a = {
name: 'Component vModel_number',
from: `const a = { setup: () => { return () => <MyComponent vModel_number={a.b} /> } }`,
to: `
import { getCurrentInstance } from "@vue/composition-api";
import { getCurrentInstance } from "source";
const a = {
setup: () => {
const __currentInstance = getCurrentInstance();
Expand Down