Skip to content

Commit a3001dc

Browse files
committed
Create a plugin to execute scripts serially on webpack compiler hooks
Uses @pika/pack to build the package. Uses xo as linter
1 parent 30041c8 commit a3001dc

File tree

8 files changed

+4380
-0
lines changed

8 files changed

+4380
-0
lines changed

.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"node": "8.9.0"
8+
}
9+
}
10+
]
11+
]
12+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
pkg
3+
*.log

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# webpack-hooks-shellscripts
2+
3+
[![Build Status](https://travis-ci.org/nutshell-lab/webpack-hooks-shellscripts.svg?branch=master)](https://travis-ci.org/nutshell-lab/webpack-hooks-shellscripts)
4+
5+
> Just triggers some shellscripts on webpack compiler hooks.

package.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "webpack-hooks-shellscripts",
3+
"version": "1.0.0",
4+
"description": "Just triggers some shellscripts on webpack compiler hooks.",
5+
"main": "src/index.js",
6+
"repository": "[email protected]:nutshell-lab/webpack-hooks-shellscripts.git",
7+
"author": "Victor Rebiard--Crépin <[email protected]>",
8+
"license": "MIT",
9+
"private": false,
10+
"scripts": {
11+
"test": "xo",
12+
"build": "pack build",
13+
"release": "pack publish"
14+
},
15+
"dependencies": {
16+
"execa": "^1.0.0",
17+
"p-map-series": "^2.1.0"
18+
},
19+
"devDependencies": {
20+
"@babel/register": "^7.4.4",
21+
"@pika/pack": "^0.3.7",
22+
"@pika/plugin-build-node": "^0.3.16",
23+
"@pika/plugin-build-types": "^0.3.16",
24+
"@pika/plugin-copy-assets": "^0.3.16",
25+
"@pika/plugin-standard-pkg": "^0.3.16",
26+
"eslint-config-prettier": "^4.2.0",
27+
"eslint-config-prettier-standard": "^2.0.0",
28+
"eslint-config-standard": "^12.0.0",
29+
"eslint-plugin-prettier": "^3.0.1",
30+
"eslint-plugin-standard": "^4.0.0",
31+
"xo": "^0.24.0"
32+
},
33+
"xo": {
34+
"env": "node",
35+
"space": true,
36+
"semicolon": false,
37+
"extends": "prettier-standard"
38+
},
39+
"@pika/pack": {
40+
"pipeline": [
41+
[
42+
"@pika/plugin-standard-pkg"
43+
],
44+
[
45+
"@pika/plugin-build-node"
46+
],
47+
[
48+
"@pika/plugin-build-types"
49+
],
50+
[
51+
"@pika/plugin-copy-assets",
52+
{
53+
"files": [
54+
"README.md"
55+
]
56+
}
57+
]
58+
]
59+
}
60+
}

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function plugin(scripts: object): void

src/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import execa from 'execa'
2+
import pMapSeries from 'p-map-series'
3+
4+
const PLUGIN_NAME = 'HooksShellScriptsPlugin'
5+
6+
const execute = script =>
7+
execa.shell(script).then(({ stdout, stderr }) => {
8+
if (stdout) process.stdout.write(stdout)
9+
if (stderr) process.stderr.write(stderr)
10+
})
11+
12+
const registerHook = compiler => ([hook, scripts]) =>
13+
compiler.hooks[hook].tap(PLUGIN_NAME, () => pMapSeries(scripts, execute))
14+
15+
/**
16+
* Bind some shell scripts to execute serially on webpack compiler hooks
17+
* @param {object} scripts Hooks scipts bindings
18+
* @returns {void}
19+
* @see https://webpack.js.org/api/compiler-hooks/
20+
* @example
21+
* ```javascript
22+
* // webpack.config.js
23+
* module.exports = {
24+
* plugins: [
25+
* hooksScriptPlugin({
26+
* beforeEmit: ['rm -r ./dist'],
27+
* afterEmit: ['echo "Build succeeded"']
28+
* })
29+
* ]
30+
* }
31+
* ```
32+
*/
33+
export default (scripts = {}) => ({
34+
apply: compiler => Object.entries(scripts).map(registerHook(compiler))
35+
})

0 commit comments

Comments
 (0)