如何使用webpack保持我的shebang? [英] How to keep my shebang in place using webpack?

查看:166
本文介绍了如何使用webpack保持我的shebang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让Webpack在我的文件顶部保留#!/ usr / bin / env节点

is there any way to make Webpack keep the #!/usr/bin/env node at the top of my file?

我正在尝试将CLI与模块捆绑在一起......分别导出我的 index.js / cli.js 有点棘手只使用一个配置文件...并使cli需要索引...我得到它的工作...

I'm trying to bundle a CLI along with a module... it was a bit tricky to export my index.js / cli.js separately using just one configuration file... and making the cli require index... i got it working...

然而..我没有找到任何方法保留我的cli文件顶部的#!/ usr / bin / env节点,有什么想法吗?

However.. i didn't find any way to keep the #!/usr/bin/env node at the top of my cli file, any ideas?

简而言之,webpack输出如下文件:

in shorts webpack outputs an file like this:

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};

/******/    // The require function
/******/    function __webpack_require__(moduleId) {

/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;
..............................................................

但我需要的是

#!/usr/bin/env node //<------ HEREEEE

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};

/******/    // The require function
/******/    function __webpack_require__(moduleId) {

/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;

..............................................................


推荐答案

您应该可以使用 BannerPlugin 原始模式。使用此插件,您可以在捆绑包顶部添加所需的任何字符串。通过使用原始模式,它不会将字符串包装在注释中。

You should be able to use BannerPlugin with raw mode for this. With this plugin you can add any string you want at the top of your bundle. By using the raw mode, it will not wrap the string in a comment.

webpack.config.js 文件:

plugins: [
    new webpack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }),
]

这篇关于如何使用webpack保持我的shebang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆