-
Notifications
You must be signed in to change notification settings - Fork 20
/
create-helpers.ts
64 lines (57 loc) · 1.51 KB
/
create-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* 创建 SSR 的 helpers
*/
import { resolve } from 'path'
import { _ } from './underscore'
import { createResolver } from './resolver'
import { SanSSRData } from './san-ssr-data'
import { JSEmitter } from '../target-js/js-emitter'
import { readStringSync } from '../utils/fs'
/**
* 编译成源代码时,需要包含的运行时文件
*/
const HELPER_FILES = [
resolve(__dirname, '../../dist/runtime/underscore.js'),
resolve(__dirname, '../../dist/runtime/san-ssr-data.js'),
resolve(__dirname, '../../dist/runtime/resolver.js')
]
export interface SanSSRHelpers {
/**
* 无状态的工具库,类似 lodash
*/
_: typeof _
/**
* SanSSRData 的 SSR 运行时替代品
*/
SanSSRData: typeof SanSSRData
/**
* 组件 render、Class 解析器
*/
createResolver: typeof createResolver
}
/**
* 产出运行时代码并封装为 IIFE 的返回值
*/
export function emitHelpersAsIIFE (emitter: JSEmitter) {
emitter.feedLine('(function (exports) {')
emitter.indent()
emitHelpers(emitter)
emitter.writeLine('return exports;')
emitter.unindent()
emitter.writeLine('})({});')
}
/**
* 产出运行时代码
*/
export function emitHelpers (emitter: JSEmitter) {
for (const file of HELPER_FILES) {
emitter.writeLines(readStringSync(file))
emitter.carriageReturn()
}
}
/**
* 编译成 render 函数时,使用的 helper
*/
export function createHelpers (): SanSSRHelpers {
return { _, SanSSRData, createResolver }
}