为什么我不能通过传递变量来确定 karma shim 中 require.context 的路径?
Why can't I determine the path for require.context in a karma shim by passing a variable?
我想在 karma 调用的垫片文件中动态设置 require.context(path, ...) 的路径(在配置文件参数中设置),但不知何故,一旦我为路径使用变量,我在 CLI 中收到错误 'Cannot find module "."'。这很奇怪,因为如果我将 very same 路径硬编码到调用中,它会毫无问题地运行。也就是说,如果我这样做
var testPath = '../src';
console.log("PATH 2 " + testPath); // ../src
var appContext = require.context(testPath, true, /\.spec\.ts/);
如果我这样做,我会得到一个错误
var appContext = require.context('../src', true, /\.spec\.ts/);
一切都很好。
在完整的 shim 文件中,代码与我在这里写的完全一样,也就是在 testPath 的定义和 require.context 之间没有其他代码,我只是包含了 console.log 来检查对于一些莫名其妙的巫术。
shim 在 karma.conf.js 中调用如下:
module.exports = function (config) {
var _config = {
.....
files: [
{pattern: './karma-test-shim.js', watched: true}
],
preprocessors: {
'./karma-test-shim.js': ['webpack', 'sourcemap']
},
.....
};
config.set(_config);
};
我错过了什么?预处理器调用同一个 shim 是否会把事情搞砸?
Webpack 不支持将文字以外的参数传递给 require.context
。项目所有者在 github 上给出的 reason 是:
It must be statically analyzable...
理论上可以对此进行动态分析:
var testPath = '../src';
console.log("PATH 2 " + testPath); // ../src
var appContext = require.context(testPath, true, /\.spec\.ts/);
发现 require.context
的第一个参数是 ../src
。但是,当您遇到以下情况时,它会变得更加复杂:
// If in browser use "foo", otherwise use "bar". (The test has been
// simplified as it is not our focus here.)
var testPath = (typeof window !== "undefined") ? "foo" : "bar";
var appContext = require.context(testPath, true, /\.spec\.ts/);
Webpack 无法有意义地解析上述代码。您是否在浏览器中是一个 运行 时间条件,但 Webpack 在构建时执行其分析,而不是在 运行 时间。启用动态分析,除了会产生大量成本之外,仍然不适用于很多用例场景。
我想在 karma 调用的垫片文件中动态设置 require.context(path, ...) 的路径(在配置文件参数中设置),但不知何故,一旦我为路径使用变量,我在 CLI 中收到错误 'Cannot find module "."'。这很奇怪,因为如果我将 very same 路径硬编码到调用中,它会毫无问题地运行。也就是说,如果我这样做
var testPath = '../src';
console.log("PATH 2 " + testPath); // ../src
var appContext = require.context(testPath, true, /\.spec\.ts/);
如果我这样做,我会得到一个错误
var appContext = require.context('../src', true, /\.spec\.ts/);
一切都很好。
在完整的 shim 文件中,代码与我在这里写的完全一样,也就是在 testPath 的定义和 require.context 之间没有其他代码,我只是包含了 console.log 来检查对于一些莫名其妙的巫术。
shim 在 karma.conf.js 中调用如下:
module.exports = function (config) {
var _config = {
.....
files: [
{pattern: './karma-test-shim.js', watched: true}
],
preprocessors: {
'./karma-test-shim.js': ['webpack', 'sourcemap']
},
.....
};
config.set(_config);
};
我错过了什么?预处理器调用同一个 shim 是否会把事情搞砸?
Webpack 不支持将文字以外的参数传递给 require.context
。项目所有者在 github 上给出的 reason 是:
It must be statically analyzable...
理论上可以对此进行动态分析:
var testPath = '../src';
console.log("PATH 2 " + testPath); // ../src
var appContext = require.context(testPath, true, /\.spec\.ts/);
发现 require.context
的第一个参数是 ../src
。但是,当您遇到以下情况时,它会变得更加复杂:
// If in browser use "foo", otherwise use "bar". (The test has been
// simplified as it is not our focus here.)
var testPath = (typeof window !== "undefined") ? "foo" : "bar";
var appContext = require.context(testPath, true, /\.spec\.ts/);
Webpack 无法有意义地解析上述代码。您是否在浏览器中是一个 运行 时间条件,但 Webpack 在构建时执行其分析,而不是在 运行 时间。启用动态分析,除了会产生大量成本之外,仍然不适用于很多用例场景。