使用grunt-requirejs编译时出现"no such file"错误 [英] 'no such file' error when using grunt-requirejs compile
问题描述
由于require.js构建的大多数问题都与文件结构和相对路径引用有关,因此我在此处创建了一个存储库:
Since most issues with require.js build has to do with file structure and relative path reference, I created a repo here: https://github.com/ttback/requirejs-example for easier troubleshoot.
错误是当我运行grunt时,我会得到no such file or directory requirejs-example/src/js/bundle/js/bundle/utils.js
The error is when i run grunt, I will get no such file or directory requirejs-example/src/js/bundle/js/bundle/utils.js
这是由于错误的baseUrl.我希望它是src/
,但是我无法设置它,因为它可以根据我的Gruntfile查找src/js/bundle/main.js
的依赖项.因此,基准位于src/js/bundle
.如果将main.js内utils.js的相对路径从.js/bundle/utils.js
更改为./utils.js
,则当前的main.js可与index.html一起使用,该应用程序将中断.
This is due to the wrong baseUrl. I want it to be src/
but I can't set it since it goes to find the dependencies for src/js/bundle/main.js
based on my Gruntfile. So the base is at src/js/bundle
. The current main.js works with the index.html, if I change the relative path to utils.js from .js/bundle/utils.js
to ./utils.js
inside main.js, the app wil break.
有什么办法可以使grunt-requirejs与我现有的东西一起工作吗?
Is there any way I can make the grunt-requirejs work with what I have?
推荐答案
首先,要添加一个体面的代码示例.
First of all, points for adding a decent code example.
这个问题实际上很容易解决.
The problem is easy to solve actually.
只需更改:
var utilsObject = require('./js/bundle/utils.js');
收件人:
var utilsObject = require('./utils');
现在,构建工具和应用程序将运行.
Now the build tool and the app will work.
通过添加.js
后缀,可以绕过RequireJS适用于模块路径的baseUrl
规则.从文档中:
By adding a .js
suffix you were bypassing the baseUrl
rules that RequireJS applies to module paths. From the docs:
RequireJS在默认情况下还假设所有依赖项都是脚本, 因此,它不希望看到尾随的".js".模块ID的后缀.
RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs.
有时候您确实想直接引用脚本,然后 不符合"baseUrl +路径"找到它的规则.如果是模块 ID具有以下特征之一,因此不会传递ID 通过"baseUrl +路径"配置,就像 相对于文档的常规URL:
There may be times when you do want to reference a script directly and not conform to the "baseUrl + paths" rules for finding it. If a module ID has one of the following characteristics, the ID will not be passed through the "baseUrl + paths" configuration, and just be treated like a regular URL that is relative to the document:
- 以".js"结尾.
- 以"/"开头.
- 包含URL协议,例如"http:"或"https:".
http://requirejs.org/docs/api.html#jsfiles
这篇关于使用grunt-requirejs编译时出现"no such file"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!