Skip to content

Commit 62a8e4c

Browse files
committed
feat(vue2): iframe support
1 parent 611ba54 commit 62a8e4c

File tree

9 files changed

+76
-13
lines changed

9 files changed

+76
-13
lines changed

packages/app-backend-core/src/hook.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export function installHook (target, iframe = false) {
5555
}
5656

5757
hook = {
58-
Vue: null,
58+
// eslint-disable-next-line accessor-pairs
59+
set Vue (value) {
60+
sendToParent(hook => { hook.Vue = value })
61+
},
5962

6063
on (event, fn) {
6164
sendToParent(hook => hook.on(event, fn))

packages/app-backend-core/src/legacy/scan.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,30 @@ export function scan () {
4242
}
4343

4444
if (isBrowser) {
45-
walk(document, function (node) {
46-
if (inFragment) {
47-
if (node === currentFragment._fragmentEnd) {
48-
inFragment = false
49-
currentFragment = null
45+
const walkDocument = document => {
46+
walk(document, function (node) {
47+
if (inFragment) {
48+
if (node === currentFragment._fragmentEnd) {
49+
inFragment = false
50+
currentFragment = null
51+
}
52+
return true
5053
}
51-
return true
52-
}
53-
const instance = node.__vue__
54+
const instance = node.__vue__
55+
56+
return processInstance(instance)
57+
})
58+
}
59+
walkDocument(document)
5460

55-
return processInstance(instance)
56-
})
61+
const iframes = document.querySelectorAll<HTMLIFrameElement>('iframe')
62+
for (const iframe of iframes) {
63+
try {
64+
walkDocument(iframe.contentDocument)
65+
} catch (e) {
66+
// Ignore
67+
}
68+
}
5769
} else {
5870
if (Array.isArray(target.__VUE_ROOT_INSTANCES__)) {
5971
target.__VUE_ROOT_INSTANCES__.map(processInstance)

packages/shell-dev-vue2/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dev": "cross-env TAILWIND_MODE=watch PORT=8100 webpack serve"
77
},
88
"dependencies": {
9-
"@vue-devtools/shell-host": "^0.0.0"
9+
"@vue-devtools/shell-host": "^0.0.0",
10+
"vuex": "^3.6.2"
1011
},
1112
"devDependencies": {
1213
"@vue-devtools/build-tools": "^0.0.0",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title></title>
6+
<style>
7+
html, body {
8+
background: white;
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
<div id="app"></div>
14+
<script src="target/iframe-app.js"></script>
15+
</body>
16+
</html>

packages/shell-dev-vue2/public/target.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<body>
88
<div id="app"></div>
99
<div id="shadow"></div>
10+
<iframe src="/target-iframe.html" width="100%"></iframe>
1011
<script src="target/hook.js"></script>
1112
<script src="target/target.js"></script>
1213
</body>

packages/shell-dev-vue2/src/Child.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
export default {
3+
data () {
4+
return {
5+
count: 0
6+
}
7+
}
8+
}
9+
</script>
10+
11+
<template>
12+
<div>
13+
{{ count }} <button @click="count++">
14+
+1
15+
</button>
16+
</div>
17+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Vue from 'vue'
2+
import Child from './Child.vue'
3+
4+
new Vue({
5+
...Child,
6+
name: 'IframeApp'
7+
}).$mount('#app')

packages/shell-dev-vue2/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = createConfig({
77
// devtools: require.resolve('@vue-devtools/shell-host/src/devtools.js'),
88
backend: require.resolve('@vue-devtools/shell-host/src/backend.js'),
99
hook: require.resolve('@vue-devtools/shell-host/src/hook.js'),
10-
target: './src/index.js'
10+
target: './src/index.js',
11+
'iframe-app': './src/iframe-app.js'
1112
},
1213
output: {
1314
path: path.join(__dirname, '/build'),

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10343,6 +10343,11 @@ vue@^3.1.0-beta.5:
1034310343
"@vue/runtime-dom" "3.1.0-beta.6"
1034410344
"@vue/shared" "3.1.0-beta.6"
1034510345

10346+
vuex@^3.6.2:
10347+
version "3.6.2"
10348+
resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.6.2.tgz#236bc086a870c3ae79946f107f16de59d5895e71"
10349+
integrity sha512-ETW44IqCgBpVomy520DT5jf8n0zoCac+sxWnn+hMe/CzaSejb/eVw2YToiXYX+Ex/AuHHia28vWTq4goAexFbw==
10350+
1034610351
vuex@^4.0.1:
1034710352
version "4.0.1"
1034810353
resolved "https://registry.yarnpkg.com/vuex/-/vuex-4.0.1.tgz#e83c541d6f31173969efabb2c5d1c465b682887e"

0 commit comments

Comments
 (0)