npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

require-control

v3.0.0

Published

Get the full control over the nodejs module system.

Downloads

6,300

Readme

require-control Build Status Greenkeeper badge

This is a module-in-the-middle, require hack, path control, this is just a toolbox to master nodejs modules.

  • Fast relief againts the imports inside your node_modules.
  • A cure from "something.default is not a thing".
  • A way to tame module name resolution.

API

  • All commands returns a function to cancel side effects.
  • All commands could be run as many times as you wish, but only name resolution will make sense.

1. To cure all imports nodejs could look inside packages, stored out of sight of babel, inside node_modules

import 'require-control/register';

And then all imports inside node_modules will work

2. To make your project webpack-compact(babel interop require) by forcing .default to exist on exports

import 'require-control/interop-require';

And then default will always exists.

3. To create a loop-aware module cache

import {mirrorModuleCache} from 'require-control';
mirrorModuleCache((requireName, absoluteFilePath) => boolean);

And that will change how cache works for the selected files.

4. To control path resolution

import {nameResolution} from 'require-control';
nameResolution((requireName, parent) => newFileName);

import {setAliases} from 'require-control';
setAliases({
  'from': 'to'
});

And then all module aliases (webpack resolve, ts paths, aliases, anything)

PS: You can use setAliases to achieve 10-100 speedup against tsconfig-paths, if you are not using "complex" typescript paths.

PPS: setAlias is doing the same module-alias does - changes how Module._resolveFilename works. If you want to migrate to aliases from relative imports you might use relative-to-alias or restructor.

4.5 To control module resolution order

import {hoistExtensions} from 'require-control';

// make tsx, ts and jsx be picked first
hoistExtensions(['.tsx', '.ts', '.jsx']);

That could be quite usefull, if you have Component.less and Component.jsx or Component.tsx, as long as require('./Component') would load .less file - it goes before any other "non-native" extension.

PS: This command just moves listed extensions prior non-listed.

5. To apply any babel transformation, anywhere

babelIt(pick, babelSettings), where pick could return true, false, or code to pipe into babel.

import {babelIt} from 'require-control';

babelIt( fileName => fileName.includes('my-folder'), {
  babelrc: false,
  plugin: ['power-converter']
})

Examples

TypeScript + ES6 + Mocha

require('ts-node/register');  // support TS
require('babel-register');    // support ES

const { esm_modules, interopRequire, setAliases, resolutions } = require('require-control');

esm_modules();                // support ES in node_modules
interopRequire();             // "webpack" default imports everywhere in js,jsx,less,scss
interopRequire(['.less']);    // "webpack" default imports for .less files

setAliases({                  // why not!
  'common': path.join(root, 'src/common'),
  'components': path.join(root, 'src/components'),
});

resolutions(request => {      // custom resolution
  if (request === 'react') {
    return 'preact';
  }
});

Webpack aliases (or typescript paths)

const {setAliases } = require('require-control');

setAliases({                  
  'common': path.join(root, 'src/common'),
  'components': path.join(root, 'src/components'),
});

Would not be done without

This project would not be ever created without my work on rewiremock.

Licence

MIT