【发布时间】:2016-02-14 08:37:10
【问题描述】:
我看到 this answer 但 AFAICT 它不适合我。也许我在做一些愚蠢的事情。
我正在使用almond 和grunt-contrib-requirejs。我已经尝试了很多东西
这是我的布局
.
├── Gruntfile.js
├── 3rdparty
│ ├── require.js
├── src
│ ├── lib.js
│ └── main.js
└── node_modules
└── almond
└── almond.js
这是我的 grunt-contrib-requirejs 配置
requirejs: {
full: {
options: {
baseUrl: "./",
name: "node_modules/almond/almond.js",
include: [ "src/main.js" ],
out: "dist/app.js",
optimize: "none",
},
},
},
main.js 看起来像这样
requirejs(['./lib',], function(lib) {
lib.hello();
});
lib.js 看起来像这样
define([], function() {
return {
hello: function() {
console.log("hello from lib");
},
};
});
如果运行一个使用 require.js 的页面
<script src="3rdparty/require.js" data-main="src/main.js"></script>
运行良好。 You can see it live it here。检查控制台,你会看到它打印出hello from lib
所以我咕哝着跑。然后我运行一个使用dist/app.js 的页面,我得到了错误
Uncaught Error: undefined missing lib
这是live page。
查看生成的dist/app.js我看到lib已经变成了这个
define('src/lib',[], function() {
...
});
main 是这样包含的
requirejs(['./lib'], function(lib) {
...
});
换句话说,r.js 生成的 id src/lib 与 main 引用的 id 不匹配 ./lib。
这似乎是 r.js 的一个非常直接的示例。就像“hello world”一样。
我做错了什么?
我尝试过的一件事是将baseUrl 更改为./src
requirejs: {
full: {
options: {
baseUrl: "./src",
name: "node_modules/almond/almond.js",
include: [ "src/main.js" ],
out: "dist/app.js",
optimize: "none",
},
},
},
但现在我明白了
{ [Error: Error: ENOENT: no such file or directory, open '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js'
at Error (native)
]
originalError:
{ [Error: ENOENT: no such file or directory, open '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js']
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js',
fileName: '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js' } }
所以我尝试修复杏仁路径
requirejs: {
full: {
options: {
baseUrl: "./src",
name: "../node_modules/almond/almond.js",
include: "main",
out: "dist/app.js",
optimize: "none",
},
},
},
但这也失败了
{ [Error: Error: ERROR: module path does not exist: ../node_modules/almond/almond.js for module named: ../node_modules/almond/almond.js. Path is relative to: /Users/gregg/temp/grunt-contrib-requirejs-example
at /Users/gregg/temp/grunt-contrib-requirejs-example/node_modules/requirejs/bin/r.js:30214:35
]
originalError: [Error: ERROR: module path does not exist: ../node_modules/almond/almond.js for module named: ../node_modules/almond/almond.js. Path is relative to: /Users/gregg/temp/grunt-contrib-requirejs-example] }
我没有得到什么?
【问题讨论】: