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

Add vue-template-es2015-compiler #33

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"parse5": "^2.2.2",
"rollup-pluginutils": "^1.5.2",
"vue-template-compiler": "^2.0.3",
"vue-template-es2015-compiler": "^1.2.4",
"vue-template-validator": "^1.1.5"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ export default function vue(options = {}) {
return result;
},
transformBundle(source) {
debug('Replace window.__VUE_WITH_STATEMENT__ with with(this) and generate style.');
generateStyleBundle();
const map = new MagicString(source);
const result = {
code: source.replace(/if[\s]*\(window\.__VUE_WITH_STATEMENT__\)/g, 'with(this)'),
code: source,
map: map.generateMap({ hires: true }),
};
debug('with(this) fixed!');
Expand Down
23 changes: 18 additions & 5 deletions src/vueTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import deIndent from 'de-indent';
import htmlMinifier from 'html-minifier';
import parse5 from 'parse5';
import validateTemplate from 'vue-template-validator';
import transpileVueTemplate from 'vue-template-es2015-compiler';
import { relative } from 'path';
import MagicString from 'magic-string';
import debug from './debug';
Expand Down Expand Up @@ -44,9 +45,6 @@ function padContent(content) {
* @returns {string}
*/
function wrapRenderFunction(code) {
// Replace with(this) by something that works on strict mode
// https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js
code = code.replace(/with\(this\)/g, 'if(window.__VUE_WITH_STATEMENT__)');
return `function(){${code}}`;
}

Expand All @@ -62,12 +60,27 @@ function injectRender(script, render, lang) {
if (['js', 'babel'].indexOf(lang.toLowerCase()) > -1) {
const matches = /(export default[^{]*\{)/g.exec(script);
if (matches) {
return script.split(matches[1])
.join(`${matches[1]}` +
const scriptWithRender = script.split(matches[1])
// buble doesn't support export default, not even with the
// module: false trasforms:
// https://buble.surge.sh/guide/#using-es-modules
.join('module.exports={' +
`render: ${wrapRenderFunction(render.render)},` +
'staticRenderFns: [' +
`${render.staticRenderFns.map(wrapRenderFunction).join(',')}],`
);
return transpileVueTemplate(scriptWithRender, {
// Remove all trasforms added by vue since it's up to the user
// to use whatever he wants
// https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js#L6
transforms: {
templateString: false,
conciseMethodProperty: false,
stripWith: true, // remove the with statement
computedProperty: false,
},
// put back the export default {
}).replace('module.exports={', 'export default {');
}

debug(`No injection location found in: \n${script}\n`);
Expand Down
20 changes: 15 additions & 5 deletions test/expects/compileTemplate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
var compileTemplate = {render: function(){with(this){return _h('div',[_h('p',[_s(msg)])])}},staticRenderFns: [],
var compileTemplate = {render: function(){var _vm=this;return _vm._h('div',[_vm._h('p',[_vm._s(_vm.msg)])])},staticRenderFns: [],
data() {
return {
msg: 'Compile Template'
}
}
msg: 'Compile Template',
};
},
computed: {
exclamation() {
return `${this.msg}!`;
},
uselessFatArrow: () => 0
},
fatArrowTest() {
const a = [5, 7];
a.map(v => this.msg);
},
};

export default compileTemplate;
export default compileTemplate;
16 changes: 13 additions & 3 deletions test/fixtures/compileTemplate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@
export default {
data() {
return {
msg: 'Compile Template'
}
}
msg: 'Compile Template',
};
},
computed: {
exclamation() {
return `${this.msg}!`;
},
uselessFatArrow: () => 0
},
fatArrowTest() {
const a = [5, 7];
a.map(v => this.msg);
},
}
</script>
Loading