简体   繁体   English

业力和requirejs:如何在requirejs.config中指定另一个项目的路径?

[英]Karma and requirejs: How to specify path from another project in requirejs.config?

I am using karma.js toghether with require.js in a VisualStudio solution. 我在Visual Studio解决方案中将karma.jsrequire.js一起使用。 My solution has two projects, one for the code and one for the tests. 我的解决方案有两个项目,一个用于代码,一个用于测试。

Following require configuration works fine for me if I reference libraries that are located in my Test.Project: 如果我引用位于Test.Project中的库,则以下require配置对我来说很好用:

requirejs.config({   
  baseUrl: '/base',

  paths: {   
    'jquery': 'bower_components2/jquery/dist/jquery'    
  },

  deps: tests,   
  callback: window.__karma__.start
});

Here is part of my folder structure: 这是我的文件夹结构的一部分:

- Solution
  + Code.Project
    * App
    * bower_components1
  + Test.Project
    * App
    * bower_components2
    * karma.conf.js

I would like to reference jquery from Code.Project and not from Test.Project : 我想从Code.Project而不是Test.Project 引用jquery

requirejs.config({   
  baseUrl: '/base',

  paths: {   
    'jquery': '../Code.Project/bower_components1/jquery/dist/jquery'    
  },

  deps: tests,   
  callback: window.__karma__.start
});

However, that does not work. 但是,这不起作用。 Jquery can not be found and I get following error: 找不到Jquery,出现以下错误:

Uncaught Error: Script error for "jquery", needed by: App/app.spec.js
  http://requirejs.org/docs/errors.html#scripterror
  at node_modules/requirejs/require.js:143

=> Is there a restriction that does not allow me to go upward from /base ? =>是否存在不允许我从/ base向上移动的限制? Should I use something different as baseUrl (I tried / and ./ but did not get it working)? 我应该使用其他东西作为baseUrl吗(我尝试过/和./,但没有使其正常工作)? Or is there some mistake in my relative path? 还是我的相对路线有误?

(Here is a full example on how to use requirejs with karma that I used as a starting point: https://github.com/kjbekkelund/karma-requirejs ) (以下是一个完整示例,说明如何将requirejs与业力一起使用: https//github.com/kjbekkelund/karma-requirejs

I just found a related answer (see below). 我刚刚找到了一个相关的答案(见下文)。

It might also make more sense to put the karma.conf.js directly in the Solution folder and not inside the test project. 将karma.conf.js直接放在Solution文件夹中而不是放在测试项目中也可能更有意义。 Then I can directly enter "karma start" in the console. 然后,我可以在控制台中直接输入“ karma start”。 (The default path for the NuGet Package Manager Console / PowerShell is the solution folder.) (NuGet软件包管理器控制台/ PowerShell的默认路径是解决方案文件夹。)

From Karma and RequireJS getting files outside base url Karma和RequireJS获取基本URL之外的文件

As you wrote it yourself 当你自己写的时候

// Karma serves files under /base, which is the basePath from your config file baseUrl: '../', // Karma在/ base下提供文件,该文件是配置文件baseUrl中的basePath:'../',

So it means if you want to be able to access files deeper than that you need to shift all your url with a: 因此,这意味着如果您希望能够访问比其更深的文件,则需要使用以下命令移动所有网址:

baseUrl: '../..', baseUrl:'../ ..',

You could also use base/ which will be understood by karma as the basePath of your karmaConf.js 您还可以使用base /,它将被karma理解为karmaConf.js的basePath

For a better understanding on how the basePath of karma.conf and test-main.js works you can have a look at this: http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/ 为了更好地了解karma.conf和test-main.js的basePath的工作方式,您可以查看一下: http ://monicalent.com/blog/2015/02/11/karma-tests-angular-js -require-J /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM