From 933a70acbf2b6a8d694bbcdcdd5a24c9bb14cc99 Mon Sep 17 00:00:00 2001 From: zhaoqi15 Date: Fri, 5 Aug 2022 17:20:33 +0800 Subject: [PATCH] fix: dll json (iife section contains | __d ===) --- Example/package.json | 2 +- Example/public/dll/_pre.android.json | 7 ++++ Example/public/dll/_pre.ios.json | 7 ++++ package.json | 2 +- src/config/craeteMustConfig.js | 14 ++++++-- src/index.js | 54 +++++++++++++++++++++++++--- src/utils/index.js | 1 + 7 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 Example/public/dll/_pre.android.json create mode 100644 Example/public/dll/_pre.ios.json diff --git a/Example/package.json b/Example/package.json index 9dcc315..5617ca5 100644 --- a/Example/package.json +++ b/Example/package.json @@ -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" diff --git a/Example/public/dll/_pre.android.json b/Example/public/dll/_pre.android.json new file mode 100644 index 0000000..fdf369a --- /dev/null +++ b/Example/public/dll/_pre.android.json @@ -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" +] \ No newline at end of file diff --git a/Example/public/dll/_pre.ios.json b/Example/public/dll/_pre.ios.json new file mode 100644 index 0000000..fdf369a --- /dev/null +++ b/Example/public/dll/_pre.ios.json @@ -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" +] \ No newline at end of file diff --git a/package.json b/package.json index fa28b9a..89da6c4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/config/craeteMustConfig.js b/src/config/craeteMustConfig.js index 50f1466..dc46e3c 100644 --- a/src/config/craeteMustConfig.js +++ b/src/config/craeteMustConfig.js @@ -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 @@ -22,8 +23,9 @@ 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 }) } @@ -31,6 +33,12 @@ module.exports = mcs => { }, // 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 = '' diff --git a/src/index.js b/src/index.js index d018109..3ea8ddd 100644 --- a/src/index.js +++ b/src/index.js @@ -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') @@ -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( @@ -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 diff --git a/src/utils/index.js b/src/utils/index.js index 5f6a5e8..da3240f 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -20,5 +20,6 @@ module.exports = { output, replacePath, dllJsonName: `_dll.${argv.platform}.json`, + preName: `_pre.${argv.platform}.json`, isBaseDllPath, }