-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
164 lines (143 loc) · 4.42 KB
/
index.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const webpack = require('webpack')
const path = require('path')
const fs = require('fs')
const rootPath = process.cwd();
const tempDir = '.tmp'
const extendDir = 'extend'
const includeDir = 'include'
const tempPath = `${rootPath}/${tempDir}/`
const extendPath = `${tempPath}${extendDir}/`
const includePath = `${tempPath}${includeDir}/`
function injectMultiTenantEnvironment(themePath, tenantPath) {
const envPath = fs.existsSync(tenantPath) ? tenantPath : themePath
const envFile = process.env.NODE_ENV === 'development' ? '.env.local.json' : `.env.${process.env.NODE_ENV}.json`
const themeEnvPath = `.${themePath}${envFile}`
const tenantEnvPath = `.${tenantPath}${envFile}`
let env = {}
if (fs.existsSync(themeEnvPath)) {
env = {
...require(`.${envPath}${envFile}`)
}
}
if (fs.existsSync(tenantEnvPath)) {
env = {
...env,
...require(`.${envPath}${envFile}`)
}
}
process.env = {
...process.env,
...env
}
}
function checkForModuleReplacement(path, excludedPaths) {
if (!path || !Array.isArray(excludedPaths)) {
return false
}
excludedPaths.push('/node_modules/')
for (let i = 0; i < excludedPaths.length; i++) {
if (path.includes(excludedPaths[i])) {
return false
}
}
return true
}
function runSetup({ srcPath, themePath, tenantPath, tenant }) {
if (!fs.existsSync(tempPath)) {
fs.mkdirSync(tempPath)
fs.mkdirSync(extendPath)
fs.symlinkSync(`${rootPath}${srcPath}`, `${tempDir}/${includeDir}`, 'dir')
}
if (!fs.existsSync(`${rootPath}${themePath}`)){
fs.mkdirSync(`${rootPath}${themePath}`)
if (tenant) {
fs.mkdirSync(`${rootPath}${tenantPath}`)
}
}
cleanDirectoryRecursive(extendPath)
}
function cleanDirectoryRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file) {
const curPath = `${path}/${file}`
if (fs.lstatSync(curPath).isDirectory()) {
cleanDirectoryRecursive(curPath)
} else {
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(path)
}
}
module.exports = function multiTenantPlugin(
{
tenant,
theme,
themeDir = 'themes',
tenantDir = 'tenants',
srcDir = 'src',
beforeRun,
injectEnvironment = false,
excludeDirs = [],
} = {}
) {
const themePath = theme ? `/${themeDir}/${theme}/` : `/${themeDir}/`
const tenantPath = tenant ? `${themePath}${tenantDir}/${tenant}/` : `${themePath}${tenantDir}/`
const srcPath = `/${srcDir}/`
const srcRegexPath = new RegExp(`(${srcPath})`)
runSetup({ srcPath, tenant, themePath, tenantPath })
if (injectEnvironment) {
injectMultiTenantEnvironment(themePath, tenantPath)
}
if (typeof beforeRun === 'function') {
beforeRun()
}
return new webpack.NormalModuleReplacementPlugin(srcRegexPath, function(resource) {
if (checkForModuleReplacement(resource.userRequest, excludeDirs)) {
const themeResourcePath = resource.userRequest.replace(
srcRegexPath,
themePath
)
const tenantResourcePath = resource.userRequest.replace(
srcRegexPath,
tenantPath
)
let filePath = ''
let resourcePath = ''
let replace = false
// if file exists in tenant use that path
if (tenant && fs.existsSync(tenantResourcePath)) {
filePath = tenantPath
resourcePath = tenantResourcePath
replace = true
// else if file exists in theme use that path
} else if (fs.existsSync(themeResourcePath)) {
filePath = themePath
resourcePath = themeResourcePath
replace = true
}
// if replacement is needed replace
if (replace) {
fs.mkdirSync(
`.tmp/extend${path
.dirname(resource.userRequest.replace(rootPath, ''))
.replace(__dirname, '')
.replace(srcPath.replace(/\//g, ''), '')}`,
{ recursive: true },
(err) => {
if (err) throw err
}
)
fs.copyFileSync(
resource.userRequest,
`.tmp/extend${path
.dirname(resource.userRequest.replace(rootPath, ''))
.replace(__dirname, '')
.replace(srcPath.replace(/\//g, ''), '')}/${path.basename(resource.userRequest)}`
)
resource.request = resource.request.replace(srcRegexPath, filePath)
resource.resource = resourcePath
}
}
})
}