Skip to content

Commit abffc65

Browse files
authoredJul 5, 2022
feat: add importSource option (#284)
1 parent 5368c63 commit abffc65

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed
 

‎packages/babel-sugar-composition-api-inject-h/src/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import syntaxJsx from '@babel/plugin-syntax-jsx'
22

3-
const importSource = '@vue/composition-api'
4-
53
/**
64
* Check if body contains JSX
75
* @param t
@@ -49,7 +47,7 @@ const remove$createElement = (t, path) => {
4947
}
5048

5149
// auto import `h` from `@vue/composition-api`
52-
const autoImportH = (t, path) => {
50+
const autoImportH = (t, path, importSource) => {
5351
if (hasJSX(t, path)) {
5452
const importNodes = path
5553
.get('body')
@@ -68,15 +66,15 @@ const autoImportH = (t, path) => {
6866
}
6967
}
7068

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

7472
return {
7573
inherits: syntaxJsx,
7674
visitor: {
7775
Program(path) {
7876
remove$createElement(t, path)
79-
autoImportH(t, path)
77+
autoImportH(t, path, importSource)
8078
},
8179
},
8280
}

‎packages/babel-sugar-composition-api-inject-h/test/test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const transpile = src =>
77
transform(
88
src,
99
{
10-
plugins: [plugin],
10+
plugins: [[plugin, { importSource: 'source' }]],
1111
},
1212
(err, result) => {
1313
if (err) {
@@ -35,13 +35,13 @@ const tests = [
3535
},
3636
{
3737
name: "Don't re-inject",
38-
from: `import { h } from "@vue/composition-api";
38+
from: `import { h } from "source";
3939
const obj = {
4040
method () {
4141
return <div>test</div>
4242
}
4343
}`,
44-
to: `import { h } from "@vue/composition-api";
44+
to: `import { h } from "source";
4545
const obj = {
4646
method() {
4747
return <div>test</div>;
@@ -56,7 +56,7 @@ const obj = {
5656
return <div>test</div>
5757
}
5858
}`,
59-
to: `import { h } from "@vue/composition-api";
59+
to: `import { h } from "source";
6060
const obj = {
6161
method() {
6262
return <div>test</div>;
@@ -75,7 +75,7 @@ const obj = {
7575
}}>test</div>
7676
}
7777
}`,
78-
to: `import { h } from "@vue/composition-api";
78+
to: `import { h } from "source";
7979
const obj = {
8080
method() {
8181
return <div foo={{
@@ -95,7 +95,7 @@ const obj = {
9595
return <div>test</div>
9696
}
9797
}`,
98-
to: `import { h } from "@vue/composition-api";
98+
to: `import { h } from "source";
9999
const obj = {
100100
get method() {
101101
return <div>test</div>;
@@ -110,7 +110,7 @@ const obj = {
110110
return <div>test</div>
111111
}
112112
}`,
113-
to: `import { h } from "@vue/composition-api";
113+
to: `import { h } from "source";
114114
const obj = {
115115
method(hey) {
116116
return <div>test</div>;
@@ -125,7 +125,7 @@ const obj = {
125125
return <div>test</div>
126126
}
127127
}`,
128-
to: `import { h } from "@vue/composition-api";
128+
to: `import { h } from "source";
129129
const obj = {
130130
render() {
131131
return <div>test</div>;
@@ -145,7 +145,7 @@ const obj = {
145145
}
146146
}
147147
}`,
148-
to: `import { h } from "@vue/composition-api";
148+
to: `import { h } from "source";
149149
const obj = {
150150
setup() {
151151
return () => {
@@ -166,7 +166,7 @@ const obj = {
166166
}
167167
}
168168
}`,
169-
to: `import { h } from "@vue/composition-api";
169+
to: `import { h } from "source";
170170
const obj = {
171171
setup2() {
172172
var h = this.$createElement;

‎packages/babel-sugar-composition-api-render-instance/src/index.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import syntaxJsx from '@babel/plugin-syntax-jsx'
22

3-
const autoImportGetCurrentInstance = (t, path) => {
4-
const importSource = '@vue/composition-api'
3+
const autoImportGetCurrentInstance = (t, path, importSource) => {
54
const importNodes = path
65
.get('body')
76
.filter(p => p.isImportDeclaration())
@@ -22,7 +21,7 @@ const autoImportGetCurrentInstance = (t, path) => {
2221

2322
const injectInstanceId = '__currentInstance'
2423

25-
export default ({ types: t }) => {
24+
export default ({ types: t }, { importSource = '@vue/composition-api' } = {}) => {
2625
return {
2726
inherits: syntaxJsx,
2827
visitor: {
@@ -35,8 +34,6 @@ export default ({ types: t }) => {
3534

3635
let instanceInjected = false
3736

38-
39-
4037
path1.traverse({
4138
JSXAttribute(path2) {
4239
const n = path2.get('name')
@@ -47,7 +44,7 @@ export default ({ types: t }) => {
4744
const obj = path3.get('object')
4845
const prop = path3.get('property')
4946
if (t.isThisExpression(obj) && t.isIdentifier(prop) && ['$', '_'].includes(prop.node.name[0])) {
50-
autoImportGetCurrentInstance(t, p)
47+
autoImportGetCurrentInstance(t, p, importSource)
5148
if (!instanceInjected) {
5249
path1.node.value.body.body.unshift(
5350
t.variableDeclaration('const', [

‎packages/babel-sugar-composition-api-render-instance/test/test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const transpile = src =>
1717
transform(
1818
result.code,
1919
{
20-
plugins: [plugin],
20+
plugins: [[plugin, { importSource: 'source' }]],
2121
},
2222
(err, result) => {
2323
if (err) {
@@ -45,7 +45,7 @@ const a = {
4545
name: 'Generic component vModel',
4646
from: `const a = { setup: () => { return () => <MyComponent vModel={a.b} /> } }`,
4747
to: `
48-
import { getCurrentInstance } from "@vue/composition-api";
48+
import { getCurrentInstance } from "source";
4949
const a = {
5050
setup: () => {
5151
const __currentInstance = getCurrentInstance();
@@ -63,7 +63,7 @@ const a = {
6363
name: 'Component vModel_number',
6464
from: `const a = { setup: () => { return () => <MyComponent vModel_number={a.b} /> } }`,
6565
to: `
66-
import { getCurrentInstance } from "@vue/composition-api";
66+
import { getCurrentInstance } from "source";
6767
const a = {
6868
setup: () => {
6969
const __currentInstance = getCurrentInstance();

0 commit comments

Comments
 (0)
Please sign in to comment.