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

Commit 085b4d7

Browse files
committed
Fix coding style
1 parent 0fd3ea5 commit 085b4d7

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"html-minifier": "latest",
3131
"parse5": "latest",
3232
"rollup-pluginutils": "latest",
33+
"vue-template-compiler": "^2.0.0-rc.4",
3334
"vue-template-validator": "latest"
3435
},
3536
"devDependencies": {
@@ -48,7 +49,6 @@
4849
"rollup": "latest",
4950
"rollup-plugin-buble": "^0.13.0",
5051
"rollup-plugin-replace": "latest",
51-
"vue-hot-reload-api": "^1.2.2",
52-
"vue-template-compiler": "^2.0.0-rc.4"
52+
"vue-hot-reload-api": "^1.2.2"
5353
}
5454
}

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function vue(options = {}) {
77
const filter = createFilter(options.include, options.exclude);
88
const styles = {};
99
let dest = options.css;
10-
const compileTemplate = !!options.compileTemplate
10+
const compileTemplate = !!options.compileTemplate;
1111

1212
return {
1313
name: 'vue',
@@ -20,7 +20,7 @@ export default function vue(options = {}) {
2020
return null;
2121
}
2222

23-
const { js, css } = vueTransform(source, id, { compileTemplate });
23+
const { js, css } = vueTransform(source, id, { compileTemplate });
2424

2525
// Map of every stylesheet
2626
styles[id] = css || {};
@@ -30,7 +30,9 @@ export default function vue(options = {}) {
3030
},
3131
ongenerate(opts, rendered) {
3232
// Put with statements back
33-
rendered.code = rendered.code.replace(/if\s*\("__VUE_WITH__"\)/g, 'with(this)');
33+
/* eslint-disable no-param-reassign */
34+
rendered.code = rendered.code.replace(/if\s*\(""__VUE_WITH_STATEMENT__"\)/g,
35+
'with(this)');
3436
if (options.css === false) {
3537
return;
3638
}

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export default {
1212
removeEmptyAttributes: true,
1313
removeOptionalTags: true,
1414
},
15+
VUE_WITH_STATEMENT: '__VUE_WITH_STATEMENT__',
1516
};

src/vueTransform.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ function padContent(content) {
3939
.join('\n');
4040
}
4141

42+
/**
43+
* Wrap code inside a with statement inside a function
44+
* This is necessary for Vue 2 template compilation
45+
*
46+
* @param {string} code
47+
* @returns {string}
48+
*/
49+
function wrapRenderFunction(code) {
50+
// Replace with(this) by something that works on strict mode
51+
// https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js
52+
return `function(){${code.replace(/with\(this\)/g, 'if("__VUE_WITH_STATEMENT__")')}}`;
53+
}
54+
4255
/**
4356
* Only support for es5 modules
4457
*
@@ -47,17 +60,16 @@ function padContent(content) {
4760
* @returns {string}
4861
*/
4962
function injectRender(script, render) {
50-
const matches = /(export default[^{]*\{)/g.exec(script);
51-
if (matches) {
52-
function toFunction (code) {
53-
// Replace with(this) by something that works on strict mode
54-
// https://github.com/vuejs/vue-template-es2015-compiler/blob/master/index.js
55-
return `function(){${code.replace(/with\(this\)/g, 'if("__VUE_WITH__")')}}`;
63+
const matches = /(export default[^{]*\{)/g.exec(script);
64+
if (matches) {
65+
return script.split(matches[1])
66+
.join(`${matches[1]}\
67+
render: ${wrapRenderFunction(render.render)},\
68+
staticRenderFns: [\
69+
${render.staticRenderFns.map(wrapRenderFunction).join(',')}\
70+
],`);
5671
}
57-
return script.split(matches[1])
58-
.join(`${matches[1]} \n /* istanbul ignore next */\n render: ${toFunction(render.render)},\n /* istanbul ignore next */\n staticRenderFns: [${render.staticRenderFns.map(toFunction).join(',')}],`);
59-
}
60-
throw new Error('[rollup-plugin-vue] could not find place to inject template in script.');
72+
throw new Error('[rollup-plugin-vue] could not find place to inject template in script.');
6173
}
6274

6375
/**
@@ -100,7 +112,7 @@ function processTemplate(node, filePath, content) {
100112
* @param {string} content
101113
* @param {string} template
102114
*/
103-
function processScript(node, filePath, content, {template, render}) {
115+
function processScript(node, filePath, content, { template, render }) {
104116
const lang = checkLang(node) || 'js';
105117
let script = parse5.serialize(node);
106118
// pad the script to ensure correct line number for syntax errors
@@ -117,7 +129,7 @@ function processScript(node, filePath, content, {template, render}) {
117129
return script;
118130
}
119131

120-
export default function vueTransform(code, filePath, options) {
132+
export default function vueTransform(code, filePath, transformOptions) {
121133
// 1. Parse the file into an HTML tree
122134
const fragment = parse5.parseFragment(code, { locationInfo: true });
123135

@@ -135,13 +147,13 @@ export default function vueTransform(code, filePath, options) {
135147

136148
// 4. Process template
137149
const template = processTemplate(nodes.template, filePath, code);
138-
let js = null
139-
if (options.compileTemplate) {
140-
const render = compileTemplate(template);
141-
js = processScript(nodes.script, filePath, code, { render })
142-
} else {
143-
js = processScript(nodes.script, filePath, code, { template })
144-
}
150+
let js;
151+
if (transformOptions.compileTemplate) {
152+
const render = compileTemplate(template);
153+
js = processScript(nodes.script, filePath, code, { render });
154+
} else {
155+
js = processScript(nodes.script, filePath, code, { template });
156+
}
145157

146158
// 5. Process script & style
147159
return {

0 commit comments

Comments
 (0)