Skip to content

Commit

Permalink
publish to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
GitaiQAQ committed Jun 18, 2019
1 parent 835befd commit f5aaf35
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
logs
*.log
npm-debug.log*
package-lock.json
yarn.lock
yarn-debug.log*
yarn-error.log*
Expand Down
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,92 @@
rollup-plugin-output
====

![](https://img.shields.io/npm/v/rollup-plugin-output.svg?style=flat)

Custom output format for rollup.

## Installation

```shell
$ npm install --save-dev rollup-plugin-output
```

Or

```shell
$ yarn add -D rollup-plugin-output
```

## Example

### Generate JSON format file

```ts
// src/manifest.ts
import * as pkg from "../package.json";

export default {
name: pkg.name,
description: pkg.description,
version: pkg.version,
app: {
background: {
scripts: ["background.js"]
}
}
};
```

```js
// rollup.config.js
import json from "rollup-plugin-json";
import output from "rollup-plugin-output";

const manifest = {
input: "src/manifest.ts",
output: {
file: "dist/manifest.json",
format: "json"
},
plugins: [json(), output()]
};

export default [manifest];
```

```json
// dist/manifest.json
{
"name": "shadowsocks-chromeos",
"description": "",
"version": "1.0.0",
"app": {
"background": {
"scripts": [
"background.js"
]
}
}
}
```

### Advanced Config(Custom output function)

```js
// rollup.config.js
const manifest = {
input: "src/manifest.ts",
output: {
file: "dist/manifest.txt",
format (code) {
return "Hello World";
}
},
plugins: [json(), output()]
};
```

```js
// dist/manifest.txt
Hello World
```
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-output",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/rollup-plugin-output.cjs.js",
"module": "dist/rollup-plugin-output.es.js",
"author": "Gitai <[email protected]>",
Expand All @@ -18,7 +18,7 @@
],
"scripts": {
"build": "rollup -c",
"prebuild": "rm -rf dist/*",
"prebuild": "shx rm -rf dist/*",
"prepare": "npm run build"
},
"dependencies": {
Expand All @@ -34,10 +34,10 @@
"typescript": "^3.4.3"
},
"files": [
"src",
"dist",
"index.d.ts",
"README.md"
"LICENSE",
"README.md",
"package.json"
],
"repository": "gitaiQAQ/rollup-plugin-output"
}
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import { name } from "../package.json";
import { createFilter } from "rollup-pluginutils";

const BUILDIN_FORMAT = {
raw(bundle) {
return new Function("define", "return " + bundle.code)(func => {
return func.toString();
});
},
json(bundle) {
return new Function("define", "return " + bundle.code)(func =>
json(code) {
return new Function("define", "return " + code)(func =>
JSON.stringify(func(), null, 4)
);
}
Expand All @@ -29,18 +24,22 @@ export default function(options: Options = {}) {
outputOptions.transform instanceof Function
) {
Object.keys(bundles).forEach(
key => filter(key) && outputOptions.transform(bundles[key])
key =>
filter(key) &&
(bundles[key].code = outputOptions.transform(
bundles[key].code
))
);
}
},
outputOptions(outputOptions) {
if (Object.keys(BUILDIN_FORMAT).includes(outputOptions.format)) {
outputOptions.format = "amd";
outputOptions.transform = BUILDIN_FORMAT[outputOptions.format];
outputOptions.format = "amd";
return outputOptions;
} else if (outputOptions.format instanceof Function) {
outputOptions.format = "amd";
outputOptions.transform = outputOptions.format;
outputOptions.format = "amd";
}
return outputOptions;
}
Expand Down

0 comments on commit f5aaf35

Please sign in to comment.