Skip to content

Commit bb446bd

Browse files
authored
chore: migrate rollup-plugin-json (#30)
* chore: migrate rollup-plugin-json * docs: remove bad line * chore: update metadata * chore: fix types test
1 parent 9579120 commit bb446bd

37 files changed

+1184
-151
lines changed

.github/deprecation-messages.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
"This module has moved and is now available at @rollup/plugin-. Please update your dependencies. This version is no longer maintained."
1+
"This module has been deprecated and is no longer maintained. Please use @rollup/plugin-."
22

33
# Moved
44

5-
This module has moved and is now available at []@rollup/plugin-](https://github.com/rollup/plugins). Please update your dependencies. This repository is no longer maintained.
5+
This module has moved and is now available at [@rollup/plugin-](https://github.com/rollup/plugins). Please update your dependencies. This repository is no longer maintained.
66

77
This module has moved and is now available at @rollup/plugin- / https://github.com/rollup/plugins

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"ava": "^2.2.0",
1818
"chalk": "^2.4.2",
1919
"codecov-lite": "^0.3.1",
20+
"del-cli": "^3.0.0",
2021
"eslint-config-rollup": "^0.1.0",
2122
"execa": "^2.0.4",
2223
"globby": "^10.0.1",

packages/json/CHANGELOG.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# rollup-plugin-json changelog
2+
3+
## 4.0.0
4+
*2019-03-18*
5+
* Pass all JSON data through dataToEsm to consistently support "compact" formatting, support empty keys, abandon Node 4 support, add prettier, update dependencies ([#53](https://github.com/rollup/rollup-plugin-json/issues/53))
6+
7+
## 3.1.0
8+
*2018-09-13*
9+
* Expose "compact" and "namedExports" options ([#45](https://github.com/rollup/rollup-plugin-json/issues/45))
10+
* Update rollup-pluginutils to support null values in JSON ([#44](https://github.com/rollup/rollup-plugin-json/issues/44))
11+
* Update dependencies and ensure [email protected] compatibility ([#46](https://github.com/rollup/rollup-plugin-json/issues/46))
12+
13+
## 3.0.0
14+
*2018-05-11*
15+
* No longer create a fake AST to support tree-shaking with upcoming versions of rollup ([#41](https://github.com/rollup/rollup-plugin-json/issues/41))
16+
17+
## 2.3.1
18+
*2018-05-11*
19+
* Update example in readme ([#38](https://github.com/rollup/rollup-plugin-json/issues/38))
20+
* Warn when using this version with upcoming rollup versions
21+
22+
## 2.3.0
23+
*2017-06-03*
24+
* Always parse JSON, so malformed JSON is identified at bundle time ([#27](https://github.com/rollup/rollup-plugin-json/issues/27))
25+
26+
## 2.2.0
27+
*2017-06-03*
28+
* Add `indent` option ([#24](https://github.com/rollup/rollup-plugin-json/issues/24))
29+
30+
## 2.1.1
31+
*2017-04-09*
32+
* Add license to package.json ([#25](https://github.com/rollup/rollup-plugin-json/pull/25))
33+
34+
## 2.1.0
35+
*2016-12-15*
36+
* Add support for `preferConst` option ([#16](https://github.com/rollup/rollup-plugin-json/pull/16))
37+
* Handle JSON files with no valid identifier keys ([#19](https://github.com/rollup/rollup-plugin-json/issues/19))
38+
39+
## 2.0.2
40+
*2016-09-07*
41+
* Generate correct fake AST
42+
43+
## 2.0.1
44+
*2016-06-23*
45+
* Return a `name`
46+
47+
## 2.0.0
48+
*2015-11-05*
49+
* Generate fake AST to avoid unnecessary traversals within Rollup
50+
51+
## 1.1.0
52+
*unpublished*
53+
* Generate named exports alongside default exports
54+
55+
## 1.0.0
56+
*2015-10-25*
57+
* First release

packages/json/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[npm]: https://img.shields.io/npm/v/@rollup/plugin-json
2+
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-json
3+
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-json
4+
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-json
5+
6+
[![npm][npm]][npm-url]
7+
[![size][size]][size-url]
8+
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
9+
10+
# @rollup/plugin-json
11+
12+
🍣 A Rollup which Converts .json files to ES6 modules.
13+
14+
## Requirements
15+
16+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
17+
18+
## Install
19+
20+
Using npm:
21+
22+
```console
23+
npm install @rollup/plugin-json --save-dev
24+
```
25+
26+
## Usage
27+
28+
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
29+
30+
```js
31+
import json from '@rollup/plugin-json';
32+
33+
export default {
34+
input: 'src/index.js',
35+
output: {
36+
dir: 'output',
37+
format: 'cjs'
38+
},
39+
plugins: [json()]
40+
};
41+
```
42+
43+
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
44+
45+
With an accompanying file `src/index.js`, the local `package.json` file would now be importable as seen below:
46+
47+
```js
48+
// src/index.js
49+
import pkg from './package.json';
50+
console.log(`running version ${pkg.version}`);
51+
```
52+
53+
## Options
54+
55+
### `compact`
56+
57+
Type: `Boolean`<br>
58+
Default: `false`
59+
60+
If `true`, instructs the plugin to ignore `indent` and generates the smallest code.
61+
62+
### `exclude`
63+
64+
Type: `String` | `Array[...String]`<br>
65+
Default: `null`
66+
67+
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.
68+
69+
### `include`
70+
71+
Type: `String` | `Array(String)`<br>
72+
Default: `null`
73+
74+
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
75+
76+
### `indent`
77+
78+
Type: `String`<br>
79+
Default: `'\t'`
80+
81+
Specifies the indentation for the generated default export.
82+
83+
### `namedExports`
84+
85+
Type: `Boolean`<br>
86+
Default: `true`
87+
88+
If `true`, instructs the plugin to generate a named export for every property of the JSON object.
89+
90+
### `preferConst`
91+
92+
Type: `Boolean`<br>
93+
Default: `false`
94+
95+
If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking.
96+
97+
## Meta
98+
99+
[CONTRIBUTING](/.github/CONTRIBUTING.md)
100+
101+
[LICENSE (MIT)](/LICENSE)

packages/json/index.d.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Plugin } from 'rollup';
2+
3+
interface RollupJsonOptions {
4+
/**
5+
* All JSON files will be parsed by default,
6+
* but you can also specifically include files
7+
*/
8+
include?: string | RegExp | ReadonlyArray<string | RegExp> | null;
9+
/**
10+
* All JSON files will be parsed by default,
11+
* but you can also specifically exclude files
12+
*/
13+
exclude?: string | RegExp | ReadonlyArray<string | RegExp> | null;
14+
/**
15+
* For tree-shaking, properties will be declared as variables, using
16+
* either `var` or `const`.
17+
* @default false
18+
*/
19+
preferConst?: boolean;
20+
/**
21+
* Specify indentation for the generated default export
22+
* @default '\t'
23+
*/
24+
indent: string;
25+
/**
26+
* Ignores indent and generates the smallest code
27+
* @default false
28+
*/
29+
compact: boolean;
30+
/**
31+
* Generate a named export for every property of the JSON object
32+
* @default true
33+
*/
34+
namedExports: true;
35+
}
36+
37+
/**
38+
* Convert .json files to ES6 modules
39+
*/
40+
export default function json(options?: RollupJsonOptions): Plugin;

packages/json/package.json

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "@rollup/plugin-json",
3+
"version": "4.0.0",
4+
"publishConfig": {
5+
"access": "public"
6+
},
7+
"description": "Convert .json files to ES6 modules",
8+
"license": "MIT",
9+
"repository": "rollup/plugins",
10+
"main": "dist/index.js",
11+
"scripts": {
12+
"build": "rollup -c",
13+
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
14+
"ci:lint": "pnpm run build && pnpm run lint && pnpm run security",
15+
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
16+
"ci:test": "pnpm run test -- --verbose && pnpm run test:ts",
17+
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
18+
"lint:docs": "prettier --single-quote --write README.md",
19+
"lint:js": "eslint --fix --cache src test",
20+
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
21+
"prebuild": "del-cli dist",
22+
"prepare": "npm run build",
23+
"prepublishOnly": "npm run lint && npm run test",
24+
"pretest": "npm run build",
25+
"security": "echo 'pnpm needs `npm audit` support'",
26+
"test": "ava",
27+
"test:ts": "tsc index.d.ts test/types.ts --noEmit"
28+
},
29+
"files": [
30+
"dist",
31+
"index.d.ts",
32+
"README.md",
33+
"LICENSE"
34+
],
35+
"keywords": [
36+
"rollup",
37+
"plugin",
38+
"json",
39+
"es2015",
40+
"npm",
41+
"modules"
42+
],
43+
"peerDependencies": {
44+
"rollup": "^1.20.0"
45+
},
46+
"dependencies": {
47+
"rollup-pluginutils": "^2.5.0"
48+
},
49+
"devDependencies": {
50+
"rollup-plugin-buble": "^0.19.6",
51+
"rollup-plugin-node-resolve": "^5.2.0",
52+
"source-map-support": "^0.5.11"
53+
},
54+
"ava": {
55+
"files": [
56+
"!**/fixtures/**",
57+
"!**/helpers/**",
58+
"!**/recipes/**",
59+
"!**/types.ts"
60+
]
61+
},
62+
"jsnext:main": "dist/rollup-plugin-json.es.js",
63+
"module": "dist/index.es.js"
64+
}

packages/json/rollup.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import buble from 'rollup-plugin-buble';
2+
3+
const pkg = require('./package.json');
4+
5+
const external = Object.keys(pkg.dependencies);
6+
7+
export default {
8+
input: 'src/index.js',
9+
output: [
10+
{ file: pkg.main, format: 'cjs', sourcemap: true },
11+
{ file: pkg.module, format: 'es', sourcemap: true }
12+
],
13+
plugins: [buble()],
14+
external
15+
};

packages/json/src/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createFilter, dataToEsm } from 'rollup-pluginutils';
2+
3+
export default function json(options = {}) {
4+
const filter = createFilter(options.include, options.exclude);
5+
const indent = 'indent' in options ? options.indent : '\t';
6+
7+
return {
8+
name: 'json',
9+
10+
// eslint-disable-next-line no-shadow
11+
transform(json, id) {
12+
if (id.slice(-5) !== '.json' || !filter(id)) return null;
13+
14+
return {
15+
code: dataToEsm(JSON.parse(json), {
16+
preferConst: options.preferConst,
17+
compact: options.compact,
18+
namedExports: options.namedExports,
19+
indent
20+
}),
21+
map: { mappings: '' }
22+
};
23+
}
24+
};
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
form/**

packages/json/test/fixtures/.eslintrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"globals": {
3+
"t": "readonly"
4+
},
5+
"rules": {
6+
"import/extensions": "off",
7+
"import/prefer-default-export": "off",
8+
"no-console": "off"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
1, 2, 3
3+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import config from './config.json';
2+
3+
t.deepEqual(config, [1, 2, 3]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"answer": 42
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import config from './config.json';
2+
3+
t.is(config.answer, 42);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"answer": 42
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Are extensionless imports and /index resolutions a good idea?": "No."
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import config from './config';
2+
import questions from './dir';
3+
4+
t.is(config.answer, 42);
5+
t.is(questions['Are extensionless imports and /index resolutions a good idea?'], 'No.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable */
2+
export var validKey=true;export var nested={subKey:"ok"};export var array=[1,"2"];export default{validKey:validKey,"invalid-key":1,nested:nested,array:array,"function":"not used","null":null};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable */
2+
export var validKey = true;
3+
export var nested = {
4+
subKey: "ok"
5+
};
6+
export var array = [
7+
1,
8+
"2"
9+
];
10+
export default {
11+
validKey: validKey,
12+
"invalid-key": 1,
13+
nested: nested,
14+
array: array,
15+
"function": "not used",
16+
"null": null
17+
};

0 commit comments

Comments
 (0)