@@ -39,6 +39,19 @@ function padContent(content) {
39
39
. join ( '\n' ) ;
40
40
}
41
41
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 ( / w i t h \( t h i s \) / g, 'if("__VUE_WITH_STATEMENT__")' ) } }` ;
53
+ }
54
+
42
55
/**
43
56
* Only support for es5 modules
44
57
*
@@ -47,17 +60,16 @@ function padContent(content) {
47
60
* @returns {string }
48
61
*/
49
62
function injectRender ( script , render ) {
50
- const matches = / ( e x p o r t d e f a u l t [ ^ { ] * \{ ) / 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 ( / w i t h \( t h i s \) / g, 'if("__VUE_WITH__")' ) } }` ;
63
+ const matches = / ( e x p o r t d e f a u l t [ ^ { ] * \{ ) / 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
+ ],` ) ;
56
71
}
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.' ) ;
61
73
}
62
74
63
75
/**
@@ -100,7 +112,7 @@ function processTemplate(node, filePath, content) {
100
112
* @param {string } content
101
113
* @param {string } template
102
114
*/
103
- function processScript ( node , filePath , content , { template, render} ) {
115
+ function processScript ( node , filePath , content , { template, render } ) {
104
116
const lang = checkLang ( node ) || 'js' ;
105
117
let script = parse5 . serialize ( node ) ;
106
118
// pad the script to ensure correct line number for syntax errors
@@ -117,7 +129,7 @@ function processScript(node, filePath, content, {template, render}) {
117
129
return script ;
118
130
}
119
131
120
- export default function vueTransform ( code , filePath , options ) {
132
+ export default function vueTransform ( code , filePath , transformOptions ) {
121
133
// 1. Parse the file into an HTML tree
122
134
const fragment = parse5 . parseFragment ( code , { locationInfo : true } ) ;
123
135
@@ -135,13 +147,13 @@ export default function vueTransform(code, filePath, options) {
135
147
136
148
// 4. Process template
137
149
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
+ }
145
157
146
158
// 5. Process script & style
147
159
return {
0 commit comments