我试图让我的代码使用显然是非标准的设置来优化到单个文件,并且我很难理解所有不同路径的含义。
想象一下,我正在制作包含杏仁的r
the requirejs command-line tool的变体。因此,我希望能够使用我的工具附带的杏仁包裹杏仁。
我的目录结构
someAppFolder
|
+--main.js
在相关文件夹中
myRPlusAlmondBuilder
|
+--myRPlusAlmonBuilder.js
|
+--node_modules
|
+--almond
| |
| +--almond.js
|
+--requirejs
|
...
所以,我希望myRPlusAlmondBuilder
能够像main.js
一样构建一个单独的JS包
cd myRPlusAlmondBuilder
node myRPlusAlmondBuilder.js someAppFolder/main.js
这是一个简单的myRPlusAlmondBuilder.js
var requirejs = require('requirejs');
var path = require('path');
var filename = process.argv[2];
var dirname = path.dirname(filename);
var barename = path.basename(filename, ".js");
var config = {
baseUrl: dirname,
name: "node_modules/almond/almond",
include: barename,
insertRequire: [barename],
out: "out.js",
wrap: true,
};
// show the values
console.log(JSON.stringify(config, undefined, " "));
requirejs.optimize(config, function() {
console.log("result is in:", config.out);
}, function(err) {
console.error(err);
});
像这样称呼它
cd myRPlusAlmondBuilder.js
node myRPlusAlmondBuilder.js /Users/gregg/temp/delme-requirejs/someAppFolder/main.js
但我得到
{
"baseUrl": "/Users/gregg/temp/delme-requirejs/someAppFolder",
"name": "node_modules/almond/almond",
"include": "main",
"insertRequire": [
"main"
],
"out": "out.js",
"wrap": true
}
{ [Error: Error: ERROR: module path does not exist: /Users/gregg/temp/delme-requirejs/someAppFolder/node_modules/almond/almond.js for module named: node_modules/almond/almond. Path is relative to: /Users/gregg/temp/delme-requirejs/myRPlusAlmondBuilder
尝试name
的所有其他值都失败
// dot in front
name: "./node_modules/almond/almond",
// .js on end
name: "node_modules/almond/almond.js",
// .dot in front and .js on end
name: "./node_modules/almond/almond.js",
// absolute path to almond.js with .js
name: path.join(__dirname, "node_modules/almond/almond.js"),
// absolute path to almond.js without .js
name: path.join(__dirname, "node_modules/almond/almond"),
" .js"获得
[Error: Error: ENOENT, no such file or directory '/Users/gregg/temp/delme-requirejs/someAppFolder/node_modules/almond/almond.js'
at Error (native)
]
具有完整路径的那些
[Error: Error: ENOENT, no such file or directory '/Users/gregg/temp/delme-requirejs/someAppFolder//Users/gregg/temp/delme-requirejs/myRPlusAlmondBuilder/node_modules/almond/almond.js'
at Error (native)
]
使用从main.js到杏仁的相对路径
name: path.relative(dirname, path.join(__dirname, "node_modules/almond/almond.js")),
但是,如果C:/somefolder/myRPlusAlmond
中的工具和D:/someAppFolder/main.js
中的文件
有没有办法使用杏仁的绝对路径?
答案 0 :(得分:0)
我发现了一种添加杏仁路径的方法
var config = {
baseUrl: dirname,
name: "__dontclash_almond__/almond",
include: barename,
insertRequire: [barename],
out: "out.js",
wrap: true,
paths: {
__dontclash_almond__: path.join(__dirname, "node_modules/almond"),
}.
};
看起来有点像黑客,因为我不得不希望我不会与用户选择的名字发生冲突。当然不太可能发生冲突