Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit d0fa8b6

Browse files
posvaznck
authored andcommitted
Add vue-template-es2015-compiler (#33)
Using it to transpile the render code generated allows to remove `with` statements, making the generated code strict mode compatible
1 parent b2c670c commit d0fa8b6

File tree

6 files changed

+209
-238
lines changed

6 files changed

+209
-238
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"parse5": "^2.2.2",
3838
"rollup-pluginutils": "^1.5.2",
3939
"vue-template-compiler": "^2.0.3",
40+
"vue-template-es2015-compiler": "^1.2.4",
4041
"vue-template-validator": "^1.1.5"
4142
},
4243
"devDependencies": {

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,10 @@ export default function vue(options = {}) {
131131
return result;
132132
},
133133
transformBundle(source) {
134-
debug('Replace window.__VUE_WITH_STATEMENT__ with with(this) and generate style.');
135134
generateStyleBundle();
136135
const map = new MagicString(source);
137136
const result = {
138-
code: source.replace(/if[\s]*\(window\.__VUE_WITH_STATEMENT__\)/g, 'with(this)'),
137+
code: source,
139138
map: map.generateMap({ hires: true }),
140139
};
141140
debug('with(this) fixed!');

src/vueTransform.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import deIndent from 'de-indent';
22
import htmlMinifier from 'html-minifier';
33
import parse5 from 'parse5';
44
import validateTemplate from 'vue-template-validator';
5+
import transpileVueTemplate from 'vue-template-es2015-compiler';
56
import { relative } from 'path';
67
import MagicString from 'magic-string';
78
import debug from './debug';
@@ -44,9 +45,6 @@ function padContent(content) {
4445
* @returns {string}
4546
*/
4647
function wrapRenderFunction(code) {
47-
// Replace with(this) by something that works on strict mode
48-
// https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js
49-
code = code.replace(/with\(this\)/g, 'if(window.__VUE_WITH_STATEMENT__)');
5048
return `function(){${code}}`;
5149
}
5250

@@ -62,12 +60,27 @@ function injectRender(script, render, lang) {
6260
if (['js', 'babel'].indexOf(lang.toLowerCase()) > -1) {
6361
const matches = /(export default[^{]*\{)/g.exec(script);
6462
if (matches) {
65-
return script.split(matches[1])
66-
.join(`${matches[1]}` +
63+
const scriptWithRender = script.split(matches[1])
64+
// buble doesn't support export default, not even with the
65+
// module: false trasforms:
66+
// https://buble.surge.sh/guide/#using-es-modules
67+
.join('module.exports={' +
6768
`render: ${wrapRenderFunction(render.render)},` +
6869
'staticRenderFns: [' +
6970
`${render.staticRenderFns.map(wrapRenderFunction).join(',')}],`
7071
);
72+
return transpileVueTemplate(scriptWithRender, {
73+
// Remove all trasforms added by vue since it's up to the user
74+
// to use whatever he wants
75+
// https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js#L6
76+
transforms: {
77+
templateString: false,
78+
conciseMethodProperty: false,
79+
stripWith: true, // remove the with statement
80+
computedProperty: false,
81+
},
82+
// put back the export default {
83+
}).replace('module.exports={', 'export default {');
7184
}
7285

7386
debug(`No injection location found in: \n${script}\n`);

test/expects/compileTemplate.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
var compileTemplate = {render: function(){with(this){return _h('div',[_h('p',[_s(msg)])])}},staticRenderFns: [],
1+
var compileTemplate = {render: function(){var _vm=this;return _vm._h('div',[_vm._h('p',[_vm._s(_vm.msg)])])},staticRenderFns: [],
22
data() {
33
return {
4-
msg: 'Compile Template'
5-
}
6-
}
4+
msg: 'Compile Template',
5+
};
6+
},
7+
computed: {
8+
exclamation() {
9+
return `${this.msg}!`;
10+
},
11+
uselessFatArrow: () => 0
12+
},
13+
fatArrowTest() {
14+
const a = [5, 7];
15+
a.map(v => this.msg);
16+
},
717
};
818

9-
export default compileTemplate;
19+
export default compileTemplate;

test/fixtures/compileTemplate.vue

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88
export default {
99
data() {
1010
return {
11-
msg: 'Compile Template'
12-
}
13-
}
11+
msg: 'Compile Template',
12+
};
13+
},
14+
computed: {
15+
exclamation() {
16+
return `${this.msg}!`;
17+
},
18+
uselessFatArrow: () => 0
19+
},
20+
fatArrowTest() {
21+
const a = [5, 7];
22+
a.map(v => this.msg);
23+
},
1424
}
1525
</script>

0 commit comments

Comments
 (0)