├── .browserslistrc ├── public ├── favicon.ico ├── index.html └── iframe.html ├── src ├── assets │ └── logo.png ├── views │ ├── about.vue │ ├── customCode │ │ ├── service.js │ │ ├── codeEditor.vue │ │ ├── index.vue │ │ ├── mountCrossIframe.vue │ │ ├── withComponent.vue │ │ ├── withMount.vue │ │ └── mountSameIframe.vue │ └── home.vue ├── store │ ├── index.js │ └── variable.js ├── app.vue ├── router │ └── index.js ├── main.js ├── utils │ ├── vm.js │ └── dom.js └── components │ ├── banner.vue │ └── helloWorld.vue ├── husky.config.js ├── babel.config.js ├── lint-staged.config.js ├── README.md ├── .editorconfig ├── scripts └── preCommit.sh ├── .gitignore ├── LICENSE ├── package.json ├── vue.config.js ├── .eslintrc.js └── doc └── Vue隐藏技能——运行时渲染.md /.browserslistrc: -------------------------------------------------------------------------------- 1 | > 1% 2 | last 2 versions 3 | not dead 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/merfais/vue-demo/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/merfais/vue-demo/HEAD/src/assets/logo.png -------------------------------------------------------------------------------- /husky.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'pre-commit': 'lint-staged', 4 | }, 5 | }; 6 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | '@vue/cli-plugin-babel/preset', 4 | ], 5 | }; 6 | -------------------------------------------------------------------------------- /src/views/about.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /lint-staged.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | '*': [ 3 | './scripts/preCommit.sh', 4 | 'git add', 5 | ], 6 | }; 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-demo 2 | 3 | **vue demo 地址**: [https://merfais.github.io/vue-demo/#/](https://merfais.github.io/vue-demo/#/) 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.{js,jsx,ts,tsx,vue}] 2 | indent_style = space 3 | indent_size = 2 4 | end_of_line = lf 5 | trim_trailing_whitespace = true 6 | insert_final_newline = true 7 | max_line_length = 100 8 | -------------------------------------------------------------------------------- /scripts/preCommit.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | export NODE_ENV='production' 4 | git diff --cached --name-only | \ 5 | grep -E "src/.*\.(js|jsx|ts|tsx|vue)$" |\ 6 | grep -v 'mocker' |\ 7 | xargs eslint -c ./.eslintrc.js --max-warnings=0 --fix --no-error-on-unmatched-pattern 8 | exit $? 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | 6 | # local env files 7 | .env.local 8 | .env.*.local 9 | 10 | # Log files 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | pnpm-debug.log* 15 | 16 | # Editor directories and files 17 | .idea 18 | .vscode 19 | *.suo 20 | *.ntvs* 21 | *.njsproj 22 | *.sln 23 | *.sw? 24 | -------------------------------------------------------------------------------- /src/store/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Vuex from 'vuex' 3 | import variable from './variable' 4 | 5 | Vue.use(Vuex) 6 | 7 | export default new Vuex.Store({ 8 | state: { 9 | filePath: '', 10 | pageName: '', 11 | }, 12 | mutations: { 13 | setState(state, payload) { 14 | Object.assign(state, payload) 15 | }, 16 | }, 17 | actions: { 18 | }, 19 | modules: { 20 | variable, 21 | }, 22 | }) 23 | -------------------------------------------------------------------------------- /src/app.vue: -------------------------------------------------------------------------------- 1 | 7 | 16 | 26 | -------------------------------------------------------------------------------- /src/router/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | import VueRouter from 'vue-router'; 3 | 4 | Vue.use(VueRouter); 5 | 6 | export const routes = [ 7 | { 8 | path: '/', 9 | name: 'Home', 10 | component: () => import(/* webpackChunkName: "home" */ '@/views/home.vue'), 11 | }, 12 | { 13 | path: '/custom-code', 14 | name: 'CustomCode', 15 | component: () => import(/* webpackChunkName customCode */ '@/views/customCode/index.vue'), 16 | }, 17 | ]; 18 | 19 | const router = new VueRouter({ 20 | routes, 21 | }); 22 | 23 | export default router; 24 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | import Antd from 'ant-design-vue'; 3 | import 'ant-design-vue/dist/antd.css'; 4 | import Codemirror from 'vue-codemirror'; 5 | import 'codemirror/lib/codemirror.css'; 6 | import App from './app.vue'; 7 | import router from './router'; 8 | import store from './store'; 9 | 10 | Vue.config.productionTip = false; 11 | 12 | Vue.use(Antd); 13 | Vue.use(Codemirror, /* { 14 | options: { theme: 'base16-dark', ... }, 15 | events: ['scroll', ...] 16 | } */); 17 | 18 | new Vue({ 19 | router, 20 | store, 21 | render: (h) => h(App), 22 | }).$mount('#app'); 23 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | <%= htmlWebpackPlugin.options.title %> 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/views/customCode/service.js: -------------------------------------------------------------------------------- 1 | export const template = `` 9 | 10 | 11 | export const js = `function generate() { 12 | return { 13 | name: 'customCode', 14 | methods: { 15 | onClick() { 16 | const cookie = window.parent.document.cookie 17 | this.$message.info(\`消息提示: cookie = \${cookie}\`) 18 | } 19 | }, 20 | }; 21 | }` 22 | 23 | export const css = `.wrapper { 24 | margin: 10px; 25 | padding: 10px; 26 | border: 1px solid #ccc; 27 | } 28 | ` 29 | -------------------------------------------------------------------------------- /src/store/variable.js: -------------------------------------------------------------------------------- 1 | import map from 'lodash/map' 2 | 3 | const variable = { 4 | namespaced: true, 5 | state: { 6 | map: { 7 | varA: 1, 8 | }, 9 | }, 10 | getters: { 11 | list(state, getters) { 12 | return map(state.map, (item) => { 13 | const getterKey = `${item.key}/value` 14 | let value = '' 15 | try { 16 | value = JSON.stringify(getters[getterKey]) 17 | } catch (e) {} 18 | return { 19 | key: item.key, 20 | type: item.type, 21 | name: item.name, 22 | value, 23 | } 24 | }) 25 | }, 26 | }, 27 | mutations: { 28 | setState(state, payload) { 29 | Object.assign(state, payload) 30 | }, 31 | }, 32 | actions: { 33 | }, 34 | } 35 | 36 | export default variable 37 | -------------------------------------------------------------------------------- /src/utils/vm.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 将字符串转换成代码对象 3 | * @param code 代码 4 | * @param value 默认值 5 | * @param params scoped变量,上下文变量,类似全局变量 6 | */ 7 | export function stringToCode(code, value, params) { 8 | const result = { value, error: null } 9 | try { 10 | result.value = new Function('context', `return ${code}`)(params) || value // eslint-disable-line no-new-func 11 | } catch (e) { 12 | console.error('js脚本错误:', e) 13 | result.error = e 14 | } 15 | return result 16 | } 17 | 18 | /** 19 | * 执行一段字符串格式的函数 20 | */ 21 | export function runFnInVm(code, params, globalParams) { 22 | const NOOP = args => args 23 | const result = stringToCode(code, NOOP, globalParams) 24 | const fn = result.value 25 | result.value = params 26 | if (result.error) { 27 | return result 28 | } 29 | if (typeof fn !== 'function') { 30 | console.error('非法的js脚本函数', fn) 31 | result.error = new Error('非法的js脚本函数') 32 | return result 33 | } 34 | try { 35 | result.value = fn.call(fn, params) 36 | } catch (e) { 37 | console.error('js脚本执行错误:', e) 38 | result.error = e 39 | } 40 | return result 41 | } 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 cof 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-demo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "serve": "vue-cli-service serve", 7 | "build": "vue-cli-service build", 8 | "lint": "NODE_ENV=production eslint -c ./.eslintrc.js --ext js,jsx,vue --max-warnings=0 --no-error-on-unmatched-pattern ./", 9 | "lint:fix": "NODE_ENV=production eslint -c ./.eslintrc.js --fix --ext js,jsx,vue --max-warnings=0 --no-error-on-unmatched-pattern ./" 10 | }, 11 | "dependencies": { 12 | "ant-design-vue": "^1.7.2", 13 | "codemirror": "^5.59.1", 14 | "core-js": "^3.6.5", 15 | "lodash": "^4.17.20", 16 | "vue": "^2.6.11", 17 | "vue-codemirror": "^4.0.6", 18 | "vue-router": "^3.2.0", 19 | "vuex": "^3.4.0" 20 | }, 21 | "devDependencies": { 22 | "@vue/cli-plugin-babel": "~4.5.0", 23 | "@vue/cli-plugin-eslint": "~4.5.0", 24 | "@vue/cli-plugin-router": "~4.5.0", 25 | "@vue/cli-plugin-vuex": "~4.5.0", 26 | "@vue/cli-service": "~4.5.0", 27 | "@vue/eslint-config-airbnb": "^5.0.2", 28 | "babel-eslint": "^10.1.0", 29 | "eslint": "^6.7.2", 30 | "eslint-plugin-import": "^2.20.2", 31 | "eslint-plugin-vue": "^6.2.2", 32 | "husky": "^4.3.8", 33 | "less": "^4.1.0", 34 | "less-loader": "^7.2.1", 35 | "lint-staged": "^10.5.3", 36 | "vue-template-compiler": "^2.6.11" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /vue.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | const devServer = { 4 | port: 8000, 5 | watchOptions: { 6 | // poll: true, 7 | }, 8 | disableHostCheck: true, 9 | }; 10 | 11 | const chainWebpack = (config) => { 12 | config.resolve.alias 13 | .set('@', path.resolve(__dirname, 'src')) 14 | .set('src', path.resolve(__dirname, 'src')) 15 | .set('plugins', path.resolve(__dirname, 'src/plugins')) 16 | .set('components', path.resolve(__dirname, 'src/components')) 17 | .set('router', path.resolve(__dirname, 'src/router')) 18 | .set('store', path.resolve(__dirname, 'src/store')) 19 | .set('services', path.resolve(__dirname, 'src/services')) 20 | .set('network', path.resolve(__dirname, 'src/network')) 21 | .set('utils', path.resolve(__dirname, 'src/utils')) 22 | .set('pages', path.resolve(__dirname, 'src/pages')) 23 | .set('views', path.resolve(__dirname, 'src/views')); 24 | 25 | if (process.env.NODE_ENV === 'development') { 26 | config.devtool(false); 27 | } 28 | }; 29 | 30 | const pages = { 31 | index: { 32 | // page 的入口 33 | entry: 'src/main.js', 34 | // 模板来源 35 | template: 'public/index.html', 36 | // 在 dist/index.html 的输出 37 | filename: 'index.html', 38 | // 当使用 title 选项时, 39 | // template 中的 title 标签需要是 <%= htmlWebpackPlugin.options.title %> 40 | title: 'demo', 41 | // 在这个页面中包含的块,默认情况下会包含 42 | // 提取出来的通用 chunk 和 vendor chunk。 43 | chunks: ['chunk-vendors', 'chunk-common', 'index'], 44 | }, 45 | }; 46 | 47 | module.exports = { 48 | pages, 49 | publicPath: './', 50 | chainWebpack, 51 | devServer, 52 | // lintOnSave: false, 53 | runtimeCompiler: true, 54 | productionSourceMap: false, 55 | }; 56 | -------------------------------------------------------------------------------- /src/utils/dom.js: -------------------------------------------------------------------------------- 1 | export function appendLink(href, doc = document) { 2 | const link = doc.createElement('link'); 3 | link.rel = 'stylesheet'; 4 | link.type = 'text/css'; 5 | link.href = href; 6 | link.media = 'all'; 7 | doc.head.appendChild(link); 8 | } 9 | 10 | export function appendScriptLink(data, doc = document) { 11 | const list = !Array.isArray(data) ? [data] : data; 12 | return Promise.all(list.map(item => new Promise(resolve => { 13 | const script = doc.createElement('script'); 14 | script.type = 'text/javascript'; 15 | script.onload = resolve; 16 | Object.assign(script, item); 17 | doc.head.appendChild(script); 18 | }))) 19 | } 20 | 21 | export function appendStyle(data, doc = document) { 22 | const style = doc.createElement('style'); 23 | style.id = Math.random().toString(36).slice(2); 24 | style.type = 'text/css'; 25 | style.appendChild(doc.createTextNode(data)); 26 | doc.head.appendChild(style); 27 | return style.id; 28 | } 29 | 30 | export function appendScript(data, doc = document) { 31 | const script = doc.createElement('script'); 32 | script.id = Math.random().toString(36).slice(2); 33 | script.type = 'text/javascript'; 34 | script.appendChild(doc.createTextNode(data)); 35 | doc.head.appendChild(script); 36 | return script.id; 37 | } 38 | 39 | export function prependDom(data, doc = document) { 40 | const tag = typeof data === 'string' ? data : data.tag; 41 | const dom = doc.createElement(tag); 42 | Object.assign(dom, data) 43 | doc.body.prepend(dom); 44 | } 45 | 46 | export function removeElement(id, doc = document) { 47 | if (!id) { 48 | return 49 | } 50 | const ele = doc.getElementById(id); 51 | if (ele) { 52 | ele.parentNode.removeChild(ele); 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/views/home.vue: -------------------------------------------------------------------------------- 1 | 18 | 19 | 35 | 36 | 90 | -------------------------------------------------------------------------------- /src/components/banner.vue: -------------------------------------------------------------------------------- 1 | 29 | 55 | 103 | -------------------------------------------------------------------------------- /src/views/customCode/codeEditor.vue: -------------------------------------------------------------------------------- 1 | 74 | 85 | -------------------------------------------------------------------------------- /src/components/helloWorld.vue: -------------------------------------------------------------------------------- 1 | 34 | 35 | 43 | 44 | 45 | 61 | -------------------------------------------------------------------------------- /src/views/customCode/index.vue: -------------------------------------------------------------------------------- 1 | 43 | 44 | 124 | 125 | 165 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | const level = process.env.NODE_ENV === 'production' ? 2 : 1 2 | 3 | module.exports = { 4 | root: true, 5 | env: { 6 | node: true, 7 | }, 8 | extends: [ 9 | 'plugin:vue/essential', 10 | '@vue/airbnb', 11 | ], 12 | parserOptions: { 13 | parser: 'babel-eslint', 14 | }, 15 | rules: { 16 | // 'import/no-unresolved': 0, 17 | // 'import/newline-after-import': 0, 18 | // 'import/imports-first': 0, 19 | // 'import/extensions': 0, 20 | // 'import/no-dynamic-require': 0, 21 | // 'import/no-extraneous-dependencies': 0, 22 | // 'import/prefer-default-export': 0, 23 | // 'import/no-named-as-default': 0, 24 | // 'import/no-webpack-loader-syntax': 0, 25 | 26 | // 关闭的规则 27 | 'vue/script-indent': 0, 28 | 'import/extensions': 0, 29 | 'import/prefer-default-export': 0, 30 | 31 | 'arrow-parens': 0, 32 | 'arrow-body-style': 0, 33 | 'consistent-return': 0, 34 | 'function-paren-newline': 0, 35 | 'prefer-destructuring': 0, 36 | 'prefer-promise-reject-errors': 0, 37 | 'no-return-assign': 0, 38 | semi: 0, 39 | 40 | // 开发时关闭的规则 41 | 'vue/html-indent': [level, 2, { baseIndent: 0, closeBracket: 1, alignAttributesVertically: false }], 42 | 'vue/no-unused-components': level, 43 | 44 | 'block-spacing': level, 45 | 'brace-style': level, 46 | camelcase: level, 47 | 'comma-spacing': level, 48 | 'comma-dangle': level, 49 | indent: [level, 2, { SwitchCase: 1 }], 50 | 'key-spacing': level, 51 | 'max-len': level, 52 | 'no-console': [level, { allow: ['warn', 'error', 'info'] }], 53 | 'no-debugger': level, 54 | 'no-empty': [level, { allowEmptyCatch: true }], 55 | 'no-mixed-operators': level, 56 | 'no-multiple-empty-lines': [level, { maxEOF: 2, max: 2, maxBOF: 1 }], 57 | 'no-multi-spaces': [level, { ignoreEOLComments: true }], 58 | 'no-param-reassign': level, 59 | 'no-underscore-dangle': level, 60 | 'no-unreachable': level, 61 | 'no-unused-vars': level, 62 | 'object-curly-newline': [level, { consistent: true }], 63 | 'prefer-const': level, 64 | 'padded-blocks': level, 65 | 'quote-props': level, 66 | quotes: level, 67 | 'spaced-comment': level, 68 | 'space-before-blocks': level, 69 | 'space-before-function-paren': level, 70 | 'space-infix-ops': level, 71 | 72 | // 'arrow-spacing': 1, 73 | // camelcase: 0, 74 | // 'comma-dangle': [1, 'only-multiline'], 75 | // 'comma-spacing': 1, 76 | // eqeqeq: 1, 77 | // 'func-names': [1, 'never'], 78 | // 'guard-for-in': 1, 79 | // 'key-spacing': 1, 80 | // 'keyword-spacing': 1, 81 | // indent: [2, 2, { SwitchCase: 1 }], 82 | // 'max-len': 1, 83 | // 'new-cap': 1, 84 | // 'newline-per-chained-call': 0, 85 | // 'no-console': [ 86 | // level, 87 | // { 88 | // allow: ['warn', 'error', 'info'], 89 | // }, 90 | // ], 91 | // 'no-debugger': level, 92 | // 'no-empty-function': 1, 93 | // 'no-trailing-spaces': [2, { 94 | // skipBlankLines: true 95 | // }], 96 | // 'no-new': 1, 97 | // 'no-mixed-operators': 0, 98 | // 'no-multiple-empty-lines': [1, { max: 2, maxEOF: 1, maxBOF: 1 }], 99 | // 'no-multi-str': 0, 100 | // 'no-multi-spaces': [1, { ignoreEOLComments: true }], 101 | // 'no-unused-vars': [2, { args: 'none' }], 102 | // 'no-unused-expressions': [2, { allowShortCircuit: true }], 103 | // 'no-underscore-dangle': [1, { allowAfterThis: true }], 104 | // 'no-unneeded-ternary': 1, 105 | // 'no-restricted-syntax': [1, 'DebuggerStatement'], 106 | // 'no-plusplus': [1, { allowForLoopAfterthoughts: true }], 107 | // 'no-param-reassign': 0, 108 | // 'no-shadow': 0, 109 | // 'object-shorthand': 0, 110 | // 'object-curly-spacing': 1, 111 | // 'object-curly-newline': [1, { consistent: true }], 112 | // 'operator-linebreak': 0, 113 | // 'one-var': 1, 114 | // 'one-var-declaration-per-line': [1, 'initializations'], 115 | // 'prefer-arrow-callback': 0, 116 | // 'prefer-spread': 1, 117 | // quotes: [1, 'single', { 118 | // avoidEscape: true, 119 | // allowTemplateLiterals: true 120 | // }], 121 | // 'quote-props': 1, 122 | // radix: [1, 'as-needed'], 123 | // 'spaced-comment': 1, 124 | // 'space-infix-ops': 1, 125 | // semi: 0, 126 | // 'space-before-function-paren': [1, 'never'], 127 | // 'space-before-blocks': 1, 128 | }, 129 | }; 130 | -------------------------------------------------------------------------------- /src/views/customCode/mountCrossIframe.vue: -------------------------------------------------------------------------------- 1 |