UNPKG

2.79 kBJavaScriptView Raw
1const path = require('path')
2
3const slash = input => {
4 const isExtendedLengthPath = /^\\\\\?\\/.test(input)
5 const hasNonAscii = /[^\u0000-\u0080]+/.test(input) // eslint-disable-line no-control-regex
6
7 if (isExtendedLengthPath || hasNonAscii) {
8 return input
9 }
10
11 return input.replace(/\\/g, '/')
12}
13
14const pathToId = file => {
15 return path.basename(slash(file)).replace(/\W/g, '_')
16}
17
18module.exports = api => {
19 const enhanceAppFiles = [...api.enhanceAppFiles].map((filepath, index) => ({
20 id: `${pathToId(filepath)}_${index}`,
21 filepath: slash(filepath)
22 }))
23
24 return `
25 import Vue from 'vue'
26 import Meta from 'vue-meta'
27 import Router from 'vue-router'
28 import _entry from '#out/entry'
29 import { getRequireDefault } from '#app/utils'
30
31 Vue.config.productionTip = false
32
33 Vue.use(Router)
34
35 Vue.use(Meta, {
36 keyName: 'head',
37 attribute: 'data-ream-head',
38 ssrAttribute: 'data-ream-ssr',
39 tagIDKeyName: 'rhid'
40 })
41
42 const enhanceApp = getRequireDefault(require('#app/enhance-app'))
43
44 ${[...enhanceAppFiles]
45 .map(file =>
46 `
47 const ${file.id} = getRequireDefault(require('${file.filepath}'))
48 `.trim()
49 )
50 .join('\n')}
51
52 export default context => {
53 if (__DEV__ && typeof _entry !== 'function') {
54 throw new TypeError(\`The entry file should export a function but got "\${typeof _entry}"\`)
55 }
56
57 const entry = _entry(context)
58 if (__DEV__ && typeof entry !== 'object') {
59 throw new TypeError(\`The return value of the default export in entry file should be a plain object but got "\${typeof entry}"\`)
60 }
61
62 let { router, extendRootOptions } = entry
63 if (context) {
64 context.entry = entry
65 }
66
67 const rootOptions = {
68 _isReamRoot: true,
69 router: entry.router || new Router({ mode: 'history' })
70 }
71 const getInitialDataContextFns = [
72 entry.getInitialDataContext
73 ].filter(Boolean)
74 const middlewares = [
75 entry.middleware
76 ].filter(Boolean)
77
78 const event = new Vue()
79 const enhanceContext = {
80 rootOptions,
81 entry,
82 ssrContext: context,
83 event,
84 getInitialDataContext(fn) {
85 getInitialDataContextFns.push(fn)
86 },
87 addMiddleware(fn) {
88 middlewares.push(fn)
89 }
90 }
91
92 enhanceApp(enhanceContext, context)
93
94 ${[...enhanceAppFiles]
95 .map(file =>
96 `
97 if (typeof ${file.id} === 'function') {
98 ${file.id}(enhanceContext)
99 }
100 `.trim()
101 )
102 .join('\n')}
103
104 if (entry.extendRootOptions) {
105 entry.extendRootOptions(rootOptions)
106 }
107
108 const app = new Vue(rootOptions)
109
110 return {
111 app,
112 router: rootOptions.router,
113 entry,
114 getInitialDataContextFns,
115 event,
116 dataStore: rootOptions.dataStore,
117 middlewares
118 }
119 }
120
121 `
122}