@dozerg/require-module
v0.0.10
Published
Search and require a module that won't be webpack'ed.
Maintainers
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 [email protected]
