|
| 1 | +import getDocFile from './getDocFile'; |
| 2 | +import stateDoc from './stateDoc'; |
| 3 | +const babel = require('babel-core'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const getRequires = require('./getRequires'); |
| 7 | +import vm from 'vm'; |
| 8 | + |
| 9 | +const babelifyCode = code => { |
| 10 | + const options = { |
| 11 | + ast: false, |
| 12 | + comments: false, |
| 13 | + presets: ['babel-preset-es2015'], |
| 14 | + }; |
| 15 | + return babel.transform(code, options); |
| 16 | +}; |
| 17 | + |
| 18 | +function clone(obj) { |
| 19 | + if (null == obj || 'object' != typeof obj) return obj; |
| 20 | + var copy = obj.constructor(); |
| 21 | + for (var attr in obj) { |
| 22 | + if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; |
| 23 | + } |
| 24 | + return copy; |
| 25 | +} |
| 26 | + |
| 27 | +function getMixins(code, file) { |
| 28 | + try { |
| 29 | + const requiresFromComponent = getRequires(code); |
| 30 | + let output = []; |
| 31 | + Object.keys(requiresFromComponent).forEach((reqFromComponent) =>{ |
| 32 | + const tempRequire = reqFromComponent.split('/'); |
| 33 | + if (tempRequire[0] === '.' || tempRequire[0] === '..') { |
| 34 | + const folderFile = path.dirname(file); |
| 35 | + const pathRequire = path.join(path.normalize(folderFile), reqFromComponent) + '.js' |
| 36 | + if (fs.existsSync(pathRequire)) { |
| 37 | + const source = fs.readFileSync(pathRequire, { encoding: 'utf-8' }); |
| 38 | + stateDoc.saveMixin(source, pathRequire); |
| 39 | + if (stateDoc.isMixin()){ |
| 40 | + const babelifycode = babelifyCode(source); |
| 41 | + const mixin = evalComponentCode(babelifycode.code); |
| 42 | + if (Object.keys(mixin.exports).length === 0 ) { |
| 43 | + mixin.exports.default = mixin.module.exports; |
| 44 | + } |
| 45 | + if (mixin.exports.default) { |
| 46 | + output.push(mixin.exports.default); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + return output; |
| 53 | + } catch (err) { |
| 54 | + throw err |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +const evalComponentCode = (code) => { |
| 59 | + try { |
| 60 | + const script = new vm.Script(code, {}); |
| 61 | + const sandbox = { |
| 62 | + exports: {}, |
| 63 | + Vue: { |
| 64 | + component: () => {}, |
| 65 | + extended: () => {}, |
| 66 | + }, |
| 67 | + module: { |
| 68 | + exports: { |
| 69 | + }, |
| 70 | + }, |
| 71 | + require:()=> { |
| 72 | + return function(){} |
| 73 | + }, |
| 74 | + document: {}, |
| 75 | + window: { |
| 76 | + location: {}, |
| 77 | + }, |
| 78 | + alert() {}, |
| 79 | + confirm() {}, |
| 80 | + console: { |
| 81 | + log() {}, |
| 82 | + debug() {}, |
| 83 | + }, |
| 84 | + sessionStorage: { |
| 85 | + getItem() {}, |
| 86 | + setItem() {}, |
| 87 | + removeItem() {}, |
| 88 | + }, |
| 89 | + localStorage: { |
| 90 | + getItem() {}, |
| 91 | + setItem() {}, |
| 92 | + removeItem() {}, |
| 93 | + }, |
| 94 | + } |
| 95 | + const context = new vm.createContext(sandbox); |
| 96 | + script.runInContext(context); |
| 97 | + const output = sandbox; |
| 98 | + return clone(output); |
| 99 | + } catch (err) { |
| 100 | + throw err |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +module.exports = function getSandbox(jscodeReqest, file) { |
| 105 | + const babelifycode = babelifyCode(jscodeReqest); |
| 106 | + let component = evalComponentCode(babelifycode.code).exports; |
| 107 | + const mixins = getMixins(babelifycode.code, file).reverse(); |
| 108 | + component.default.mixins = mixins; |
| 109 | + return component; |
| 110 | +}; |
0 commit comments