需要模块
搜索并需要一个不会被网络包装的模块.
使用量
npm i @dozerg/require-module
APIs 密码
requireModule(moduleName: string, fromPath: string)
从给定路径搜索并加载一个模块 .
如果没有找到, undefined 将返回.
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);
//...
}
当你用网络包装上面的代码时, prettier 其依附者,将不被集合.
运行时,上面的代码会尝试 require('prettier') 从 filePath 及其父目录, 其中 node_modules/prettier 可见.
requireModule(moduleName: string, fromPath: string, defaultModule: any)
从给定路径搜索并加载一个模块 .
如果没有找到, defaultModule 将返回.
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);
//...
当你用网络包装上面的代码时, prettier 和它的依附者将被包装.
运行时,上面的代码会尝试 require('prettier') 从 filePath 及其父目录, 其中 node_modules/prettier 可见.
如果找不到, 包装 prettier 将被使用.
许可证
麻省理工学院 达伊多多@gmail.com