【发布时间】:2017-06-04 09:32:18
【问题描述】:
我的目标是创建一个文件
- 要求目录中的所有 JS 文件不以
_test.js结尾 - 做一个
module.exports等于从这些视图文件返回的模块名称数组。
我以为我有这个:
// Automagically crawls through this directory, finds every js file inside any
// subdirectory, removes test files, and requires the resulting list of files,
// registering the exported module names as dependencies to the myApp.demoApp.views module.
var context = require.context('.', true, /\/.*\/.*\.js$/);
var moduleNames = _.chain(context.keys())
.filter(function(key) {
console.log(key, key.indexOf('_test.js') == -1);
return key.indexOf('_test.js') == -1;
})
.map(function(key) {
console.log("KEY", key);
return context(key)
})
.value();
module.exports = angular.module('myApp.demoApp.views', moduleNames).name;
#2 正在按预期工作
#1 不幸的是我太天真了。虽然模块名称被过滤掉,但这仍然需要所有带有_test 的文件,因此测试文件最终会出现在我构建的代码中。
我试图通过更新正则表达式来解决这个问题,但 JS 不支持正则表达式负后视,而且我没有足够的正则表达式知识来做到这一点。
【问题讨论】:
-
它为我提供了足够的信息来解决我的问题。谢谢!
-
欢迎您。在这种情况下以重复的形式关闭您的问题
-
在下面查看我的答案。你认为它足够不同,值得坚持吗?我觉得对于任何使用 webpack 和
index.views.js约定的人来说,这是一个流行的用例,这很流行。
标签: javascript angularjs webpack