Package Exports
- @dozerg/require-module
- @dozerg/require-module/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@dozerg/require-module) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Require Module
Search and require a module that won't be webpack'ed.
Usage
npm i @dozerg/require-moduleAPIs
requireModule(moduleName: string, fromPath: string)
Search and load a module from given path.
If not found, undefined will be returned.
import requireModule from '@dozerg/require-module';
const filePath = '/path/to/a/file.ts';
// Search and find Prettier for given source file.
const pt = requireModule('prettier', filePath);
if(pt === undefined) {
console.log('Cannot find Prettier from given path.');
} else {
// Resolve Prettier config for given source file.
const config = await pt.resolveConfig(filePath);
//...
}When you webpack the above code, prettier and its dependencies will NOT be packed.
At runtime, the above code will try to require('prettier') from filePath and its parent directories, where node_modules/prettier can be found.
requireModule(moduleName: string, fromPath: string, defaultModule: any)
Search and load a module from given path.
If not found, defaultModule will be returned.
import prettier from 'prettier';
import requireModule from '@dozerg/require-module';
const filePath = '/path/to/a/file.ts';
// Search and find Prettier for given source file.
// If not found, use 'prettier' instead.
const pt = requireModule('prettier', filePath, prettier);
// Resolve Prettier config for given source file.
const config = await pt.resolveConfig(filePath);
//...When you webpack the above code, prettier and its dependencies WILL be packed.
At runtime, the above code will try to require('prettier') from filePath and its parent directories, where node_modules/prettier can be found.
If not found, the packed prettier will be used.
License
MIT © Zhao DAI daidodo@gmail.com