Skip to content

Commit 7349e72

Browse files
wip(core): vue2 support, replace buble fork with a babel plugin
1 parent 61014db commit 7349e72

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

build/fakeBuble.mjs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

build/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ ${ pkg.name } v${ pkg.version }
215215
'stylus': false,
216216
'less': false,
217217
'prettier': false,
218+
'./buble.js': Path.resolve(__dirname, 'fakeBuble.mjs'), // used by vue-template-es2015-compiler
218219

219220
...!genSourcemap ? {
220221
'source-map': false,

0 commit comments

Comments
 (0)