-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
294 lines (270 loc) · 10 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const path = require('path')
const { mergeConfig } = require('metro-config')
const { fse, ejs, tapable: { SyncHook, SyncBailHook }, log, paths: ps, dataExtend, argv } = require('general-tools')
const baseConfig = require('./config/baseConfig')
const InjectVar = require('./plugins/InjectVar')
const { isBaseDllPath, paths, dllJsonName, output, replacePath, preName } = require('./utils')
const { BuildType } = require('./types')
const pkg = require('../package.json')
/**
* @typedef {typeof defaultOptions} DefaultOptions
*/
const defaultOptions = {
output: {
publicPath: '', // involving chunk prefix
chunkDir: 'chunks', // chunk relative output dir
chunkHashLength: 20, // chunk hash length
chunkLoadTimeout: 120000, // chunk request timeout time
},
dll: {
entry: [], // which three - party library into dll
referenceDir: './dll', // the JSON address to reference for the build DLL file
},
dynamicImports: { // DynamicImports can also be set to false to disable this feature if it is not required
asyncFlag: 'async', // async flag
minSize: 20000, // minimum split size before compression
},
globalInlayVarName: '__APP__',
// module path
// /node_modules/metro/src/Server.js
baseJSBundlePath: 'metro/src/DeltaBundler/Serializers/baseJSBundle',
bundleToStringPath: 'metro/src/lib/bundleToString',
asyncRequirePath: 'metro-runtime/src/modules/asyncRequire',
// asyncRequire tpl address
asyncRequireTpl: require.resolve('./tpl/wrapAsyncRequire.ejs'),
asyncRequireTplExtraOptions: {},
plugins: [],
createBusinessModuleId ({ cacheMap, absolutePath, relativePath }) {
cacheMap.set(absolutePath, relativePath)
return relativePath
}
}
class MetroCodeSplit {
freezeFields = [
'serializer.processModuleFilter',
'serializer.createModuleIdFactory',
'serializer.customSerializer',
]
// match functions according to the output
bundleOutputInfos = [
{
// Output dll json manifest
name: BuildType.DllJson,
regexp: /_dll\.(ios|android)\.json/,
processModuleFilter: (() => {
const dllArr = []
const busineArr = []
let timeId = null
return arg => {
const relativePath = replacePath(arg.path)
const arr = this.options.dll.entry.length !== 0 && isBaseDllPath(relativePath) ? dllArr : busineArr
arr.push(relativePath)
timeId && clearTimeout(timeId)
timeId = setTimeout(async () => {
try {
const dllOutputPath = path.resolve(paths.outputDir, dllJsonName)
const dllContent = JSON.stringify([...new Set(dllArr)], null, 2)
await fse.writeFile(dllOutputPath, dllContent)
console.log(`info Writing json output to: ${replacePath(dllOutputPath)}`)
} catch (err) {
console.error(err)
}
}, 1500)
return true
}
})(),
},
{
// Output dll package
name: BuildType.Dll,
regexp: /_dll\.(ios|android)/,
processModuleFilter: arg => this.isDllPath(arg.path),
},
{
// Output busine package
name: BuildType.Busine,
regexp: /buz\.(ios|android)\.bundle/,
processModuleFilter: arg => !this.isDllPath(arg.path),
},
]
/**
* @param {DefaultOptions} options
*/
constructor (options = {}) {
/** @type {DefaultOptions} */
this.options = dataExtend(true, {}, defaultOptions, options)
this.hooks = Object.freeze({
/** @type {SyncHook<[any[]]>} */
beforeInit: new SyncHook(['bundleOutputInfos']),
/** @type {SyncHook<[string[]]>} */
beforeCheck: new SyncHook(['freezeFields']),
/** @type {SyncHook<[string[]]>} */
afterCheck: new SyncHook(['freezeFields']),
/** @type {SyncBailHook<[any, any, any, any]>} */
beforeCustomSerializer: new SyncBailHook(['entryPoint', 'prepend', 'graph', 'bundleOptions']),
/** @type {SyncHook<[object]>} */
beforeOutputChunk: new SyncHook(['chunkInfo']),
/** @type {SyncHook<[string]>} */
afterCustomSerializer: new SyncHook(['bundle']),
})
this.registerPlugin()
this.hooks.beforeInit.call(this.bundleOutputInfos)
// singleton pattern
this._init = this.init()
}
registerPlugin () {
const plugins = this.options.plugins.slice()
plugins.push(new InjectVar()) // First the external plugin in the built-in plugin
for (const plugin of plugins) {
plugin && plugin.register && plugin.register(this)
}
}
async init () {
await Promise.all([
this.isBuildDll && this.createDllEntryTpl(),
this.hasDynamicImports && this.createAsyncRequire()
])
}
async createDllEntryTpl () {
let dllEntryContent = ''
for (const [i, v] of Object.entries(this.options.dll.entry)) {
dllEntryContent += `\nimport * as arg${i} from '${v}'\nconsole.log(arg${i})\n`
}
await fse.ensureFile(paths.dllEntryPath)
await fse.writeFile(paths.dllEntryPath, dllEntryContent)
}
async createAsyncRequire () {
const content = await ejs.renderFile(
path.resolve(ps.cwdDir, this.options.asyncRequireTpl),
{
options: {
packageName: pkg.name,
asyncRequirePath: this.options.asyncRequirePath,
globalInlayVarName: this.options.globalInlayVarName,
relativeChunkDir: this.options.output.chunkDir,
fileSuffix: output.fileSuffix,
timeoutTime: (this.hasDynamicImports ? this.options : defaultOptions)['output']['chunkLoadTimeout'],
...this.options.asyncRequireTplExtraOptions,
}
}
)
await fse.ensureFile(paths.asyncRequireModulePath)
await fse.writeFile(paths.asyncRequireModulePath, content)
}
check (busineConfig) {
this.hasDynamicImports && this.freezeFields.push('transformer.asyncRequireModulePath')
// check freezeFields
for (const v of this.freezeFields) {
const arr = v.split('.')
let median = null
for (let i = 0; i <= arr.length - 1; i++) {
const key = arr[i]
if (i === arr.length - 1 && median[key]) {
log(
`The merge conflict originates from field "${v}". Try to delete the conflicting fieldMerging Conflicts Origins!`,
'red'
)
process.exit(1)
} else {
if (typeof busineConfig[key] === 'object') {
median = busineConfig[key]
} else {
break // there is no problem
}
}
}
}
}
/**
* is dll resource path
* @param { string } p absolute path
* @returns { boolean }
*/
isDllPath = p => {
let prePaths = []
let commonPaths = []
const preRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, preName))
const dllRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, dllJsonName))
try {
prePaths = require(preRefPath)
} catch (err) {
!this.isBuildDllJson && log('warning: failed to load the preRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
}
try {
commonPaths = require(dllRefPath)
} catch (err) {
!this.isBuildDllJson && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
}
// inertia method
this.isDllPath = ap => {
const rp = replacePath(ap)
// iife section contains | __d ===
return prePaths.some(v => rp.endsWith(v) || v.endsWith(rp)) || commonPaths.includes(rp)
}
return this.isDllPath(p)
}
/**
* @param { string } p absolute path
* @returns { string }
*/
findDllModuleId = p => {
let prePaths = []
let commonPaths = []
const preRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, preName))
const dllRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, dllJsonName))
try {
prePaths = require(preRefPath)
} catch (err) {
!this.isBuildDllJson && log('warning: failed to load the preRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
}
try {
commonPaths = require(dllRefPath)
} catch (err) {
!this.isBuildDllJson && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
}
// inertia method
this.findDllModuleId = ap => {
const rp = replacePath(ap)
const iifeId = prePaths.find(v => rp.endsWith(v) || v.endsWith(rp))
if (iifeId) return iifeId
const dId = commonPaths.find(v => v === rp)
if (dId) return dId
throw new Error('failed to find the dll module id!')
}
return this.findDllModuleId(p)
}
async mergeTo (busineConfig) {
if (!require('./utils').isProduction) {
log(
`Package "${pkg.name}" should only be used in production environments!`,
'red'
)
process.exit(1)
}
await this._init
this.hooks.beforeCheck.call(this.freezeFields)
this.check(busineConfig)
this.hooks.afterCheck.call(this.freezeFields)
const craeteMustConfig = require('./config/craeteMustConfig')
return mergeConfig(baseConfig, busineConfig, craeteMustConfig(this))
}
get baseJSBundle () { return require(this.options.baseJSBundlePath) }
get bundleToString () { return require(this.options.bundleToStringPath) }
get outputDir () { return paths.outputDir }
get outputChunkDir () { return path.resolve(paths.outputDir, this.options.output.chunkDir) }
get hasDynamicImports () { return !!this.options.dynamicImports }
// Do not match the default required to play the business
get bundleOutputInfo () {
let bundleOutputInfo = this.bundleOutputInfos.find(({ regexp }) => regexp.test(argv['bundle-output']))
bundleOutputInfo === undefined && (bundleOutputInfo = this.bundleOutputInfos.find(({ name }) => name === BuildType.Busine))
return bundleOutputInfo
}
// Whether or not to build Dll is relevant
get isBuildDll () {
return [BuildType.DllJson, BuildType.Dll].includes(this.bundleOutputInfo.name)
}
get isBuildDllJson () {
return BuildType.DllJson === this.bundleOutputInfo.name
}
}
module.exports = MetroCodeSplit