Skip to content

Commit

Permalink
fix(json): prepare for Rollup 3
Browse files Browse the repository at this point in the history
BREAKING CHANGES: Requires Node 14
  • Loading branch information
lukastaegert committed Sep 30, 2022
1 parent 32aa6d2 commit 176afc3
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 73 deletions.
6 changes: 4 additions & 2 deletions packages/json/README.md
Expand Up @@ -13,7 +13,7 @@

## Requirements

This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.

## Install

Expand Down Expand Up @@ -46,7 +46,9 @@ With an accompanying file `src/index.js`, the local `package.json` file would no

```js
// src/index.js
import pkg from './package.json';
import { readFileSync } from 'fs';

const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
console.log(`running version ${pkg.version}`);
```
Expand Down
32 changes: 21 additions & 11 deletions packages/json/package.json
Expand Up @@ -13,8 +13,15 @@
"author": "rollup",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/json#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"module": "dist/index.es.js",
"main": "./dist/cjs/index.js",
"module": "./dist/es/index.js",
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/es/index.js"
},
"engines": {
"node": ">=14.0.0"
},
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
Expand All @@ -30,7 +37,7 @@
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
},
"files": [
"dist",
"dist/**/*.{js,json}",
"types",
"README.md",
"LICENSE"
Expand All @@ -44,22 +51,25 @@
"modules"
],
"peerDependencies": {
"rollup": "^1.20.0 || ^2.0.0"
"rollup": "^1.20.0||^2.0.0||^3.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
},
"dependencies": {
"@rollup/pluginutils": "^3.0.8"
"@rollup/pluginutils": "^4.2.1"
},
"devDependencies": {
"@rollup/plugin-buble": "^0.21.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"rollup": "^2.67.3",
"source-map-support": "^0.5.19"
"@rollup/plugin-node-resolve": "^14.1.0",
"rollup": "^3.0.0-7",
"source-map-support": "^0.5.21"
},
"types": "types/index.d.ts",
"ava": {
"babel": {
"compileEnhancements": false
},
"workerThreads": false,
"files": [
"!**/fixtures/**",
"!**/helpers/**",
Expand Down
15 changes: 0 additions & 15 deletions packages/json/rollup.config.js

This file was deleted.

13 changes: 13 additions & 0 deletions packages/json/rollup.config.mjs
@@ -0,0 +1,13 @@
import { readFileSync } from 'fs';

import buble from '@rollup/plugin-buble';

import { createConfig } from '../../shared/rollup.config.mjs';

export default {
...createConfig({
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
}),
input: 'src/index.js',
plugins: [buble()]
};
66 changes: 33 additions & 33 deletions packages/json/test/snapshots/test.js.md
Expand Up @@ -2,41 +2,17 @@

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## generates correct code with compact=true

> Snapshot 1
'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};'

## generates correct code with namedExports=false

> Snapshot 1
`export default {␊
validKey: true,␊
"invalid-key": 1,␊
nested: {␊
subKey: "ok"␊
},␊
array: [␊
1,␊
"2"␊
],␊
"function": "not used",␊
"null": null␊
};`

## generates correct code with preferConst
## generates properly formatted code

> Snapshot 1
`export const validKey = true;␊
export const nested = {␊
`export var validKey = true;␊
export var nested = {␊
subKey: "ok"␊
};␊
export const array = [␊
export var array = [␊
1,␊
"2"␊
];␊
Expand All @@ -50,15 +26,15 @@ Generated by [AVA](https://ava.li).
};␊
`

## generates properly formatted code
## generates correct code with preferConst

> Snapshot 1
`export var validKey = true;␊
export var nested = {␊
`export const validKey = true;␊
export const nested = {␊
subKey: "ok"␊
};␊
export var array = [␊
export const array = [␊
1,␊
"2"␊
];␊
Expand Down Expand Up @@ -93,3 +69,27 @@ Generated by [AVA](https://ava.li).
"null": null␊
};␊
`

## generates correct code with compact=true

> Snapshot 1
'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};'

## generates correct code with namedExports=false

> Snapshot 1
`export default {␊
validKey: true,␊
"invalid-key": 1,␊
nested: {␊
subKey: "ok"␊
},␊
array: [␊
1,␊
"2"␊
],␊
"function": "not used",␊
"null": null␊
};`
Binary file modified packages/json/test/snapshots/test.js.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/json/test/test.js
Expand Up @@ -48,7 +48,7 @@ test('generates named exports', async (t) => {
plugins: [json()]
});

const { code, result } = await testBundle(t, bundle, { exports: {} });
const { code, result } = await testBundle(t, bundle, { inject: { exports: {} } });

t.is(result.version, '1.33.7');
t.is(code.indexOf('this-should-be-excluded'), -1, 'should exclude unused properties');
Expand Down
63 changes: 55 additions & 8 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions shared/rollup.config.mjs
Expand Up @@ -23,14 +23,14 @@ export function createConfig({ pkg, external = [] }) {
output: [
{
format: 'cjs',
file: pkg.main,
file: pkg.exports.require,
exports: 'named',
footer: 'module.exports = Object.assign(exports.default, exports);',
sourcemap: true
},
{
format: 'es',
file: pkg.module,
file: pkg.exports.import,
plugins: [emitModulePackageFile()],
sourcemap: true
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Expand Up @@ -12,7 +12,7 @@
"pretty": true,
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2019"
},
"exclude": ["dist", "node_modules", "test/types"]
}

0 comments on commit 176afc3

Please sign in to comment.