Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/toyobayashi/native-require
Export native require function. It can be ignored by webpack preprocessing and avoid bundling webpack node polyfills.
https://github.com/toyobayashi/native-require
Last synced: 2 days ago
JSON representation
Export native require function. It can be ignored by webpack preprocessing and avoid bundling webpack node polyfills.
- Host: GitHub
- URL: https://github.com/toyobayashi/native-require
- Owner: toyobayashi
- Created: 2020-04-26T04:35:24.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-08T07:22:05.000Z (over 3 years ago)
- Last Synced: 2024-10-11T20:58:34.214Z (27 days ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# native-require
Try to get native require function in any environment.
``` bash
$ npm install @tybys/native-require
```## When should you use this package
If you want to write a library which supports both Node.js and browser, and you hope it can be work well with webpack, you can use this package instead of writing `__non_webpack_require__` directly in your code.
## Usage:
### Webpack
Plugin is **not** neccesary.
``` js
const { NativeRequireWebpackPlugin } = require('@tybys/native-require/plugins/webpack.js')module.exports = {
/* ... */
plugins: [
new NativeRequireWebpackPlugin()
]
}
```CommonJS:
``` js
const { tryGetRequireFunction } = require('@tybys/native-require/index.js') // index.js can not be omitted
const nativeRequire = tryGetRequireFunction()
if (typeof nativeRequire === 'function') {
// ...
}
```ESM format input / TypeScript:
``` ts
import { tryGetRequireFunction } from '@tybys/native-require'
const nativeRequire = tryGetRequireFunction()
if (typeof nativeRequire === 'function') {
// ...
}
```### Rollup
**NOTE**: If you are using commonjs `require('@tybys/native-require/index.js')` with `@rollup/plugin-commonjs`, you need to add `@tybys/native-require/plugins/rollup.js`.
``` js
const { nativeRequireRollupPlugin } = require('@tybys/native-require/plugins/rollup.js')module.exports = {
plugins: [
nativeRequireRollupPlugin(),
/* commonjs node-resolve ... */
]
}
`````` js
const { tryGetRequireFunction } = require('@tybys/native-require/index.js') // index.js can not be omitted
```ESM / TypeScript:
``` js
import { tryGetRequireFunction } from '@tybys/native-require'
const nativeRequire = tryGetRequireFunction()
if (typeof nativeRequire === 'function') {
// ...
}
```### Browser (Generally do not use) / Electron renderer process
``` html
(function () {
var nativeRequire = nr.tryGetRequireFunction(typeof module !== 'undefined' ? module : undefined)();
if (typeof nativeRequire === 'function') {
// ...
}
})();```
Examples are in `test` folder.