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

Feat/2.0.0 #15

Merged
merged 12 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
run: |
cp package.json dist/package.json
cp README.md dist/README.md
cp LICENSE dist/LICENSE

- name: Config
env:
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Pull request checks
on:
pull_request:
types:
- opened
- reopened
branches:
- main

jobs:
pull_request:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install
uses: pnpm/action-setup@v4
with:
version: latest
run_install: true

- name: TypeScript Compiler
run: tsc --noEmit

- name: Lint
run: pnpx eslint .
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eslint .
tsc --noEmit
181 changes: 111 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,117 +1,158 @@
# esbuild plugin for package.json
<div align="center">

![NPM Downloads](https://img.shields.io/npm/dw/esbuild-plugin-package-json) ![NPM License](https://img.shields.io/npm/l/esbuild-plugin-package-json)
<img width="196" src="https://raw.githubusercontent.com/simonkovtyk/esbuild-plugin-package-json/d7d77d5766ef9ef97f157c2d221d61a7d3cef51c/docs/esbuild-favicon.svg" />

Prepares the package.json by removing all unnecessary fields and copying it to the out-folder of esbuild, so the built package can be published directly.
<h1>package.json Plugin</h1>

* Supports newest esbuild version
* Uses NPM's package.json discovery
* Uses esbuild config to determine the out folder
* Type declarations (d.ts) included
<p>Prepares the package.json by removing all unnecessary fields and<br/>copying it to the output folder of esbuild.</p>

## Unnecessary fields
![NPM Downloads](https://img.shields.io/npm/dw/esbuild-plugin-package-json)
![NPM License](https://img.shields.io/npm/l/esbuild-plugin-package-json)
![GitHub package.json version](https://img.shields.io/npm/v/esbuild-plugin-package-json)
![TypeScript types](https://img.shields.io/badge/TypeScript_types-included-blue)

While a package should be published, there are a few fields, that may are considered as a security vulnerability or are just not needed in the published package.
<br />

Following fields will be removed:
- devDependencies
- scripts
Add a ⭐ to this repository — *it motivates me a lot!*

## How It Works
</div>

1. Parses the package.json from the project root.
2. Deletes all unnecessary fields.
3. Determines the out-folder by using the existing esbuild configuration.
4. Writes the new package.json to this dir.
## ⚡️ Getting started

This plugin prefers an ``outdir`` over an ``outfile``, but if only an ``outfile`` is provided, the plugin will choose the directory of the ``outfile`` as output directory for the package.json instead.\
The ``outbase`` is used as a prefix for the ``outdir`` or ``outfile`` and it can be left as empty, if it is not needed.
Simply install this package with your package manager.

## Options
````shell
npm install esbuild-plugin-package-json
````

### Parsing the package.json file
<details>
<summary>📦 other package manager</summary>

The default behavior is, that this package will use NPM's package.json resolution.\
Otherwise it may be helpful, to overwrite the path to the package.json manually:
Here are examples for installing the package with other package manager.

```typescript
packageJsonPlugin(
[...]
pathToPackageJson?: string | undefined
);
```
> 💾 **yarn**
> ````shell
> yarn install esbuild-plugin-package-json
> ````

The path to the custom package.json can be customized by using the ``pathToPackageJson`` key.
> 💾 **pnpm**
> ````shell
> pnpm install esbuild-plugin-package-json
> ````

### Output directory
</details>

This plugin will use the esbuild configuration to determine the output directory for the package.json.\
Sometimes it can be helpful to overwrite the output directory.
Looks good so far 🔥 — now you have installed the latest version!

````typescript
packageJsonPlugin(
[...]
overrideOutBase?: string | undefined,
overrideOutDir?: string | undefined,
overrideOutFile?: string | undefined
);
## 💡 Introduction

While a package is a relase candidat, there are a few fields, that may are considered as a security vulnerability or are just not needed in the published package.

The following fields can be safely removed:
````json5
{
"scripts": {
// something here...
},
"devDependencies": {
// something here...
}
}
````

Each overwrite will overwrite the specific esbuild configuration.
## 🔧 Usage

[See here for more details about the out configuration of esbuild.](https://esbuild.github.io/api/#outbase)
```typescript
packageJsonPlugin(options);
```

### Lifecycle
This function needs to be called inside the esbuild configuration in order to use this plugin. It will provide the plugin inside the build process of esbuild.

You can configure at which lifecycle of esbuild the plugin will be called.
<details>
<summary>Show an example of the integration</summary>

````typescript
packageJsonPlugin(
[...]
lifecycle: "onStart" | "onEnd" | "onDispose" | undefined
);
esbuild.build({
// some configuration...
plugins: [
packageJsonPlugin();
// more plugins here...
]
})
````

[See here for more about the esbuild lifecycles.](https://esbuild.github.io/plugins/#concepts)
</details>

## Usage
<details>
<summary>Show an example of the configuration</summary>

### Installation
````typescript
packageJsonPlugin({
// configure here
});
````
</details>

The plugin can be installed by any package manager.
### Properties

<details><summary><b>Show instructions</b></summary>
#### ``lifecycle``

> npm \
> ``npm install esbuild-plugin-package-json``
> Default: ``onEnd``

> yarn \
> ``yarn install esbuild-plugin-package-json``
An string with either the value ``onStart`` or ``onEnd``.

> pnpm \
> ``pnpm install esbuild-plugin-package-json``
<details>
<summary>Show an example</summary>

````typescript
packageJsonPlugin({
lifecycle: "onStart"
});
````
</details>

### Integration
[See here](https://esbuild.github.io/plugins/#concepts) for more about esbuild lifecycles.

#### ``overrideOut``

> Default: ``undefined`` (esbuild's output directory)

The easy way to integrate this plugin in esbuild.
A ``string``, that specifies the output directory for the package.json.

<details><summary><b>Show instructions</b></summary>
<details>
<summary>Show an example</summary>

````typescript
await esbuild.build({
[...]
plugins: [
packageJsonPlugin(...)
]
})
packageJsonPlugin({
overrideOut: "dist" // any directory allowed
});
````

[See here for more about the esbuild plugin integration.](https://esbuild.github.io/plugins/#using-plugins)
</details>

#### ``overridePackageJson``

> Default: ``undefined`` (npm default)

You can override the start directory for the package.json search, if a ``string`` is provided here.

<details>
<summary>Show an example</summary>

````typescript
packageJsonPlugin({
overridePackageJson: "libs/my-lib" // any directory allowed
});
````

</details>

### Returns

Type: ``Plugin``

An instance of this plugin, that will be used by esbuild automatically.

## License

The MIT License (MIT) - Please have a look at the LICENSE file for more details.
Expand All @@ -121,7 +162,7 @@ The MIT License (MIT) - Please have a look at the LICENSE file for more details.
Feel free to contribute to this project.\
You can fork this project and create a new pull request for contributing.

[Get to the repository at GitHub.](https://github.com/simonkovtyk/esbuild-plugin-package-json)
[See here](https://github.com/simonkovtyk/esbuild-plugin-package-json) for the repository at GitHub.

<hr>

Expand Down
7 changes: 7 additions & 0 deletions docs/esbuild-favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(
{
ignores: [
".github",
".idea",
"dist",
"docs",
"node_modules"
]
},
eslint.configs.recommended,
...tseslint.configs.strict,
...tseslint.configs.stylistic,
);
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"package config plugin"
],
"license": "MIT",
"version": "1.0.5",
"version": "2.0.0",
"bugs": "https://github.com/simonkovtyk/esbuild-plugin-package-json/issues",
"homepage": "https://github.com/simonkovtyk/esbuild-plugin-package-json",
"repository": {
Expand Down Expand Up @@ -55,8 +55,17 @@
"esbuild": ">=0.20.0"
},
"devDependencies": {
"typescript": "^5.5.4",
"@eslint/js": "^9.10.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.5.4",
"@types/npmcli__package-json": "^4.0.4",
"@types/node": "^22.5.4"
"eslint": "^9.10.0",
"typescript": "^5.5.4",
"typescript-eslint": "^8.6.0",
"@stylistic/eslint-plugin-ts": "^2.8.0",
"husky": "^9.1.6"
},
"scripts": {
"prepare": "husky"
}
}
2 changes: 1 addition & 1 deletion src/core/constants/file.constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const PACKAGE_JSON_FILENAME: string = "package.json";
const PACKAGE_JSON_FILENAME = "package.json";

export {
PACKAGE_JSON_FILENAME
Expand Down
7 changes: 7 additions & 0 deletions src/core/enums/message.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Messages {
PACKAGE_JSON_WRITE = "package.json could not be written."
}

export {
Messages
}
37 changes: 14 additions & 23 deletions src/core/helpers/out.helper.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
import path from "node:path";
import process from "node:process";
import { ResolvePathOptions } from "../types/options.type";
import { EsbuildOptionPaths, PathOverrides } from "../types/options.type";

// Prefer out dir before out file
const resolveOutDir = (options: ResolvePathOptions): string => {
const explicitOutBase: string | undefined = options.overrideOutBase ?? options.outBase;

const outBase = explicitOutBase
? path.join(process.cwd(), explicitOutBase)
: process.cwd();

const explicitOutDir: string | undefined = options.overrideOutDir ?? options.outDir;

if (explicitOutDir !== undefined) {
return path.join(outBase, explicitOutDir);
}

const explicitOutFile: string | undefined = options.overrideOutFile ?? options.outFile;

if (explicitOutFile !== undefined) {
const dirOfOutFile = path.parse(explicitOutFile).dir;

return path.join(outBase, dirOfOutFile);
}

return path.join(outBase, "dist")
const resolveOutDir = (options: EsbuildOptionPaths & PathOverrides): string => {
if (options.overrideOut !== undefined)
return path.join(process.cwd(), options.overrideOut);

const outBase: string = options.outBase ?? ".";

return path.join(
process.cwd(),
outBase,
options.outDir ?? options.outFile === undefined
? "dist"
: path.parse(options.outFile).dir
);
}

export {
Expand Down
Loading