0.0.7 • Published 1 month ago

@dozerg/require-module v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Require Module

npm Downloads Build Status

Search and require a module that won't be webpack'ed.

Usage

npm i @dozerg/require-module

APIs

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

0.0.7

1 month ago

0.0.6

5 months ago

0.0.5

5 months ago

0.0.4

8 months ago

0.0.3

1 year ago

0.0.2

2 years ago

0.0.1

2 years ago