esbuild-plugin-clear 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # esbuild-plugin-clear
2
2
 
3
- esbuild plugin that deletes a folder before every build, so no stale files are left in the output directory.
3
+ esbuild plugin that deletes the output folder before every build, so no stale files are left in the output directory.
4
+ Ships as ESM and CommonJS with TypeScript types.
4
5
 
5
6
  Documentation: https://www.marco-starker.de/en/projects/esbuild-plugin-clear/docs
6
7
  (Deutsch: https://www.marco-starker.de/de/projekte/esbuild-plugin-clear/doku)
@@ -20,13 +21,23 @@ await esbuild.build({
20
21
  bundle: true,
21
22
  outfile: './dist/index.js',
22
23
  plugins: [
23
- clear('./dist'),
24
+ clear(),
24
25
  ],
25
26
  });
26
27
  ```
27
28
 
28
- > [!CAUTION]
29
- > The plugin deletes the given path without any check. `clear('.')` deletes your whole project.
29
+ Without a path the plugin clears `outdir` or the folder of `outfile`. It refuses to delete the working directory, one
30
+ of its parents or anything outside of it unless you set `allowOutsideWorkingDir`.
31
+
32
+ ## Local development
33
+
34
+ ```bash
35
+ npm ci
36
+ npm test
37
+ npm run build
38
+ ```
39
+
40
+ The package itself runs on Node.js 22.13 or later. Building and testing need Node.js 22.18 or later.
30
41
 
31
42
  ## License
32
43
 
package/dist/index.cjs ADDED
@@ -0,0 +1,66 @@
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ // \0rolldown/runtime.js
6
+ var __create = Object.create;
7
+ var __defProp = Object.defineProperty;
8
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
+ var __getOwnPropNames = Object.getOwnPropertyNames;
10
+ var __getProtoOf = Object.getPrototypeOf;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
14
+ key = keys[i];
15
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
+ get: ((k) => from[k]).bind(null, key),
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+ let node_fs_promises = require("node:fs/promises");
27
+ let node_path = require("node:path");
28
+ node_fs_promises = __toESM(node_fs_promises, 1);
29
+ node_path = __toESM(node_path, 1);
30
+ // src/clear.ts
31
+ function getOutputDir({ outdir, outfile }) {
32
+ if (outdir !== void 0) return outdir;
33
+ return outfile === void 0 ? void 0 : node_path.default.dirname(outfile);
34
+ }
35
+ function isInside(parent, child) {
36
+ const relative = node_path.default.relative(parent, child);
37
+ return relative !== "" && relative !== ".." && relative.startsWith(`..${node_path.default.sep}`) === false && node_path.default.isAbsolute(relative) === false;
38
+ }
39
+ function clear(options = {}) {
40
+ const { allowOutsideWorkingDir = false, path: target } = typeof options === "string" ? { path: options } : options;
41
+ return {
42
+ name: "clear",
43
+ setup: (build) => {
44
+ build.onStart(async () => {
45
+ const workingDir = build.initialOptions.absWorkingDir ?? process.cwd();
46
+ const relative = target ?? getOutputDir(build.initialOptions);
47
+ if (relative === void 0 || relative === "") return { errors: [{ text: "No path given and the build has neither outdir nor outfile." }] };
48
+ const resolved = node_path.default.resolve(workingDir, relative);
49
+ if (resolved === workingDir || isInside(resolved, workingDir)) return { errors: [{ text: `Refusing to delete "${resolved}", it is the working directory or one of its parents.` }] };
50
+ if (allowOutsideWorkingDir === false && isInside(workingDir, resolved) === false) return { errors: [{ text: `Refusing to delete "${resolved}", it is outside of the working directory "${workingDir}". Set allowOutsideWorkingDir to allow it.` }] };
51
+ try {
52
+ await node_fs_promises.default.rm(resolved, {
53
+ force: true,
54
+ recursive: true
55
+ });
56
+ } catch (error) {
57
+ return { errors: [{ text: `Could not delete "${resolved}": ${error instanceof Error ? error.message : String(error)}` }] };
58
+ }
59
+ return {};
60
+ });
61
+ }
62
+ };
63
+ }
64
+ // src/index.ts
65
+ var src_default = clear;
66
+ exports.default = src_default;
@@ -0,0 +1,9 @@
1
+ import { Plugin } from "esbuild";
2
+ interface ClearPluginOptionInterface {
3
+ /** Allows deleting a path outside the working directory of the build. */
4
+ allowOutsideWorkingDir?: boolean;
5
+ /** Path to delete, relative to the working directory of the build. Defaults to the output folder (outdir or the folder of outfile). */
6
+ path?: string;
7
+ }
8
+ export default function clear(options?: ClearPluginOptionInterface | string): Plugin;
9
+ export type { ClearPluginOptionInterface };
@@ -0,0 +1,9 @@
1
+ import { Plugin } from "esbuild";
2
+ interface ClearPluginOptionInterface {
3
+ /** Allows deleting a path outside the working directory of the build. */
4
+ allowOutsideWorkingDir?: boolean;
5
+ /** Path to delete, relative to the working directory of the build. Defaults to the output folder (outdir or the folder of outfile). */
6
+ path?: string;
7
+ }
8
+ export default function clear(options?: ClearPluginOptionInterface | string): Plugin;
9
+ export type { ClearPluginOptionInterface };
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ // src/clear.ts
4
+ function getOutputDir({ outdir, outfile }) {
5
+ if (outdir !== void 0) return outdir;
6
+ return outfile === void 0 ? void 0 : path.dirname(outfile);
7
+ }
8
+ function isInside(parent, child) {
9
+ const relative = path.relative(parent, child);
10
+ return relative !== "" && relative !== ".." && relative.startsWith(`..${path.sep}`) === false && path.isAbsolute(relative) === false;
11
+ }
12
+ function clear(options = {}) {
13
+ const { allowOutsideWorkingDir = false, path: target } = typeof options === "string" ? { path: options } : options;
14
+ return {
15
+ name: "clear",
16
+ setup: (build) => {
17
+ build.onStart(async () => {
18
+ const workingDir = build.initialOptions.absWorkingDir ?? process.cwd();
19
+ const relative = target ?? getOutputDir(build.initialOptions);
20
+ if (relative === void 0 || relative === "") return { errors: [{ text: "No path given and the build has neither outdir nor outfile." }] };
21
+ const resolved = path.resolve(workingDir, relative);
22
+ if (resolved === workingDir || isInside(resolved, workingDir)) return { errors: [{ text: `Refusing to delete "${resolved}", it is the working directory or one of its parents.` }] };
23
+ if (allowOutsideWorkingDir === false && isInside(workingDir, resolved) === false) return { errors: [{ text: `Refusing to delete "${resolved}", it is outside of the working directory "${workingDir}". Set allowOutsideWorkingDir to allow it.` }] };
24
+ try {
25
+ await fs.rm(resolved, {
26
+ force: true,
27
+ recursive: true
28
+ });
29
+ } catch (error) {
30
+ return { errors: [{ text: `Could not delete "${resolved}": ${error instanceof Error ? error.message : String(error)}` }] };
31
+ }
32
+ return {};
33
+ });
34
+ }
35
+ };
36
+ }
37
+ // src/index.ts
38
+ var src_default = clear;
39
+ export { src_default as default };
package/package.json CHANGED
@@ -1,25 +1,61 @@
1
1
  {
2
2
  "name": "esbuild-plugin-clear",
3
- "version": "1.0.3",
4
- "description": "a clear plugin for esbuild",
5
- "main": "./src/index.js",
3
+ "version": "2.0.0",
4
+ "description": "esbuild plugin that deletes the output folder before every build",
6
5
  "type": "module",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+https://github.com/DasRed/esbuild-plugin-clear.git"
10
- },
11
6
  "author": "Marco Starker",
12
7
  "license": "MIT",
13
8
  "bugs": {
14
9
  "url": "https://github.com/DasRed/esbuild-plugin-clear/issues"
15
10
  },
16
- "files": [
17
- "/src/*"
18
- ],
19
11
  "homepage": "https://www.marco-starker.de/en/projects/esbuild-plugin-clear/docs",
12
+ "engines": {
13
+ "node": ">=22.13.0"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/DasRed/esbuild-plugin-clear.git"
18
+ },
20
19
  "keywords": [
21
20
  "esbuild",
22
21
  "plugin",
23
- "clear"
24
- ]
22
+ "clear",
23
+ "clean"
24
+ ],
25
+ "sideEffects": false,
26
+ "main": "./dist/index.cjs",
27
+ "module": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "import": {
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "require": {
36
+ "types": "./dist/index.d.cts",
37
+ "default": "./dist/index.cjs"
38
+ }
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "files": [
43
+ "dist/*"
44
+ ],
45
+ "scripts": {
46
+ "build": "tsdown",
47
+ "test": "vitest run",
48
+ "coverage": "vitest run --coverage --reporter=default --reporter=junit --outputFile.junit=coverage/reports/unittest.xml"
49
+ },
50
+ "peerDependencies": {
51
+ "esbuild": ">=0.19.0"
52
+ },
53
+ "devDependencies": {
54
+ "@types/node": "^26.6.6",
55
+ "@vitest/coverage-v8": "^5.1.1",
56
+ "esbuild": "^0.28.2",
57
+ "tsdown": "^0.23.7",
58
+ "typescript": "^7.0.2",
59
+ "vitest": "^5.1.1"
60
+ }
25
61
  }
package/src/index.js DELETED
@@ -1,14 +0,0 @@
1
- import fs from 'fs';
2
-
3
- export default (path) => {
4
- return {
5
- name: 'Clear',
6
- setup: (build) => {
7
- build.onStart(() => {
8
- if (fs.existsSync(path)) {
9
- fs.rmSync(path, {recursive: true});
10
- }
11
- });
12
- },
13
- };
14
- }