为什么我无法通过传递变量来确定因果垫片中require.context的路径? [英] Why can't I determine the path for require.context in a karma shim by passing a variable?

查看:604
本文介绍了为什么我无法通过传递变量来确定因果垫片中require.context的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在由karma调用的shim文件中设置require.context(path,...)的路径(在config中的files参数中设置),但是只要我以某种方式将变量用于路径,我在CLI中收到错误找不到模块."".这很奇怪,因为如果将非常相同路径硬编码到调用中,则该路径运行不会出现问题.再来一次,如果我愿意

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来检查一些莫名其妙的伏都教徒. /p>

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);
};

我想念什么?预处理器对同一垫片的调用是否会使事情搞砸了?

解决方案

Webpack不支持将文字以外的参数传递给require.context.项目所有者在github上给出的原因是:

它必须是静态可分析的...

理论上可以对此进行动态分析:

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在构建时而不是在运行时执行其分析.启用动态分析,除了会带来高昂的成本外,在许多用例场景中仍然无法使用.

I want to set the path for require.context(path, ...) in a shim file called by karma (set in the files parameter in the config) dynamically, but somehow, as soon as I use a variable for path, I get the error 'Cannot find module "."' in the CLI. This is very odd, because if I hardcode the very same path into the call, it runs without a problem. Aka, if I do

var testPath = '../src';
console.log("PATH 2 " + testPath); // ../src
var appContext = require.context(testPath, true, /\.spec\.ts/);

I'll get an error, if I do

var appContext = require.context('../src', true, /\.spec\.ts/);

everything is fine.

In the full shim file the code appears exactly as I have written it here, aka there is no other code between the definition of testPath and require.context, I just included the console.log to check for some inexplicable voodoo.

The shim is invoked as follows in the 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);
};

What am I missing? Does the call to the same shim by preprocessors mess things up?

解决方案

Webpack does not support passing parameters other than literals to require.context. The reason given by the project owner on github is:

It must be statically analyzable...

It would be possible in theory to do a dynamic analysis of this:

var testPath = '../src';
console.log("PATH 2 " + testPath); // ../src
var appContext = require.context(testPath, true, /\.spec\.ts/);

and discover that the first parameter to require.context is ../src. However, it gets more complicated when you have things like:

// 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/);

The code above cannot be meaningfully resolved by Webpack. Whether or not you are in a browser is a run time condition but Webpack is performing its analysis at build time, not at run time. Enabling dynamic analysis, besides the significant cost it would have, would still not work for a lot of use-case scenarios.

这篇关于为什么我无法通过传递变量来确定因果垫片中require.context的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆