-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
By design doesn't mean it has to stay that way forever. It might be a nice improvement. |
@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? |
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) 😀 |
And the name of it is? Edit: https://www.npmjs.com/package/babel-plugin-require-context-hook |
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?
The text was updated successfully, but these errors were encountered: