Skip to content

Commit

Permalink
fix: dll json (iife section contains | __d ===)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqi15 committed Aug 5, 2022
1 parent 47f07b4 commit 933a70a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"babel-jest": "^26.6.3",
"eslint": "^7.14.0",
"jest": "^26.6.3",
"metro-code-split": "^0.1.6",
"metro-code-split": "^0.1.7",
"metro-react-native-babel-preset": "^0.64.0",
"react-test-renderer": "17.0.1",
"typescript": "^3.8.3"
Expand Down
7 changes: 7 additions & 0 deletions Example/public/dll/_pre.android.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"__prelude__",
"node_modules/metro-runtime/src/polyfills/require.js",
"node_modules/@react-native/polyfills/console.js",
"node_modules/@react-native/polyfills/error-guard.js",
"node_modules/@react-native/polyfills/Object.es7.js"
]
7 changes: 7 additions & 0 deletions Example/public/dll/_pre.ios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"__prelude__",
"node_modules/metro-runtime/src/polyfills/require.js",
"node_modules/@react-native/polyfills/console.js",
"node_modules/@react-native/polyfills/error-guard.js",
"node_modules/@react-native/polyfills/Object.es7.js"
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metro-code-split",
"version": "0.1.6",
"version": "0.1.7",
"main": "src/index.js",
"bin": {
"mcs-scripts": "./src/bin/index.js"
Expand Down
14 changes: 11 additions & 3 deletions src/config/craeteMustConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path')
const { fse } = require('general-tools')
const dynamicImports = require('./dynamicImports')
const { paths, replacePath } = require('../utils')
const { paths, replacePath, preName } = require('../utils')

/**
* craete must config
Expand All @@ -22,15 +23,22 @@ module.exports = mcs => {
if (moduleId) return moduleId
const relativePath = replacePath(absolutePath)
if (mcs.isDllPath(absolutePath)) { // dll module
cacheMap.set(absolutePath, relativePath)
return relativePath
const dllId = mcs.findDllModuleId(absolutePath)
cacheMap.set(absolutePath, dllId)
return dllId
} else { // business module
return mcs.options.createBusinessModuleId({ mcs, cacheMap, absolutePath, relativePath })
}
}
},
// Serializer
async customSerializer (...args) {
// build dllJosn pre section
if (mcs.isBuildDllJson) {
const preOutputPath = path.resolve(paths.outputDir, preName)
const preContent = JSON.stringify(args[1].map(v => replacePath(v.path)), null, 2)
await fse.writeFile(preOutputPath, preContent)
}
if (!mcs.isBuildDll) mcs.hooks.beforeCustomSerializer.call(...args)

let bundle = ''
Expand Down
54 changes: 50 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 } = require('./utils')
const { isBaseDllPath, paths, dllJsonName, output, replacePath, preName } = require('./utils')
const { BuildType } = require('./types')
const pkg = require('../package.json')

Expand Down Expand Up @@ -194,18 +194,60 @@ class MetroCodeSplit {
* @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 {
const dllRefPath = path.resolve(ps.cwdDir, path.join(this.options.dll.referenceDir, dllJsonName))
commonPaths = require(dllRefPath)
} catch (err) {
BuildType.DllJson !== this.bundleOutputInfo.name && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
!this.isBuildDllJson && log('warning: failed to load the dllRefPath correctly! are you setting the "dll.referenceDir" correctly?', 'yellow')
}
// inertia method
this.isDllPath = p => commonPaths.includes(replacePath(p))
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(
Expand Down Expand Up @@ -243,6 +285,10 @@ class MetroCodeSplit {
get isBuildDll () {
return [BuildType.DllJson, BuildType.Dll].includes(this.bundleOutputInfo.name)
}

get isBuildDllJson () {
return BuildType.DllJson === this.bundleOutputInfo.name
}
}

module.exports = MetroCodeSplit
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ module.exports = {
output,
replacePath,
dllJsonName: `_dll.${argv.platform}.json`,
preName: `_pre.${argv.platform}.json`,
isBaseDllPath,
}

0 comments on commit 933a70a

Please sign in to comment.