@@ -74,7 +74,7 @@ function createCodegenContext(
74
74
ast : RootNode ,
75
75
{
76
76
mode = 'function' ,
77
- prefixIdentifiers = mode === 'module' || mode === 'cjs' ,
77
+ prefixIdentifiers = mode === 'module' ,
78
78
sourceMap = false ,
79
79
filename = `template.vue.html` ,
80
80
scopeId = null ,
@@ -169,7 +169,6 @@ export function generate(
169
169
const {
170
170
mode,
171
171
push,
172
- helper,
173
172
prefixIdentifiers,
174
173
indent,
175
174
deindent,
@@ -182,58 +181,10 @@ export function generate(
182
181
const genScopeId = ! __BROWSER__ && scopeId != null && mode === 'module'
183
182
184
183
// preambles
185
- if ( mode === 'function' || mode === 'cjs' ) {
186
- const VueBinding = mode === 'function' ? `Vue` : `require("vue")`
187
- // Generate const declaration for helpers
188
- // In prefix mode, we place the const declaration at top so it's done
189
- // only once; But if we not prefixing, we place the declaration inside the
190
- // with block so it doesn't incur the `in` check cost for every helper access.
191
- if ( hasHelpers ) {
192
- if ( prefixIdentifiers ) {
193
- push (
194
- `const { ${ ast . helpers . map ( helper ) . join ( ', ' ) } } = ${ VueBinding } \n`
195
- )
196
- } else {
197
- // "with" mode.
198
- // save Vue in a separate variable to avoid collision
199
- push ( `const _Vue = ${ VueBinding } \n` )
200
- // in "with" mode, helpers are declared inside the with block to avoid
201
- // has check cost, but hoists are lifted out of the function - we need
202
- // to provide the helper here.
203
- if ( ast . hoists . length ) {
204
- const staticHelpers = [ CREATE_VNODE , CREATE_COMMENT , CREATE_TEXT ]
205
- . filter ( helper => ast . helpers . includes ( helper ) )
206
- . map ( s => `${ helperNameMap [ s ] } : _${ helperNameMap [ s ] } ` )
207
- . join ( ', ' )
208
- push ( `const { ${ staticHelpers } } = _Vue\n` )
209
- }
210
- }
211
- }
212
- genHoists ( ast . hoists , context )
213
- newline ( )
214
- push ( `return ` )
184
+ if ( mode === 'module' ) {
185
+ genModulePreamble ( ast , context , genScopeId )
215
186
} else {
216
- // generate import statements for helpers
217
- if ( genScopeId ) {
218
- ast . helpers . push ( WITH_SCOPE_ID )
219
- if ( ast . hoists . length ) {
220
- ast . helpers . push ( PUSH_SCOPE_ID , POP_SCOPE_ID )
221
- }
222
- }
223
- if ( hasHelpers ) {
224
- push ( `import { ${ ast . helpers . map ( helper ) . join ( ', ' ) } } from "vue"\n` )
225
- }
226
- if ( ast . imports . length ) {
227
- genImports ( ast . imports , context )
228
- newline ( )
229
- }
230
- if ( genScopeId ) {
231
- push ( `const withId = ${ helper ( WITH_SCOPE_ID ) } ("${ scopeId } ")` )
232
- newline ( )
233
- }
234
- genHoists ( ast . hoists , context )
235
- newline ( )
236
- push ( `export ` )
187
+ genFunctionPreamble ( ast , context )
237
188
}
238
189
239
190
// enter render function
@@ -315,6 +266,82 @@ export function generate(
315
266
}
316
267
}
317
268
269
+ function genFunctionPreamble ( ast : RootNode , context : CodegenContext ) {
270
+ const { mode, helper, prefixIdentifiers, push, newline } = context
271
+ const VueBinding = mode === 'function' ? `Vue` : `require("vue")`
272
+ // Generate const declaration for helpers
273
+ // In prefix mode, we place the const declaration at top so it's done
274
+ // only once; But if we not prefixing, we place the declaration inside the
275
+ // with block so it doesn't incur the `in` check cost for every helper access.
276
+ if ( ast . helpers . length > 0 ) {
277
+ if ( prefixIdentifiers ) {
278
+ push ( `const { ${ ast . helpers . map ( helper ) . join ( ', ' ) } } = ${ VueBinding } \n` )
279
+ } else {
280
+ // "with" mode.
281
+ // save Vue in a separate variable to avoid collision
282
+ push ( `const _Vue = ${ VueBinding } \n` )
283
+ // in "with" mode, helpers are declared inside the with block to avoid
284
+ // has check cost, but hoists are lifted out of the function - we need
285
+ // to provide the helper here.
286
+ if ( ast . hoists . length ) {
287
+ const staticHelpers = [ CREATE_VNODE , CREATE_COMMENT , CREATE_TEXT ]
288
+ . filter ( helper => ast . helpers . includes ( helper ) )
289
+ . map ( s => `${ helperNameMap [ s ] } : _${ helperNameMap [ s ] } ` )
290
+ . join ( ', ' )
291
+ push ( `const { ${ staticHelpers } } = _Vue\n` )
292
+ }
293
+ }
294
+ }
295
+ // generate variables for ssr helpers
296
+ if ( ! __BROWSER__ && ast . ssrHelpers && ast . ssrHelpers . length ) {
297
+ // ssr guaruntees prefixIdentifier: true
298
+ push (
299
+ `const { ${ ast . ssrHelpers
300
+ . map ( helper )
301
+ . join ( ', ' ) } } = require("@vue/server-renderer")\n`
302
+ )
303
+ }
304
+ genHoists ( ast . hoists , context )
305
+ newline ( )
306
+ push ( `return ` )
307
+ }
308
+
309
+ function genModulePreamble (
310
+ ast : RootNode ,
311
+ context : CodegenContext ,
312
+ genScopeId : boolean
313
+ ) {
314
+ const { push, helper, newline, scopeId } = context
315
+ // generate import statements for helpers
316
+ if ( genScopeId ) {
317
+ ast . helpers . push ( WITH_SCOPE_ID )
318
+ if ( ast . hoists . length ) {
319
+ ast . helpers . push ( PUSH_SCOPE_ID , POP_SCOPE_ID )
320
+ }
321
+ }
322
+ if ( ast . helpers . length ) {
323
+ push ( `import { ${ ast . helpers . map ( helper ) . join ( ', ' ) } } from "vue"\n` )
324
+ }
325
+ if ( ! __BROWSER__ && ast . ssrHelpers && ast . ssrHelpers . length ) {
326
+ push (
327
+ `import { ${ ast . ssrHelpers
328
+ . map ( helper )
329
+ . join ( ', ' ) } } from "@vue/server-renderer"\n`
330
+ )
331
+ }
332
+ if ( ast . imports . length ) {
333
+ genImports ( ast . imports , context )
334
+ newline ( )
335
+ }
336
+ if ( genScopeId ) {
337
+ push ( `const withId = ${ helper ( WITH_SCOPE_ID ) } ("${ scopeId } ")` )
338
+ newline ( )
339
+ }
340
+ genHoists ( ast . hoists , context )
341
+ newline ( )
342
+ push ( `export ` )
343
+ }
344
+
318
345
function genAssets (
319
346
assets : string [ ] ,
320
347
type : 'component' | 'directive' ,
0 commit comments