用 webpack 输出一个可执行文件 [英] Output an executable file with webpack
问题描述
我目前正在编写一个节点 CLI 工具并使用 webpack 来捆绑我的所有资产.此应用程序的入口点是我实际解析 process.argv
并运行命令的 js 文件(作为参考,我使用的是 tj/commander).这样,一旦捆绑完成,我就可以输入 ./
并且它会运行我的应用程序.入口文件如下所示:
I'm currently writing a node CLI tool and using webpack to bundle all of my assets. The entry point for this application is the js file where I actually parse process.argv
and run a command (For reference, I'm using tj/commander). This way, once the bundling is complete, I can enter ./<outputFile>
and it will run my application. The entry file looks like this:
import cli from './cli';
cli.parse(process.argv);
// If nothing was supplied
if (!process.argv.slice(2).length) {
cli.outputHelp();
}
捆绑工作正常,但我无法让 webpack 将文件作为可执行文件输出.一旦我运行 chmod +x <outputFile>
,一切正常.有没有办法告诉 webpack 授予输出文件的权限?
The bundling works fine but I can't get webpack to output the file as an executable. Once I run chmod +x <outputFile>
, everything works perfectly. Is there a way that I can tell webpack what permissions to grant an output file?
推荐答案
一种简单的方法是使用 npm.你的项目中有 package.json
吗?将 "build": "webpack && chmod +x outputFile"
添加到 package.json
的 scripts
部分并构建您的项目通过运行 npm run build
.
One simple way is to use npm. Do you have an package.json
in your project?
Add "build": "webpack && chmod +x outputFile"
to the scripts
section of your package.json
and build your project by running npm run build
.
另一种方法是将这些解决方案之一添加到您的 webpack.config.js
中:
Another way is to add one of these solutions to your webpack.config.js
:
来自这个答案的简单插件,它具有
pre
和post
构建处理程序
simple plugin from this answer which has
pre
andpost
build handlers
使用 on-build-webpack 插件,它会执行在 webpack 构建过程结束时添加 js 代码
use on-build-webpack plugin, which executes js code at the end of the webpack build process
无论您选择什么,您都需要添加这段代码:
Whatever you choose, you'll need to add this piece of code:
var chmod = require('chmod');
chmod("outputFile", 500);
这篇关于用 webpack 输出一个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!