如何使用Babel进行CLI程序?

时间:2021-08-11 15:40:31

I'm trying to write some CLI program on node using Babel. I'd seen question How do I use babel in a node CLI program? and there loganfsmyth said:

我正在尝试使用Babel在节点上编写一些CLI程序。我见过问题如何在节点CLI程序中使用babel? loganfsmyth说:

Ideally you'd precompile before distributing your package.

理想情况下,在分发包之前,您需要进行预编译。

Okay, now I'm using:

好的,现在我正在使用:

"scripts": {
    "transpile": "babel cli.js --out-file cli.es5.js",
    "prepublish": "npm run transpile",
}

But, I faced the problem, when Babel adds 'use strict'; line right behind #!/usr/bin/env node header. For example, if I have cli.js:

但是,当Babel添加'use strict'时,我遇到了问题;排在#!/ usr / bin / env节点标题后面。例如,如果我有cli.js:

#!/usr/bin/env node

import pkg from './package'

console.log(pkg.version);

I'll get this:

我会得到这个:

#!/usr/bin/env node'use strict';

var _package = require('./package');

… … …

And this doesn't work. When I try to run it, I always get:

这不起作用。当我尝试运行它时,我总是得到:

/usr/bin/env: node'use strict';: This file or directory does'nt exist

How can I solved this problem?

我怎样才能解决这个问题?

2 个解决方案

#1


6  

The solution from @DanPrince is perfectly acceptable but there's an alternative

来自@DanPrince的解决方案是完全可以接受的,但还有另一种选择

cli.js

keep this file es5

保留此文件es5

#!/usr/bin/env node
require("./run.es5.js");

run.js

// Put the contents of your existing cli.js file here,
// but this time *without* the shebang
// ...

Update your scripts to

将脚本更新为

"scripts": {
    "transpile": "babel run.js > run.es5.js",
    "prepublish": "npm run transpile",
}

The idea here is that the cli.js shim doesn't need to be tranpsiled so you can keep your shebang in that file.

这里的想法是cli.js垫片不需要传送,所以你可以将你的shebang保存在该文件中。

cli.js will just load run.es5.js which is the babel-transpiled version of run.js.

cli.js将只加载run.es5.js,这是run.js的babel-transiled版本。

#2


1  

You could use another NPM script to add the shebang as the last part of your build process. It ain't pretty but it works.

您可以使用另一个NPM脚本将shebang添加为构建过程的最后一部分。它不漂亮,但它的工作原理。

"scripts": {
  "transpile": "babel cli.js --out-file es5.js",
  "shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js",
  "prepublish": "npm run transpile && npm run shebang",
}

Then your original cli.js would become

然后你的原始cli.js将成为

import pkg from './package'

console.log(pkg.version);

The resulting es5.js file becomes

生成的es5.js文件变为

'use strict';

var _package = require('./package');

And finally, cli.es5.js becomes

最后,cli.es5.js成为了

#!/usr/bin/env node
'use strict';

var _package = require('./package');

This could be improved with a clean script.

这可以通过干净的脚本来改进。

"scripts": {
  "transpile": "babel cli.js --out-file es5.js",
  "shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js",
  "clean": "rm es5.js cli.es5.js",
  "prepublish": "npm run clean && npm run transpile && npm run shebang",
}

Of course, this requires that you are on a system with bash (or some alternate compatible shell), however you could make it cross-platform by rewriting the build scripts to use node implementations of these commands with something like ShellJS.

当然,这需要您使用bash(或某些备用兼容shell)的系统,但是您可以通过重写构建脚本以使用ShellJS之类的命令来实现这些命令的节点实现,从而使其跨平台。

#1


6  

The solution from @DanPrince is perfectly acceptable but there's an alternative

来自@DanPrince的解决方案是完全可以接受的,但还有另一种选择

cli.js

keep this file es5

保留此文件es5

#!/usr/bin/env node
require("./run.es5.js");

run.js

// Put the contents of your existing cli.js file here,
// but this time *without* the shebang
// ...

Update your scripts to

将脚本更新为

"scripts": {
    "transpile": "babel run.js > run.es5.js",
    "prepublish": "npm run transpile",
}

The idea here is that the cli.js shim doesn't need to be tranpsiled so you can keep your shebang in that file.

这里的想法是cli.js垫片不需要传送,所以你可以将你的shebang保存在该文件中。

cli.js will just load run.es5.js which is the babel-transpiled version of run.js.

cli.js将只加载run.es5.js,这是run.js的babel-transiled版本。

#2


1  

You could use another NPM script to add the shebang as the last part of your build process. It ain't pretty but it works.

您可以使用另一个NPM脚本将shebang添加为构建过程的最后一部分。它不漂亮,但它的工作原理。

"scripts": {
  "transpile": "babel cli.js --out-file es5.js",
  "shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js",
  "prepublish": "npm run transpile && npm run shebang",
}

Then your original cli.js would become

然后你的原始cli.js将成为

import pkg from './package'

console.log(pkg.version);

The resulting es5.js file becomes

生成的es5.js文件变为

'use strict';

var _package = require('./package');

And finally, cli.es5.js becomes

最后,cli.es5.js成为了

#!/usr/bin/env node
'use strict';

var _package = require('./package');

This could be improved with a clean script.

这可以通过干净的脚本来改进。

"scripts": {
  "transpile": "babel cli.js --out-file es5.js",
  "shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js",
  "clean": "rm es5.js cli.es5.js",
  "prepublish": "npm run clean && npm run transpile && npm run shebang",
}

Of course, this requires that you are on a system with bash (or some alternate compatible shell), however you could make it cross-platform by rewriting the build scripts to use node implementations of these commands with something like ShellJS.

当然,这需要您使用bash(或某些备用兼容shell)的系统,但是您可以通过重写构建脚本以使用ShellJS之类的命令来实现这些命令的节点实现,从而使其跨平台。