Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate rollup-plugin-run #41

Merged
merged 16 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/run/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved
node_modules
17 changes: 17 additions & 0 deletions packages/run/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# rollup-plugin-run changelog
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved

## 1.1.0

- Allow arguments and options to be passed to `child_process.fork`

## 1.0.2

- Warn if Rollup version is too low

## 1.0.1

- Handle output files with different names from input files

## 1.0.0

- First release
80 changes: 80 additions & 0 deletions packages/run/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# @rollup/plugin-run
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved

🍣 A Rollup plugin which runs your bundles in Node once they're built.

Using this plugin gives much faster results compared to what you would do with [nodemon](https://nodemon.io/).

## Install

Using npm:

```console
npm i -D @rollup/plugin-run
```

## Usage

Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
shellscape marked this conversation as resolved.
Show resolved Hide resolved

```js
// rollup.config.js
import run from "@rollup/plugin-run";

export default {
input: "src/index.js",
output: {
file: "dist/index.js",
format: "cjs"
},
plugins: [run()]
};
```

The app is run in a child process using [child_process.fork(...)](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options). Each time your bundle is rebuilt when the source code changes, this plugin starts the generated file as a child process (after first closing the previous process, if it's not the first run).

Since this feature is intended for development use, you may prefer to only include it when Rollup is being run in watch mode:

```diff
// rollup.config.js
import run from '@rollup/plugin-run';

+const dev = process.env.ROLLUP_WATCH === 'true';

export default {
input: 'src/index.js',
output: {
file: 'dist/index.js',
format: 'cjs'
},
plugins: [
- run()
+ dev && run()
]
};
```

> NOTE: It works with Rollup's code-splitting if you're using dynamic `import(...)` — the only constraint is that you have a single entry point specified in the config.

## Options

You can pass through options such as `env` and `execArgv`. For example, to debug with sourcemaps using [source-map-support](https://www.npmjs.com/package/source-map-support) you could do this:

```diff
// rollup.config.js
import run from '@rollup/plugin-run';

export default {
input: 'src/index.js',
output: {
file: 'dist/index.js',
format: 'cjs',
+ sourcemap: true
},
plugins: [
- run()
+ run({
+ execArgv: ['-r', 'source-map-support/register']
+ })
]
};
```
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved
69 changes: 69 additions & 0 deletions packages/run/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const path = require('path');
const childProcess = require('child_process');

module.exports = (opts = {}) => {
let input;
let proc;

const args = opts.args || [];
const forkOptions = opts.options || opts;
delete forkOptions.args;

return {
name: 'run',

// eslint-disable-next-line no-shadow
options(opts) {
let inputs = opts.input;

if (typeof inputs === 'string') {
inputs = [inputs];
}

if (typeof inputs === 'object') {
inputs = Object.values(inputs);
}

if (inputs.length > 1) {
throw new Error(`rollup-plugin-run only works with a single entry point`);
}

input = path.resolve(inputs[0]);
},

generateBundle(outputOptions, bundle, isWrite) {
if (!isWrite) {
this.error(`rollup-plugin-run currently only works with bundles that are written to disk`);
}

const dir = outputOptions.dir || path.dirname(outputOptions.file);

let dest;

for (const fileName in bundle) {
if (Object.prototype.hasOwnProperty.call(bundle, fileName)) {
const chunk = bundle[fileName];

if (!('isEntry' in chunk)) {
this.error(`rollup-plugin-run requires Rollup 0.65 or higher`);
}

// eslint-disable-next-line no-continue
if (!chunk.isEntry) continue;

if (chunk.modules[input]) {
dest = path.join(dir, fileName);
break;
}
}
}

if (dest) {
if (proc) proc.kill();
proc = childProcess.fork(dest, args, forkOptions);
} else {
this.error(`rollup-plugin-run could not find output chunk`);
}
}
};
};
5 changes: 5 additions & 0 deletions packages/run/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions packages/run/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@rollup/plugin-run",
"version": "1.1.0",
"publishConfig": {
"access": "public"
},
"description": "Run your bundle after you've built it",
"license": "MIT",
"repository": "rollup/plugins",
"author": "Rich Harris",
"homepage": "https://github.com/rollup/plugins/packages/run/#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "index.js",
"scripts": {
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved
"lint:docs": "prettier --single-quote --write README.md",
"lint:js": "eslint --fix --cache *.js",
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
"test": "echo \"Error: no test specified\" && exit 1"
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [
"rollup"
shrirambalaji marked this conversation as resolved.
Show resolved Hide resolved
]
}