|
| 1 | +import { |
| 2 | + parse, |
| 3 | +} from '@babel/parser'; |
| 4 | + |
| 5 | +import { |
| 6 | + traverse, |
| 7 | + transformFromAstSync, |
| 8 | + types as t, |
| 9 | +} from '@babel/core'; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +export function transform(source, opts) { |
| 14 | + |
| 15 | + /* |
| 16 | + opts: |
| 17 | + objectAssign: "Object.assign" |
| 18 | + transforms: |
| 19 | + modules: false |
| 20 | + stripWith: true |
| 21 | + stripWithFunctional: false |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | + // links : |
| 26 | + // babel types: https://babeljs.io/docs/en/babel-types |
| 27 | + // doc: https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md#toc-inserting-a-sibling-node |
| 28 | + // astexplorer: https://astexplorer.net/ |
| 29 | + // buble/src/program/types/WithStatement.js : https://github.com/yyx990803/buble/blob/f5996c9cdb2e61cb7dddf0f6c6f25d0f3f600055/src/program/types/WithStatement.js |
| 30 | + |
| 31 | + const srcAst = parse(source); |
| 32 | + |
| 33 | + const names = 'Infinity,undefined,NaN,isFinite,isNaN,' + |
| 34 | + 'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' + |
| 35 | + 'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' + |
| 36 | + 'require,' + // for webpack |
| 37 | + 'arguments,' + // parsed as identifier but is a special keyword... |
| 38 | + '_h,_c' // cached to save property access (_c for ^2.1.5) |
| 39 | + |
| 40 | + const hash = Object.create(null) |
| 41 | + names.split(',').forEach(name => { |
| 42 | + hash[name] = true |
| 43 | + }) |
| 44 | + |
| 45 | + const isDeclaration = type => /Declaration$/.test(type); |
| 46 | + const isFunction = type => /Function(Expression|Declaration)$/.test(type); |
| 47 | + |
| 48 | + // see https://github.com/yyx990803/buble/blob/f5996c9cdb2e61cb7dddf0f6c6f25d0f3f600055/src/utils/prependVm.js#L6 |
| 49 | + function shouldPrependVm(identifier) { |
| 50 | + |
| 51 | + if ( |
| 52 | + |
| 53 | + // not id of a Declaration |
| 54 | + !(isDeclaration(identifier.parent.type) && identifier.parent.id === identifier) && |
| 55 | + |
| 56 | + // not a params of a function |
| 57 | + !(isFunction(identifier.parent.type) && identifier.parent.params.indexOf(identifier) > -1) && |
| 58 | + |
| 59 | + // not a key of Property |
| 60 | + !(identifier.parent.type === 'ObjectProperty' && identifier.parent.key === identifier.node && !identifier.parent.computed) && |
| 61 | + |
| 62 | + // not a property of a MemberExpression |
| 63 | + !(identifier.parent.type === 'MemberExpression' && identifier.parent.property === identifier.node && !identifier.parent.computed) && |
| 64 | + |
| 65 | + // not in an Array destructure pattern |
| 66 | + !(identifier.parent.type === 'ArrayPattern') && |
| 67 | + |
| 68 | + // not in an Object destructure pattern |
| 69 | + !(identifier.parentPath.parent.type === 'ObjectPattern') && |
| 70 | + |
| 71 | + // skip globals + commonly used shorthands |
| 72 | + !hash[identifier.node.name] && |
| 73 | + |
| 74 | + // not already in scope |
| 75 | + !identifier.scope.hasBinding(identifier.node.name) |
| 76 | + |
| 77 | + ) { |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + const withStatementVisitor = { |
| 86 | + Identifier(path) { |
| 87 | + |
| 88 | + if ( shouldPrependVm(path) ) { |
| 89 | + |
| 90 | + // don't know how to handle re-rentancy |
| 91 | + //path.replaceWith(t.MemberExpression(t.identifier('_vm'), t.identifier(path.node.name))); |
| 92 | + // then use: |
| 93 | + path.node.name = '_vm.' + path.node.name; |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + traverse(srcAst, { |
| 99 | + |
| 100 | + // https://babeljs.io/docs/en/babel-types#withstatement |
| 101 | + WithStatement(path) { |
| 102 | + |
| 103 | + path.traverse(withStatementVisitor); |
| 104 | + |
| 105 | + const left = parse('var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h'); |
| 106 | + path.replaceWithMultiple([ ...left.program.body, ...path.node.body.body ]); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + }); |
| 111 | + |
| 112 | + const { code, map, ast } = transformFromAstSync(srcAst, source); |
| 113 | + |
| 114 | + |
| 115 | + return { |
| 116 | + code, |
| 117 | + } |
| 118 | +} |
0 commit comments