Comments (8)
Thanks for your reply! @davidaurelio
Yes, I probably knew we should retain numeric module ID for Random Access Bundle because of it's special index table. Now your reply also help me realize more about it.
How about when using normal bundle, except unbundle, provide an option to let users choose, or define their own moduleIdFactory?
I can realize about bundle size increase that your've mentioned. But I think it maybe not so much.
In fact, our team are using a local fork of react-native based on 0.35, changed the createModuleIdFactory
to use hash module ID. code like:
function createModuleIdFactory({projectRoots, prefix}) {
const fileToIdMap = Object.create(null);
const usedIds = {};
return ({path: modulePath, name}) => {
var relativePath = getPathRelativeToRoot(projectRoots, modulePath);
if (!(modulePath in fileToIdMap)) {
fileToIdMap[modulePath] = getModuleHashedPathId(relativePath, usedIds);
}
return fileToIdMap[modulePath];
};
}
function getModuleHashedPathId(path, usedIds){
var len = 4;
var hash = crypto.createHash("md5");
hash.update(path);
var id = hash.digest("base64");
while(usedIds[id.substr(0, len)]){
len++;
}
id = id.substr(0, len);
usedIds[id] = path;
return id;
}
Part of the output bundle like this:
Now I've make a compare at one of our RN apps, which total 539 modules in production. Here is the size change betwteen numeric module ID and hash module ID (both with uglify, dev=false):
838KB - 847KB. only 10KB increased.
So in contrast, I prefer to use hash module ID for normal text bundle, it give us ability to split bundle by stable module ID.
What you can do as of today is to implement the postProcessModulesForBuck hook to order modules by path.
I haven't tried postProcessModulesForBuck
yet. I will have a look at it. But I think maybe reorder modules by path can't keep stable when deps was added/removed, their are still numbers related.
If I have any mistake please correct me. Thanks again!
from metro.
Hey @mc-zone, @lishoulong : We recently merged f347e4f, which allows to customize the createModuleIdFactory()
function, so if you use v0.24.1 you're going to be able to use your custom logic for generating the module ids.
from metro.
the new ModuleGraph
implementation only needs module IDs at the very end of the build process.
We could count the modules in each chunk, and assign them ranges, e.g. 0–1000 for the main bundle, 1000–2000 for the first chunk, depending on the number of files in each chunk.
By adding some headroom we could minimize changes to chunks that should normally be unaffected. It’s a 20%/80% solution, i.e. not perfect, but maybe good enough.
Could metro-bundler support string module ID?
numbers allow for some interesting optimizations, specifically memory-friendly look-up tables. We use that for Random Access Bundles (“unbundle”, yet to be documented :-( ) in native code (C++).
Unique hashes have to have a certain length to avoid collisions, and for large bundles that can make a non-trivial difference in size.
Everything is a tradeoff, we have to find the right one.
Using hashes in Random Access Bundles means we would have to change lookup structures from tables to hash maps, and that is undesirable, as it would incur a small TTI penalty.
Currently we are focused on runtime performance for mobile. Stable module IDs are desirable, too, but we don’t want the latter at the expense of the former.
We want to address the “multiple app” scenario in React Native with clever bundle splitting that allows sharing code between chunks, while retaining the positive properties of numeric module IDs.
What you can do as of today is to implement the postProcessModulesForBuck
hook to order modules by path. This should have neutral performance for plain text bundles, but creates significant TTI regressions when using a Random Access Bundle.
from metro.
I agree with @mc-zone 's opinion, We have a B2B project that likes the expo, developers upload their react-native code to service, after that, service sends developer's code to our customer's app and combine it.
We have to split the bundle file to a base bundle and developer's business bundles, If we use numeric module ID, because the base module has only one,Two different developer's generate there module ID will be same.
Only use moduleName or hashName can do these.
I Sincerely hope the bundle commands of the packager will be more flexible and more powerful,Thank you very much.
from metro.
@mc-zone ,I use your createModuleIdFactory for hashed ids, but when I run "react-native run build --dev false ...", it error the followings "Unexpected token: punc ()) in file "/Users/lifeifei/Documents/react-native/sourceCode/newRNBundle/index.js" at line 2:26".
I guess this is the cause of --dev false, maybe uglify.
I only change the code for the createModuleIdFactory as yours, Is I use right?
thanks!
from metro.
@lishoulong I'm not sure, haven't seen this error. Could you provide a repo about your code?
from metro.
Hi,
I fork the official metro-bundler, and I change the method of createModuleIdFactory as of you mention above.
The blow is my commits, please take your time have a look.
from metro.
@mc-zone , I know what my question is , the below is the part code I copy from the builded bundle.
var AnimatedAddition = require(helpcenter-AnimatedAddition); // helpcenter-AnimatedAddition = ./nodes/AnimatedAddition;
As you can see the required helpcenter-AnimatedAddition is not quoted , so the program look helpcenter-AnimatedAddition as a variable.The question is some require content is also quoted.
But I do not know where the problem is?
from metro.
Related Issues (20)
- metro-config 0.73.1: RNCWebViewNativeComponent.js: Could not find component config for native component HOT 4
- Named and default exports have different resolving strategies/behaviors HOT 5
- RCT-Folly Compilation Error on React Native iOS build HOT 1
- Cannot open Hermes JS Debugger on adding metro-config package to address - "metro-file-map: Haste module naming collision" due to amplify/#current-cloud-backend having same files as amplify/backend.
- Can't debug app anymore on xcode 15 HOT 2
- bug: Nested module names change during resolution
- (Suggestion): Add SWC
- Could not find or use auto-linked framework 'CoreAudioTypes': framework 'CoreAudioTypes' not found HOT 4
- Duplicate files error while running was as react-native backend after reloading.
- React-Native ERROR Invariant Violation: `new NativeEventEmitter()` requires a non-null argument., "myApp" has not been registered. iOS only
- Package Exports not working HOT 11
- `react-dom/server` cannot be resolved with `unstable_enablePackageExports` and condition `browser` HOT 5
- FATAL EXCEPTION
- SHA-1 for file C:\Users\Eportal\AppData\Roaming\npm\node_modules\react-native\node_modules\metro-runtime\src\polyfills\require.js is not computed. HOT 2
- unstable_enableSymlinks does not seems to work HOT 15
- Property "watcher.unstable_workerThreads" shows warning message when running Metro HOT 3
- unstable_enableSymlinks not work with library with react components with Babel transpilation
- Android font not shown even after linking
- None of these files exist HOT 2
- Cannot find module @callstack/out-of-tree-platforms after run `yarn start --reset-cache` in rn-tester app HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from metro.