Skip to content

Commit

Permalink
feat(compiler-core): more hoisting optimizations (vuejs#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
HcySunYang authored and yyx990803 committed Oct 15, 2019
1 parent 9a37c4b commit 68a3879
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 14 deletions.
12 changes: 12 additions & 0 deletions packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap
Expand Up @@ -3986,6 +3986,7 @@ Object {
Object {
"content": Object {
"content": "a < b",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7151,6 +7152,7 @@ Object {
Object {
"content": Object {
"content": "'</div>'",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7320,6 +7322,7 @@ Object {
Object {
"arg": Object {
"content": "se",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7640,6 +7643,7 @@ Object {
Object {
"content": Object {
"content": "",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7798,6 +7802,7 @@ Object {
Object {
"arg": Object {
"content": "class",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -7816,6 +7821,7 @@ Object {
},
"exp": Object {
"content": "{ some: condition }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7876,6 +7882,7 @@ Object {
Object {
"arg": Object {
"content": "style",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -7894,6 +7901,7 @@ Object {
},
"exp": Object {
"content": "{ color: 'red' }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -7983,6 +7991,7 @@ Object {
Object {
"arg": Object {
"content": "style",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -8001,6 +8010,7 @@ Object {
},
"exp": Object {
"content": "{ color: 'red' }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down Expand Up @@ -8080,6 +8090,7 @@ Object {
Object {
"arg": Object {
"content": "class",
"isConstant": true,
"isStatic": true,
"loc": Object {
"end": Object {
Expand All @@ -8098,6 +8109,7 @@ Object {
},
"exp": Object {
"content": "{ some: condition }",
"isConstant": false,
"isStatic": false,
"loc": Object {
"end": Object {
Expand Down
20 changes: 20 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Expand Up @@ -298,6 +298,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `message`,
isStatic: false,
isConstant: false,
loc: {
start: { offset: 2, line: 1, column: 3 },
end: { offset: 9, line: 1, column: 10 },
Expand All @@ -322,6 +323,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `a<b`,
isStatic: false,
isConstant: false,
loc: {
start: { offset: 3, line: 1, column: 4 },
end: { offset: 6, line: 1, column: 7 },
Expand All @@ -347,6 +349,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: `a<b`,
isStatic: false,
isConstant: false,
loc: {
start: { offset: 3, line: 1, column: 4 },
end: { offset: 6, line: 1, column: 7 },
Expand All @@ -365,6 +368,7 @@ describe('compiler: parse', () => {
content: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: false,
isConstant: false,
content: 'c>d',
loc: {
start: { offset: 12, line: 1, column: 13 },
Expand All @@ -390,6 +394,8 @@ describe('compiler: parse', () => {
content: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: false,
// The `isConstant` is the default value and will be determined in `transformExpression`.
isConstant: false,
content: '"</div>"',
loc: {
start: { offset: 8, line: 1, column: 9 },
Expand Down Expand Up @@ -974,6 +980,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: false,
isConstant: false,
loc: {
start: { offset: 11, line: 1, column: 12 },
end: { offset: 12, line: 1, column: 13 },
Expand All @@ -999,6 +1006,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'click',
isStatic: true,
isConstant: true,

loc: {
source: 'click',
Expand Down Expand Up @@ -1071,6 +1079,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'click',
isStatic: true,
isConstant: true,

loc: {
source: 'click',
Expand Down Expand Up @@ -1107,6 +1116,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,

loc: {
source: 'a',
Expand All @@ -1127,6 +1137,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,

loc: {
start: { offset: 8, line: 1, column: 9 },
Expand All @@ -1153,6 +1164,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,

loc: {
source: 'a',
Expand All @@ -1173,6 +1185,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,

loc: {
start: { offset: 13, line: 1, column: 14 },
Expand All @@ -1199,6 +1212,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,

loc: {
source: 'a',
Expand All @@ -1219,6 +1233,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,

loc: {
start: { offset: 8, line: 1, column: 9 },
Expand All @@ -1245,6 +1260,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,

loc: {
source: 'a',
Expand All @@ -1265,6 +1281,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'b',
isStatic: false,
isConstant: false,

loc: {
start: { offset: 14, line: 1, column: 15 },
Expand All @@ -1291,6 +1308,7 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'a',
isStatic: true,
isConstant: true,
loc: {
source: 'a',
start: {
Expand All @@ -1310,6 +1328,8 @@ describe('compiler: parse', () => {
type: NodeTypes.SIMPLE_EXPRESSION,
content: '{ b }',
isStatic: false,
// The `isConstant` is the default value and will be determined in transformExpression
isConstant: false,
loc: {
start: { offset: 10, line: 1, column: 11 },
end: { offset: 15, line: 1, column: 16 },
Expand Down
Expand Up @@ -152,6 +152,93 @@ return function render() {
}"
`;
exports[`compiler: hoistStatic transform prefixIdentifiers hoist class with static object value 1`] = `
"const _Vue = Vue
const _createVNode = Vue.createVNode
const _hoisted_1 = { class: { foo: true }}
return function render() {
with (this) {
const { toString: _toString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue
return (_openBlock(), _createBlock(\\"div\\", null, [
_createVNode(\\"span\\", _hoisted_1, _toString(_ctx.bar), 1 /* TEXT */)
]))
}
}"
`;
exports[`compiler: hoistStatic transform prefixIdentifiers hoist nested static tree with static interpolation 1`] = `
"const _Vue = Vue
const _createVNode = Vue.createVNode
const _hoisted_1 = _createVNode(\\"span\\", null, [\\"foo \\", _toString(1), _toString(2)])
return function render() {
with (this) {
const { toString: _toString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue
return (_openBlock(), _createBlock(\\"div\\", null, [
_hoisted_1
]))
}
}"
`;
exports[`compiler: hoistStatic transform prefixIdentifiers hoist nested static tree with static prop value 1`] = `
"const _Vue = Vue
const _createVNode = Vue.createVNode
const _hoisted_1 = _createVNode(\\"span\\", { foo: 0 }, _toString(1), 1 /* TEXT */)
return function render() {
with (this) {
const { toString: _toString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue
return (_openBlock(), _createBlock(\\"div\\", null, [
_hoisted_1
]))
}
}"
`;
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist expressions that with scope variable (2) 1`] = `
"const _Vue = Vue
return function render() {
with (this) {
const { renderList: _renderList, openBlock: _openBlock, createBlock: _createBlock, Fragment: _Fragment, toString: _toString, createVNode: _createVNode } = _Vue
return (_openBlock(), _createBlock(\\"div\\", null, [
(_openBlock(), _createBlock(_Fragment, null, _renderList(_ctx.list, (o) => {
return (_openBlock(), _createBlock(\\"p\\", null, [
_createVNode(\\"span\\", null, _toString(o + 'foo'), 1 /* TEXT */)
]))
}), 128 /* UNKEYED_FRAGMENT */))
]))
}
}"
`;
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist expressions that with scope variable 1`] = `
"const _Vue = Vue
return function render() {
with (this) {
const { renderList: _renderList, openBlock: _openBlock, createBlock: _createBlock, Fragment: _Fragment, toString: _toString, createVNode: _createVNode } = _Vue
return (_openBlock(), _createBlock(\\"div\\", null, [
(_openBlock(), _createBlock(_Fragment, null, _renderList(_ctx.list, (o) => {
return (_openBlock(), _createBlock(\\"p\\", null, [
_createVNode(\\"span\\", null, _toString(o), 1 /* TEXT */)
]))
}), 128 /* UNKEYED_FRAGMENT */))
]))
}
}"
`;
exports[`compiler: hoistStatic transform should NOT hoist components 1`] = `
"const _Vue = Vue
Expand Down

0 comments on commit 68a3879

Please sign in to comment.