Giter Site home page Giter Site logo

require-control's Introduction

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

require-control's People

Contributors

greenkeeper[bot] avatar matthewramsey avatar thekashey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

matthewramsey

require-control's Issues

preferCached

in some circumstances, file might not exists on the file system, but might exists in the cache

  • expose preferCached API to look at cache first, on the filesystem next

Upgrade Babel 6 to Babel 7 and/or make ESM detection more accurate

The current parser being used in babelIt is old and doesn't always work with more modern versions of JavaScript that are being used in certain packages. @testing-library/react is an example of this.

Here's a minimal replicable example:

require('require-control').esm_modules();
require('@testing-library/react');

You will then receive the following error:

Thrown:
{ [SyntaxError: unknown: Unexpected token (131:4)
  129 |       }
  130 |     },
> 131 |     ...(0, _dom.getQueriesForElement)(baseElement, queries)
      |     ^
  132 |   };
  133 | }
  134 | 
]

EDIT: As discussed below. This is an issue but the real issue is that @testing-library/react shouldn't be detected as using ESM syntax to begin with.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.