snack-require-context
v0.1.0
Published
A Snack-compatible implementation of `require.context`
Downloads
81
Readme
snack-require-context
Support for require.context
in Snack is tricky, since we have no access to any bundler when executing code from users. This package helps re-implementing this API in Snack, for both libraries (through Snackager) and user-provided code (through Runtime).
Usage
This package contains code for both the Snack Runtime and Snackager.
Snackager
The only implementation for Snackager is the transformation of require.context
statements to require('<virtual-module>')
.
This is done through Babel and converts the full require.context
signature to a path with query parameter.
Once this code is evaluated within the app, the Snack Runtime knows it's a virtual module and generates the result.
Runtime
The require.context
system is more integrated in the Snack Runtime.
It needs to recognize virtual module requests, generate the require.context
result based on the current project, and create new require.context
requests when using Expo Router.
Here are the different integration points of the require.context
system within the Snack Runtime.
- When Expo Router is detected in a project, the Snack-compatible entry point is rendered with a virtual module request of the
./app
directory. - When the requested module is a virtual module, it generates the result of the
require.context
based on the files and assets within the Snack. - When parent module of the
require.context
statement or any of the containing files or assets changes, it reinitialzes the virtual module.
Contributing
This package has a few commands, mainly to build, analyze and build for publishing.
yarn dev
→ Builds an unoptimized development build.yarn build
→ Builds an optimized production build.