Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 根据文件系统信息在webpack中打包和公开react组件_Node.js_Reactjs_Webpack_Reactjs.net - Fatal编程技术网

Node.js 根据文件系统信息在webpack中打包和公开react组件

Node.js 根据文件系统信息在webpack中打包和公开react组件,node.js,reactjs,webpack,reactjs.net,Node.js,Reactjs,Webpack,Reactjs.net,我将reactjs.net与Webpack一起使用,并尝试加载与index.js位于同一文件夹/子文件夹中的所有组件 //Components/index.js var fs = require('fs'); var path = require('path'); var process = require("process"); var directory = __dirname; function addFile(file, path, exports) { if (!file.end

我将reactjs.net与Webpack一起使用,并尝试加载与index.js位于同一文件夹/子文件夹中的所有组件

//Components/index.js
var fs = require('fs');
var path = require('path');
var process = require("process");
var directory = __dirname;

function addFile(file, path, exports) {
  if (!file.endsWith(".jsx")) return exports;
  exports[file.substring(0,file.length-4)] = require(path);
  return exports;
}
function addFolder(dir, exports) {
    fs.readdir(dir, function (err, files) {
        if (err) {
            console.error("Could not list the directory.", err);
            process.exit(1);
        }

        files.forEach(function (file, index) {
            var currentPath = path.join(dir, file);
                fs.stat(dir, function (error, stat) {
                    if (error) {
                        console.error("Error stating file.", error);
                        return;
                    }
                    if (stat.isFile())
                        exports = addFile(file, currentPath, exports);
                    else if (stat.isDirectory())
                        exports = addFolder(currentPath, exports);

                });
            });
            return exports;
        });
    }
module.exports = addFolder(directory, {});
然后需要index.js,如下所示:

//Content/client.js
var Components = require('expose-loader?Components!./../Components');

当然,Webpack只是将整个addFolder函数打包到输出中,这在客户端当然不起作用。很明显,我想执行index.js中的代码,所以现在我很好奇如何加载我的所有组件,而不用手动声明并在components对象中全局公开它们,同时使用文件名作为标识符。

我不再需要答案,因为我不需要公开所有组件,只是一些作为切入点,用手做起来并不那么痛苦。