UNPKG

1.72 kBJavaScriptView Raw
1const fs = require('fs')
2const getPathnameProjectConfigFile = require('./get-pathname-project-config-file')
3// const readBuildConfigFile = require('../utils/read-build-config-file')
4
5const extractType = () => {
6 const pathnameKootJS = getPathnameProjectConfigFile()
7
8 try {
9 const { type } = require(pathnameKootJS)
10 return type
11 } catch (e) { }
12
13 const content = fs.readFileSync(pathnameKootJS, 'utf-8')
14 const matches = /type[ ]*=[ ]*['"](.+?)['"]/gm.exec(content)
15 if (Array.isArray(matches) && matches.length > 1)
16 return matches[1]
17
18 return undefined
19}
20
21/**
22 * 从核心配置文件 (./koot.js) 中读取 App 类型 (type),并修改部分环境变量
23 * * WEBPACK_BUILD_TYPE: 'isomorphic' || 'spa' || etc...
24 * * KOOT_PROJECT_TYPE: 'ReactApp' || 'ReactSPA' || etc...
25 * @async
26 * @returns {String} process.env.KOOT_PROJECT_TYPE
27 */
28module.exports = async () => {
29 if (typeof process.env.KOOT_PROJECT_TYPE === 'undefined') {
30 process.env.KOOT_PROJECT_TYPE = extractType() || ''
31 }
32
33 switch (process.env.KOOT_PROJECT_TYPE.toLowerCase()) {
34 case 'react': {
35 // if ((await readBuildConfigFile()).server)
36 process.env.WEBPACK_BUILD_TYPE = 'isomorphic'
37 process.env.KOOT_PROJECT_TYPE = 'ReactApp'
38 // return 'ReactSPA'
39 break
40 }
41
42 case 'react-spa':
43 case 'reactspa': {
44 process.env.WEBPACK_BUILD_TYPE = 'spa'
45 process.env.KOOT_PROJECT_TYPE = 'ReactSPA'
46 break
47 }
48
49 // default:
50 // return process.env.KOOT_PROJECT_TYPE
51 }
52
53 return process.env.KOOT_PROJECT_TYPE
54}