【发布时间】:2017-03-18 12:54:19
【问题描述】:
我有一些老式的普通 ES5 JavaScript,我需要将它们拉入我使用 webpack 作为捆绑器的应用程序中。问题是这些文件的位置在一个路径是动态的文件夹中。似乎 webpack 不能很好地处理 require 中的表达式,而 require.context 根本不允许它们。
对于需要一个目录及其子目录中所有文件的已知路径目录,这种方法效果很好:
let domainRequires = require.context('./app/domain', true, /\.js$/);
domainRequires.keys().forEach(domainRequires);
由于我不能在require.context 中使用表达式,我尝试只使用普通的require,它不起作用:
require('../../path/to/code/my-library-' + versionNumber + '/domain/clazz.js');
使用完整的相对路径。
我也尝试过使用require.context,但增强了正则表达式部分以使其正常工作并且没有运气:
require.context('../../path/to/code', true, /^my-library-.*\/domain\/.*\.js$/);
欢迎任何想法、建议或解决方案。如果我需要在 webpack 中使用 3rd 方,那也没关系。
【问题讨论】: