Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I doesn't do anything #16

Open
thany opened this issue Jul 7, 2022 · 5 comments
Open

I doesn't do anything #16

thany opened this issue Jul 7, 2022 · 5 comments

Comments

@thany
Copy link

thany commented Jul 7, 2022

It's just a mock that always returns an empty set.

What if I actually need its real implementation to work?... Can you please include it as a working polyfill?

@sergei-startsev
Copy link

sergei-startsev commented Jul 7, 2022

It returns an empty set by design:

A Babel plugin that transforms webpack-specific require.context() into dummy function calls

If you need a real implementation, you can take a look at other solutions.

@thany
Copy link
Author

thany commented Jul 7, 2022

By design doesn't mean it has to stay that way forever. It might be a nice improvement.

@asapach
Copy link
Owner

asapach commented Jul 7, 2022

@thany, I'm not interested in implementing or maintaining a polyfill. However, if there is already an existing well-maintained polyfill, I'm open to making changes in order to support it. Do you have one in mind?

@thany
Copy link
Author

thany commented Jul 15, 2022

Sure! I'm sorry I don't have time to create a PR, but in the mean time I built one myself, slightly inspired on... Somewhere I don't remember 😞

So if you don't mind me just pasting it here:

'use strict';

const template = require('@babel/template');

module.exports = ({ types: t }) => {
  const contextTemplate = template.smart(
    `(function(...args) {
      const requireContext = (base = '.', scanSubDirectories = false, regularExpression = /\.js$/) => {
        const path = require('path');
        const fs = require('fs');
        const files = {};

        function readDirectory(directory) {
          fs.readdirSync(directory).forEach((file) => {
            const fullPath = path.resolve(directory, file);

            if (fs.statSync(fullPath).isDirectory()) {
              if (scanSubDirectories) readDirectory(fullPath);

              return;
            }

            if (!regularExpression.test(fullPath)) return;

            files[fullPath] = true;
          });
        }

        readDirectory(path.resolve(__dirname, base));

        function Module(file) {
          return require(file);
        }

        Module.keys = () => Object.keys(files);

        return Module;
      };
      return requireContext(...args);
    })()`);

  return {
    visitor: {
      CallExpression(path) {
        const node = path.node;
        if (t.isMemberExpression(node.callee)) {
          if (
            t.isIdentifier(node.callee.object, { name: 'require' }) &&
            t.isIdentifier(node.callee.property, { name: 'context' })
          ) {
            const ast = contextTemplate();
            ast.expression.arguments = [...node.arguments];
            path.replaceWith(ast);
          }
        }
      }
    }
  }
};

I'm sure you are more adept at writing a babel-plugin than I am, so this might not be up to spec. However, it does work (on my machine, on our project) 😀

@dominictobias
Copy link

dominictobias commented Jul 11, 2023

@thany, I'm not interested in implementing or maintaining a polyfill. However, if there is already an existing well-maintained polyfill, I'm open to making changes in order to support it. Do you have one in mind?

And the name of it is? Edit: https://www.npmjs.com/package/babel-plugin-require-context-hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants