Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep my shebang in place using webpack?

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

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...

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

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; .............................................................. 

but what i need is

#!/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;  .............................................................. 
like image 995
Rafael Milewski Avatar asked Nov 23 '16 02:11

Rafael Milewski


People also ask

How does webpack bundling work?

Webpack is a module bundler. It takes disparate dependencies, creates modules for them and bundles the entire network up into manageable output files. This is especially useful for Single Page Applications (SPAs), which is the defacto standard for Web Applications today.

Is webpack worth using?

Should I Use Webpack? If you're building a complex Front End™ application with many non-code static assets such as CSS, images, fonts, etc, then yes, Webpack will give you great benefits.

How does webpack config work?

Webpack is first and foremost a bundler. Webpack's base functionality is to take a JavaScript file, resolve any dependencies ( require() , import , etc.), and output a bundled JavaScript file that contains all those dependencies. You can then run the bundled file without having to load those dependencies again.

Is webpack used in backend?

Why to use webpack on node backend. If we are talking about react and node app you can build isomorphic react app. And if you are using import ES6 Modules in react app on client side it's ok - they are bundled by webpack on the client side.


1 Answers

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.

In your webpack.config.js file:

plugins: [     new webpack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }), ] 
like image 134
spacek33z Avatar answered Oct 08 '22 07:10

spacek33z