RequireJs Optimization on Windows fails

2019-02-27 19:26发布

问题:

Trying to optimize an requirejs-based project on Windows. Have put the r.js.cmd in the Scripts folder along with the following nodeBuild.js file.

({
baseUrl: ".",
paths: {
    jquery: "empty:"
},
name: "main",
out: "main-built.js"
})

When using the command

node r.js.cmd -o nodeBuild.js

I get the following response:

r.js.cmd:1
(function (exports, require, module, __filename, __dirname) { @IF EXIST "%~dp0
                                                              ^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

Tried searching for it but can't find similar error description and since it is windows I use the r.js.cmd instead of the r.js. The requirejs main.js file is in the Scripts folder and all other modules is in subdirectories to that.

回答1:

Don't run it through node.

r.js.cmd -o nodeBuild.js


回答2:

Based on the documentation it seems to me that the equivalent of a *nix:

r.js -o nodeBuild.js

would be on Windows:

r.js.cmd -o nodeBuild.js

That is, don't pass it to Node but let Windows run it. r.js.cmd is a Windows command file, which Node cannot interpret. When you do it like you've shown in your question, Node tries to interpret that file and fails.