Skip to content

Commit

Permalink
Merge pull request #9 from alanblins/custom-merge
Browse files Browse the repository at this point in the history
feat: add custom merge option
  • Loading branch information
karolis-sh committed Dec 8, 2023
2 parents 8f72feb + ac00104 commit f868b35
Show file tree
Hide file tree
Showing 8 changed files with 1,201 additions and 2,023 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,33 @@ Type: `string`

JSON output destination.

### merge

Type: `(items: JSONValue[]) => JSONValue`

By default the merge function uses `Object.assign`.

```js
const esbuild = require('esbuild');
const jsonMerge = require('esbuild-plugin-json-merge');
const { defaultComposer } = require('default-composer');

const { version, name, description } = require('./package.json');

esbuild
.build({
entryPoints: ['src/index.js'],
outdir: 'build',
plugins: [
jsonMerge({
entryPoints: ['src/manifest.json', { version, name, description }],
outfile: 'manifest.json',
merge: (items) => defaultComposer(...items), //Custom merge
}),
],
})
.catch(() => process.exit(1));
```

[package-version-badge]: https://badge.fury.io/js/esbuild-plugin-json-merge.svg
[package-version]: https://www.npmjs.com/package/esbuild-plugin-json-merge
14 changes: 14 additions & 0 deletions __tests__/__fixtures__/listobjects/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Aral 😊",
"surname": "",
"isDeveloper": true,
"isDesigner": false,
"age": 33,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
},
"emails": ["contact@aralroca.com"],
"hobbies": ["programming"]
}
10 changes: 10 additions & 0 deletions __tests__/__fixtures__/listobjects/b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Aral",
"emails": [],
"phone": "555555555",
"age": null,
"address": {
"zip": "54321"
},
"hobbies": ["parkour", "computer science", "books", "nature"]
}
25 changes: 25 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const os = require('os');
const path = require('path');

const { defaultComposer } = require('default-composer');
const { build } = require('esbuild');
const fse = require('fs-extra');

Expand Down Expand Up @@ -52,3 +53,27 @@ it('should merge list files', async () => {
});
expect(json).toEqual([null, 1, false, 4, 5]);
});

it('should merge list files with custom merge', async () => {
const json = await merge({
entryPoints: [path.join(FIXTURES_DIR, 'listobjects/*.json')],
outfile: 'out.json',
merge: (items) => defaultComposer(...items),
});
expect(json).toEqual({
name: 'Aral',
surname: '',
isDeveloper: true,
isDesigner: false,
emails: ['contact@aralroca.com'],
phone: '555555555',
age: 33,
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA',
zip: '54321',
},
hobbies: ['parkour', 'computer science', 'books', 'nature'],
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/jest": "^29.2.6",
"@types/node": "^18.11.18",
"cz-conventional-changelog": "^3.3.0",
"default-composer": "^0.6.0",
"esbuild": "^0.17.4",
"eslint": "^8.32.0",
"husky": "^8.0.3",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ const getContentChunks = (entryPoints: InputEntry[]): Promise<JSONValue[]> =>
),
).then((items) => items.flat().filter((item) => item != null));

const merge = (items: JSONValue[]): JSONValue =>
const defaultMerge = (items: JSONValue[]): JSONValue =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Object.assign(...items);

export = ({ entryPoints, outfile }: Options = {}): Plugin => ({
export = ({
entryPoints,
outfile,
merge = defaultMerge,
}: Options = {}): Plugin => ({
name: NAME,
async setup(build) {
if (!outfile) {
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export type InputEntry = string | JSONValue[] | { [key: string]: JSONValue };
export type Options = Partial<{
entryPoints: InputEntry[];
outfile: string;
merge?: (items: JSONValue[]) => JSONValue;
}>;
Loading

0 comments on commit f868b35

Please sign in to comment.