Skip to content

Commit 933a70a

Browse files
author
zhaoqi15
committed
fix: dll json (iife section contains | __d ===)
1 parent 47f07b4 commit 933a70a

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
lines changed

Example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"babel-jest": "^26.6.3",
3535
"eslint": "^7.14.0",
3636
"jest": "^26.6.3",
37-
"metro-code-split": "^0.1.6",
37+
"metro-code-split": "^0.1.7",
3838
"metro-react-native-babel-preset": "^0.64.0",
3939
"react-test-renderer": "17.0.1",
4040
"typescript": "^3.8.3"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
"__prelude__",
3+
"node_modules/metro-runtime/src/polyfills/require.js",
4+
"node_modules/@react-native/polyfills/console.js",
5+
"node_modules/@react-native/polyfills/error-guard.js",
6+
"node_modules/@react-native/polyfills/Object.es7.js"
7+
]

Example/public/dll/_pre.ios.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
"__prelude__",
3+
"node_modules/metro-runtime/src/polyfills/require.js",
4+
"node_modules/@react-native/polyfills/console.js",
5+
"node_modules/@react-native/polyfills/error-guard.js",
6+
"node_modules/@react-native/polyfills/Object.es7.js"
7+
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "metro-code-split",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"main": "src/index.js",
55
"bin": {
66
"mcs-scripts": "./src/bin/index.js"

src/config/craeteMustConfig.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const path = require('path')
12
const { fse } = require('general-tools')
23
const dynamicImports = require('./dynamicImports')
3-
const { paths, replacePath } = require('../utils')
4+
const { paths, replacePath, preName } = require('../utils')
45

56
/**
67
* craete must config
@@ -22,15 +23,22 @@ module.exports = mcs => {
2223
if (moduleId) return moduleId
2324
const relativePath = replacePath(absolutePath)
2425
if (mcs.isDllPath(absolutePath)) { // dll module
25-
cacheMap.set(absolutePath, relativePath)
26-
return relativePath
26+
const dllId = mcs.findDllModuleId(absolutePath)
27+
cacheMap.set(absolutePath, dllId)
28+
return dllId
2729
} else { // business module
2830
return mcs.options.createBusinessModuleId({ mcs, cacheMap, absolutePath, relativePath })
2931
}
3032
}
3133
},
3234
// Serializer
3335
async customSerializer (...args) {
36+
// build dllJosn pre section
37+
if (mcs.isBuildDllJson) {
38+
const preOutputPath = path.resolve(paths.outputDir, preName)
39+
const preContent = JSON.stringify(args[1].map(v => replacePath(v.path)), null, 2)
40+
await fse.writeFile(preOutputPath, preContent)
41+
}
3442
if (!mcs.isBuildDll) mcs.hooks.beforeCustomSerializer.call(...args)
3543

3644
let bundle = ''

src/index.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { mergeConfig } = require('metro-config')
33
const { fse, ejs, tapable: { SyncHook, SyncBailHook }, log, paths: ps, dataExtend, argv } = require('general-tools')
44
const baseConfig = require('./config/baseConfig')
55
const InjectVar = require('./plugins/InjectVar')
6-
const { isBaseDllPath, paths, dllJsonName, output, replacePath } = require('./utils')
6+
const { isBaseDllPath, paths, dllJsonName, output, replacePath, preName } = require('./utils')
77
const { BuildType } = require('./types')
88
const pkg = require('../package.json')
99

@@ -194,18 +194,60 @@ class MetroCodeSplit {
194194
* @returns { boolean }
195195
*/
196196
isDllPath = p => {
197+
let prePaths = []
197198
let commonPaths = []
199+
const preRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, preName))
200+
const dllRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, dllJsonName))
201+
try {
202+
prePaths = require(preRefPath)
203+
} catch (err) {
204+
!this.isBuildDllJson && log('warning: failed to load the preRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
205+
}
198206
try {
199-
const dllRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, dllJsonName))
200207
commonPaths = require(dllRefPath)
201208
} catch (err) {
202-
BuildType.DllJson !== this.bundleOutputInfo.name && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
209+
!this.isBuildDllJson && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
203210
}
204211
// inertia method
205-
this.isDllPath = p => commonPaths.includes(replacePath(p))
212+
this.isDllPath = ap => {
213+
const rp = replacePath(ap)
214+
// iife section contains | __d ===
215+
return prePaths.some(v => rp.endsWith(v) || v.endsWith(rp)) || commonPaths.includes(rp)
216+
}
206217
return this.isDllPath(p)
207218
}
208219

220+
/**
221+
* @param { string } p absolute path
222+
* @returns { string }
223+
*/
224+
findDllModuleId = p => {
225+
let prePaths = []
226+
let commonPaths = []
227+
const preRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, preName))
228+
const dllRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, dllJsonName))
229+
try {
230+
prePaths = require(preRefPath)
231+
} catch (err) {
232+
!this.isBuildDllJson && log('warning: failed to load the preRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
233+
}
234+
try {
235+
commonPaths = require(dllRefPath)
236+
} catch (err) {
237+
!this.isBuildDllJson && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
238+
}
239+
// inertia method
240+
this.findDllModuleId = ap => {
241+
const rp = replacePath(ap)
242+
const iifeId = prePaths.find(v => rp.endsWith(v) || v.endsWith(rp))
243+
if (iifeId) return iifeId
244+
const dId = commonPaths.find(v => v === rp)
245+
if (dId) return dId
246+
throw new Error('failed to find the dll module id!')
247+
}
248+
return this.findDllModuleId(p)
249+
}
250+
209251
async mergeTo (busineConfig) {
210252
if (!require('./utils').isProduction) {
211253
log(
@@ -243,6 +285,10 @@ class MetroCodeSplit {
243285
get isBuildDll () {
244286
return [BuildType.DllJson, BuildType.Dll].includes(this.bundleOutputInfo.name)
245287
}
288+
289+
get isBuildDllJson () {
290+
return BuildType.DllJson === this.bundleOutputInfo.name
291+
}
246292
}
247293

248294
module.exports = MetroCodeSplit

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ module.exports = {
2020
output,
2121
replacePath,
2222
dllJsonName: `_dll.${argv.platform}.json`,
23+
preName: `_pre.${argv.platform}.json`,
2324
isBaseDllPath,
2425
}

0 commit comments

Comments
 (0)