-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Build assets-registry for general consumption #39440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Base commit: b29b393 |
cc @huntie |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for migrating this package into the shared build setup 😄.
nit: Changelog in PR summary can be reduced to Changelog: [Internal]
.
"dist", | ||
"path-support.js", | ||
"registry.js", | ||
"src" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"dist", | |
"path-support.js", | |
"registry.js", | |
"src" | |
"dist", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion still applies — all other files do not need to be packaged, but are instead present at dev-time.
getAndroidResourceIdentifier, | ||
getBasePath, | ||
}; | ||
module.exports = require('./src/path-support'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To enable running from source, let's add babel-register
wrapper files for these two entry points. And yeah, actually that means keeping these files around (for Flow's type checker, which sadly doesn't follow "exports"
).
You can find an example of this pattern in packages/dev-middleware/
: https://github.com/facebook/react-native/blob/main/packages/dev-middleware/src/index.js.
And the file structure you'll need:
assets
├── path-support.js.flow # (Flow only) Re-export all types and values
├── registry.js.flow
└── src
├── path-support.js # Entry point - babel-register
├── path-support.flow.js # Implementation
├── registry.js
└── registry.flow.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I didn't quite understand what those files were. Makes sense now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So… it looks like flow-check
is unable to find .js.flow
files. I had to rename them back to .js
and fix a bunch of Dependencies of a @flow strict module must also be @flow strict!
errors. The check seems to pass locally now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this pattern works. babel-register
cannot be found when the package is consumed outside the repository. Do you have any suggestions for how to address this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
babel-register
cannot be foundwhen the package is consumed outside the repository.
Do you mean you have a setup where another project is consuming a local checkout of the react-native
repository from source (e.g. via yarn link
)?
This should work. Happy to debug via chat.
ade331e
to
16a72c5
Compare
* @format strict | ||
* @flow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this line was accidentally rearranged first!
Also, feel free to use strict-local
everywhere, it's slightly more forgiving.
* @format strict | |
* @flow | |
* @flow strict-local | |
* @format |
getAndroidResourceIdentifier, | ||
getBasePath, | ||
}; | ||
module.exports = require('./src/path-support'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
babel-register
cannot be foundwhen the package is consumed outside the repository.
Do you mean you have a setup where another project is consuming a local checkout of the react-native
repository from source (e.g. via yarn link
)?
This should work. Happy to debug via chat.
'use strict'; | ||
|
||
export type PackagerAsset = { | ||
+__packager_asset: boolean, | ||
+fileSystemLocation: string, | ||
+httpServerLocation: string, | ||
+width: ?number, | ||
+height: ?number, | ||
+scales: Array<number>, | ||
+hash: string, | ||
+name: string, | ||
+type: string, | ||
... | ||
}; | ||
|
||
const assets: Array<PackagerAsset> = []; | ||
|
||
function registerAsset(asset: PackagerAsset): number { | ||
// `push` returns new array length, so the first asset will | ||
// get id 1 (not 0) to make the value truthy | ||
return assets.push(asset); | ||
} | ||
|
||
function getAssetByID(assetId: number): PackagerAsset { | ||
return assets[assetId - 1]; | ||
} | ||
|
||
module.exports = {registerAsset, getAssetByID}; | ||
module.exports = require('./src/registry.flow'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'use strict'; | |
export type PackagerAsset = { | |
+__packager_asset: boolean, | |
+fileSystemLocation: string, | |
+httpServerLocation: string, | |
+width: ?number, | |
+height: ?number, | |
+scales: Array<number>, | |
+hash: string, | |
+name: string, | |
+type: string, | |
... | |
}; | |
const assets: Array<PackagerAsset> = []; | |
function registerAsset(asset: PackagerAsset): number { | |
// `push` returns new array length, so the first asset will | |
// get id 1 (not 0) to make the value truthy | |
return assets.push(asset); | |
} | |
function getAssetByID(assetId: number): PackagerAsset { | |
return assets[assetId - 1]; | |
} | |
module.exports = {registerAsset, getAssetByID}; | |
module.exports = require('./src/registry.flow'); | |
export * from './src/registry.flow'; |
"dist", | ||
"path-support.js", | ||
"registry.js", | ||
"src" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion still applies — all other files do not need to be packaged, but are instead present at dev-time.
getAndroidResourceIdentifier, | ||
getBasePath, | ||
}; | ||
module.exports = require('./src/path-support'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module.exports = require('./src/path-support'); | |
export * from './src/path-support'; |
// Make Metro able to resolve required external dependencies | ||
watchFolders: [ | ||
path.resolve(__dirname, '../../node_modules'), | ||
path.resolve(__dirname, '../../scripts/build'), // babel-register.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, @react-native/asset-registry
is not a Node package — it's consumed by runtime code in packages/react-native/Libraries/
!
This probably means we don't need the babel-register
setup at all (just the root .js.flow
files redirecting to src/
) — since all modules loaded by React Native go through @react-native/babel-preset
. This setup is just for run-from-source behaviour within the React Native monorepo.
Back to the PR summary:
Transpile
assets-registry
so it can be consumed outside of a React Native bundle:
This is technically new ground. I think we want to define a new build target
of 'react-native'
/'react-native-and-external'
in scripts/build/config.js
, configuring this to build files using @react-native/babel-preset
as its Babel config. Will reach out via DM.
I meet an error , pls help . thanks ! here is the error stack: [TypeError: _$$_REQUIRE(_dependencyMap[7], "(...)ssets-registry/path-support.js") is not a function (it is Object)] {"componentStack": " |
This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This PR was closed because it has been stalled for 7 days with no activity. |
Summary:
Transpile
assets-registry
so it can be consumed outside of a React Native bundle:.js
files intosrc
package.json
.js
files re-export the corresponding files in thesrc
folderThis is a follow-up to #38795 (comment). Let me know if this looks feasible to you.
Changelog:
[INTERNAL] [CHANGED] - Transpile
assets-registry
so it can be consumed outside of a React Native bundleTest Plan:
n/a