s00d / webpack-shell-plugin-next

Run shell commands either before or after webpack 4 and 5 builds
MIT License
92 stars 12 forks source link

npm version npm downloads NPM license npm type definitions Build Status donate GitHub Repo stars

Webpack Shell Plugin Next

fix webpack deprecated method. add typescript and other

This plugin allows you to run any shell commands before or after webpack 5 builds. This will work for both webpack 5.

Goes great with running cron jobs, reporting tools, or tests such as selenium, protractor, phantom, ect.

Webpack compatibility

Webpack webpack-shell-plugin-next
*-4.x 1.*
5.x 2.*

WARNING

This plugin is meant for running simple command line executions. It is not meant to be a task management tool.

Installation

npm install --save-dev webpack-shell-plugin-next

Setup

In webpack.config.js:

const WebpackShellPluginNext = require('webpack-shell-plugin-next');
...
module.exports = {
  //...
  plugins: [
    new WebpackShellPluginNext({
      onBuildStart:{
        scripts: ['echo "Webpack Start"'],
        blocking: true,
        parallel: false
      }, 
      onBuildEnd:{
        scripts: ['echo "Webpack End"'],
        blocking: false,
        parallel: true
      }
    })
  ]
  //...
}

More example in webpack.config.ts

API

Default for all: {scripts: [],blocking: false,parallel: false}

Note: below combination is not supported.

{
  blocking: true
  parallel: true
} 

Other global params

new WebpackShellPlugin({
      onBeforeNormalRun: {
        // ...
      },
      dev: false,
      safe: false,
      logging: true
    })
  ]
}

TypeScript

This project is written in TypeScript, and type declarations are included. You can take advantage of this if your project's webpack configuration is also using TypeScript (e.g. webpack.config.ts and webpack.config.js).

Function in scripts

how to use functions in the queue?

Example:

{
  {
    scripts: [
      // sync
      () => {
        console.log('run tTimeout 1');
        setTimeout(() => console.log('end Timeout 1'), 1000);
      },
      // async
      () => new Promise((resolve, reject) => {
        console.log('run async tTimeout');
        setTimeout(() => {
          console.log('end async tTimeout');
          resolve('ok');
        }, 1000);
      }),
    ],
      blocking: true
  }
}
// use exec
import * as os from 'os'
// ..
{
    safe: os.platform() === 'win32', // by default spawn is used everywhere. If you have problems try using safe: true
    scripts: [
      //...
    ]
    //  ...
}

Developing

If opening a pull request, create an issue describing a fix or feature. Have your pull request point to the issue by writing your commits with the issue number in the message.

Make sure you lint your code by running npm run lint and you can build the library by running npm run build.

I appreciate any feed back as well, Thanks for helping!