如何使用 webpack 保持我的shebang?

编程入门 行业动态 更新时间:2024-10-28 12:17:02
本文介绍了如何使用 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...

但是..我没有找到任何方法将 #!/usr/bin/env node 保留在我的 cli 文件的顶部,有什么想法吗?

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?

本文发布于:2023-10-20 21:19:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   webpack   shebang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!