https://github.com/avajs/require-precompiled
Require extension that allows for caching/precompiling
https://github.com/avajs/require-precompiled
caching nodejs precompiling require-extension
Last synced: 7 months ago
JSON representation
Require extension that allows for caching/precompiling
- Host: GitHub
- URL: https://github.com/avajs/require-precompiled
- Owner: avajs
- License: mit
- Created: 2015-12-16T03:55:18.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-02-09T13:55:02.000Z (almost 6 years ago)
- Last Synced: 2025-05-23T22:29:57.949Z (8 months ago)
- Topics: caching, nodejs, precompiling, require-extension
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# require-precompiled
Modifies `require()` so you can load precompiled module sources.
## Install
```
$ npm install --save require-precompiled
```
## Usage
```js
const installPrecompiler = require('require-precompiled');
const cache = require('my-cache-implementation');
installPrecompiler(filename => {
if (cache.hasEntryFor(filename)) {
return cache.getPrecompiledCode(filename);
}
// fall through to underlying extension chain
return null;
});
// any module required from this point on will be checked against the cache
const foo = require('some-module');
```
## API
```ts
function installPrecompiler(
loadSource: (filename: string) => string | null,
ext = '.js',
): void
```
The `loadSource()` function should return a source string when a precompiled source is available. Return `null` to fall back to Node.js' default behavior.
By default the precompiler is installed for `.js` files. You can specify alternative extensions by providing the second argument:
```js
installPrecompiler(filename => {
// ...
}, '.cjs')
```
## License
MIT © [James Talmage](https://github.com/jamestalmage)