@rollup-extras/plugin-clean 1.3.9 → 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
@@ -4,7 +4,7 @@ Rollup plugin to clean a directory during the build.
4
4
 
5
5
  Points:
6
6
 
7
- - Uses fs.rm to remove directories that shipped with nodejs and has built-in retries
7
+ - Uses `fs.rm` (shipped with Node.js) to remove directories, with built-in retries
8
8
  - Can be used with no configuration
9
9
  - Runs once per directory by default (good for watch mode)
10
10
  - A minimal amount of logs by default
package/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ declare module '@rollup-extras/plugin-clean' {
2
+ import type { Plugin } from 'rollup';
3
+ export default function _default(options?: CleanPluginOptions): Plugin & {
4
+ api: {
5
+ addInstance(): Plugin;
6
+ };
7
+ };
8
+ export type CleanPluginOptionsObject = {
9
+ targets?: string | string[];
10
+ pluginName?: string;
11
+ deleteOnce?: boolean;
12
+ outputPlugin?: boolean;
13
+ verbose?: boolean;
14
+ };
15
+ export type CleanPluginOptions = CleanPluginOptionsObject | string | string[];
16
+
17
+ export {};
18
+ }
19
+
20
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "version": 3,
3
+ "file": "index.d.ts",
4
+ "names": [
5
+ "CleanPluginOptionsObject",
6
+ "CleanPluginOptions"
7
+ ],
8
+ "sources": [
9
+ "index.js"
10
+ ],
11
+ "sourcesContent": [
12
+ null
13
+ ],
14
+ "mappings": ";;;;;;;;;;;;;;",
15
+ "ignoreList": []
16
+ }
@@ -1,57 +1,86 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import { createLogger } from '@niceties/logger';
1
+ import { rm } from 'node:fs/promises';
2
+ import { dirname, normalize } from 'node:path';
3
+
4
+ import { createLogger, LogLevel } from '@niceties/logger';
5
+ import { multiConfigPluginBase } from '@rollup-extras/utils/multi-config-plugin-base';
4
6
  import { getOptions } from '@rollup-extras/utils/options';
5
- import { multiConfigPluginBase } from '@rollup-extras/utils/mutli-config-plugin-base';
6
7
 
7
- function index (options = {}) {
8
+ export default function (options = {}) {
9
+
8
10
  const inProgress = new Map();
11
+
9
12
  const hasChildrenInProgress = new Map();
10
13
  let deleted = false;
11
- const normalizedOptions = getOptions(options, {
12
- pluginName: '@rollup-extras/plugin-clean',
13
- deleteOnce: true,
14
- verbose: false,
15
- outputPlugin: true
16
- }, 'targets');
14
+
15
+ const normalizedOptions = getOptions(
16
+ options,
17
+ {
18
+ pluginName: '@rollup-extras/plugin-clean',
19
+ deleteOnce: true,
20
+ verbose: false,
21
+ outputPlugin: true,
22
+ },
23
+ 'targets'
24
+ );
17
25
  const { pluginName, deleteOnce, verbose, outputPlugin } = normalizedOptions;
18
26
  let { targets } = normalizedOptions;
27
+
19
28
  const instance = multiConfigPluginBase(false, pluginName, cleanup);
20
- const baseAddInstance = instance.api.addInstance;
21
- const baseRenderStart = instance.renderStart;
29
+ const baseAddInstance = (instance.api.addInstance);
30
+ const baseRenderStart =
31
+ (
32
+ (instance).renderStart
33
+ );
34
+
22
35
  if (outputPlugin) {
23
- instance.renderStart = async function (options, inputOptions) {
24
- baseRenderStart.call(this, options, inputOptions);
25
- await renderStart(options);
36
+
37
+ instance.renderStart = async function (
38
+ outputOptions,
39
+ inputOptions
40
+ ) {
41
+ baseRenderStart.call(this, outputOptions, inputOptions);
42
+ await renderStart(outputOptions);
26
43
  };
27
- }
28
- else {
44
+ } else {
29
45
  instance.buildStart = buildStart;
30
46
  instance.options = optionsHook;
31
47
  }
48
+
32
49
  instance.api.addInstance = () => {
33
50
  const instance = baseAddInstance();
34
- const baseRenderStart = instance.renderStart;
51
+ const baseRenderStart =
52
+ (
53
+ (instance).renderStart
54
+ );
55
+
35
56
  if (outputPlugin) {
36
- instance.renderStart = async function (options, inputOptions) {
37
- baseRenderStart.call(this, options, inputOptions);
38
- await renderStart(options);
57
+
58
+ instance.renderStart = async function (
59
+ outputOptions,
60
+ inputOptions
61
+ ) {
62
+ baseRenderStart.call(this, outputOptions, inputOptions);
63
+ await renderStart(outputOptions);
39
64
  };
40
- }
41
- else {
65
+ } else {
42
66
  instance.buildStart = buildStart;
43
67
  instance.options = optionsHook;
44
68
  }
69
+
45
70
  return instance;
46
71
  };
72
+
47
73
  return instance;
74
+
48
75
  async function optionsHook(config) {
49
76
  if (!targets && config) {
50
- targets = (Array.isArray(config.output) ? config.output.map(item => item.dir) : [config.output?.dir])
51
- .filter(Boolean);
77
+ targets = (
78
+ (Array.isArray(config.output) ? config.output.map(item => item.dir) : [config.output?.dir]).filter(Boolean)
79
+ );
52
80
  }
53
81
  return null;
54
82
  }
83
+
55
84
  async function buildStart() {
56
85
  if (deleted) {
57
86
  return;
@@ -60,6 +89,7 @@ function index (options = {}) {
60
89
  await Promise.all(targets.map(removeDir));
61
90
  }
62
91
  }
92
+
63
93
  async function renderStart(options) {
64
94
  if (deleted) {
65
95
  return;
@@ -68,67 +98,77 @@ function index (options = {}) {
68
98
  if (options.dir) {
69
99
  await removeDir(options.dir);
70
100
  }
71
- }
72
- else {
101
+ } else {
73
102
  await Promise.all(targets.map(removeDir));
74
103
  }
75
104
  }
105
+
76
106
  async function removeDir(dir) {
77
- const normalizedDir = normalizeSlash(path.normalize(dir));
107
+ const normalizedDir = normalizeSlash(normalize(dir));
78
108
  if (inProgress.has(normalizedDir)) {
79
109
  return outputPlugin && inProgress.get(normalizedDir);
80
110
  }
111
+
81
112
  let removePromise;
113
+
82
114
  let parentsInProgress;
83
115
  if (hasChildrenInProgress.has(dir)) {
84
- removePromise = Promise.resolve(hasChildrenInProgress.get(dir))
85
- .then(() => doRemove(normalizedDir));
86
- }
87
- else if ((parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item))).length > 0) {
88
- return inProgress.get(parentsInProgress[0]);
89
- }
90
- else {
116
+ removePromise = Promise.resolve(hasChildrenInProgress.get(dir)).then(() => doRemove(normalizedDir));
117
+ } else {
118
+ parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item));
119
+ if (parentsInProgress.length > 0) {
120
+ return inProgress.get( (parentsInProgress[0]));
121
+ }
91
122
  removePromise = doRemove(normalizedDir);
92
123
  }
93
124
  inProgress.set(normalizedDir, removePromise);
94
125
  for (const parentDir of parentDirs(dir)) {
95
126
  if (!hasChildrenInProgress.has(parentDir)) {
96
127
  hasChildrenInProgress.set(parentDir, removePromise);
97
- }
98
- else {
99
- hasChildrenInProgress.set(parentDir, Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]));
128
+ } else {
129
+ hasChildrenInProgress.set(
130
+ parentDir,
131
+ (
132
+ (Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]))
133
+ )
134
+ );
100
135
  }
101
136
  }
102
137
  return removePromise;
103
138
  }
139
+
104
140
  async function doRemove(normalizedDir) {
105
141
  const logger = createLogger(pluginName);
106
142
  try {
107
- logger.start(`cleaning '${normalizedDir}'`, verbose ? 1 /* LogLevel.info */ : 0 /* LogLevel.verbose */);
108
- await fs.rm(normalizedDir, { recursive: true });
143
+ logger.start(`cleaning '${normalizedDir}'`, verbose ? LogLevel.info : LogLevel.verbose);
144
+ await rm(normalizedDir, { recursive: true });
109
145
  logger.finish(`cleaned '${normalizedDir}'`);
110
- }
111
- catch (e) { // eslint-disable-line @typescript-eslint/no-explicit-any
112
- const loglevel = e['code'] === 'ENOENT' ? undefined : 2 /* LogLevel.warn */;
113
- logger.finish(`failed cleaning '${normalizedDir}'`, loglevel, e);
146
+ } catch ( e) {
147
+ const loglevel = (e).code === 'ENOENT' ? undefined : LogLevel.warn;
148
+ logger.finish(`failed cleaning '${normalizedDir}'`, loglevel, (e));
114
149
  }
115
150
  }
151
+
116
152
  function cleanup() {
117
153
  inProgress.clear();
118
154
  hasChildrenInProgress.clear();
119
155
  deleted = deleteOnce;
120
156
  }
121
157
  }
158
+
122
159
  function normalizeSlash(dir) {
123
160
  if (dir.endsWith('/')) {
124
161
  return `${dir.substring(0, dir.length - 1)}`;
125
162
  }
126
163
  return dir;
127
164
  }
165
+
128
166
  function* parentDirs(dir) {
129
- while (((dir = path.dirname(dir)) !== '.') && (dir !== '/')) {
167
+ for (;;) {
168
+ dir = dirname(dir);
169
+ if (dir === '.' || dir === '/') {
170
+ break;
171
+ }
130
172
  yield dir;
131
173
  }
132
174
  }
133
-
134
- export { index as default };
package/package.json CHANGED
@@ -1,25 +1,19 @@
1
1
  {
2
- "type": "module",
3
- "version": "1.3.9",
4
- "name": "@rollup-extras/plugin-clean",
5
- "description": "Rollup plugin to clean a directory during build.",
2
+ "version": "2.0.0",
6
3
  "license": "MIT",
4
+ "name": "@rollup-extras/plugin-clean",
7
5
  "author": "Konstantin Shutkin",
8
- "main": "./dist/index.cjs",
9
- "module": "./dist/index.mjs",
6
+ "type": "module",
7
+ "types": "./index.d.ts",
10
8
  "exports": {
11
9
  ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.cjs",
15
- "default": "./dist/index.mjs"
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
16
12
  },
17
13
  "./package.json": "./package.json"
18
14
  },
19
- "files": [
20
- "dist",
21
- "LICENSE"
22
- ],
15
+ "main": "./index.js",
16
+ "module": "./index.js",
23
17
  "engines": {
24
18
  "node": ">=15"
25
19
  },
@@ -31,22 +25,22 @@
31
25
  "bugs": "https://github.com/kshutkin/rollup-extras/issues",
32
26
  "homepage": "https://github.com/kshutkin/rollup-extras/blob/main/plugin-clean/README.md",
33
27
  "readme": "README.md",
28
+ "description": "Rollup plugin to clean a directory during build.",
34
29
  "keywords": [
35
30
  "rollup-plugin",
36
31
  "clean",
37
32
  "delete"
38
33
  ],
39
34
  "dependencies": {
40
- "@niceties/logger": "^1.1.4",
41
- "@rollup-extras/utils": "^1.4.5"
35
+ "@niceties/logger": "^2.1.0",
36
+ "@rollup-extras/utils": "^2.0.0"
42
37
  },
43
38
  "peerDependencies": {
44
- "rollup": "^2.60.1 || ^3.0.0 || ^4.0.0"
39
+ "rollup": "^4.0.0"
45
40
  },
46
41
  "peerDependenciesMeta": {
47
42
  "rollup": {
48
43
  "optional": true
49
44
  }
50
- },
51
- "types": "./dist/index.d.ts"
45
+ }
52
46
  }
package/dist/index.cjs DELETED
@@ -1,136 +0,0 @@
1
- 'use strict';
2
-
3
- var fs = require('fs/promises');
4
- var path = require('path');
5
- var logger = require('@niceties/logger');
6
- var options = require('@rollup-extras/utils/options');
7
- var mutliConfigPluginBase = require('@rollup-extras/utils/mutli-config-plugin-base');
8
-
9
- function index (options$1 = {}) {
10
- const inProgress = new Map();
11
- const hasChildrenInProgress = new Map();
12
- let deleted = false;
13
- const normalizedOptions = options.getOptions(options$1, {
14
- pluginName: '@rollup-extras/plugin-clean',
15
- deleteOnce: true,
16
- verbose: false,
17
- outputPlugin: true
18
- }, 'targets');
19
- const { pluginName, deleteOnce, verbose, outputPlugin } = normalizedOptions;
20
- let { targets } = normalizedOptions;
21
- const instance = mutliConfigPluginBase.multiConfigPluginBase(false, pluginName, cleanup);
22
- const baseAddInstance = instance.api.addInstance;
23
- const baseRenderStart = instance.renderStart;
24
- if (outputPlugin) {
25
- instance.renderStart = async function (options, inputOptions) {
26
- baseRenderStart.call(this, options, inputOptions);
27
- await renderStart(options);
28
- };
29
- }
30
- else {
31
- instance.buildStart = buildStart;
32
- instance.options = optionsHook;
33
- }
34
- instance.api.addInstance = () => {
35
- const instance = baseAddInstance();
36
- const baseRenderStart = instance.renderStart;
37
- if (outputPlugin) {
38
- instance.renderStart = async function (options, inputOptions) {
39
- baseRenderStart.call(this, options, inputOptions);
40
- await renderStart(options);
41
- };
42
- }
43
- else {
44
- instance.buildStart = buildStart;
45
- instance.options = optionsHook;
46
- }
47
- return instance;
48
- };
49
- return instance;
50
- async function optionsHook(config) {
51
- if (!targets && config) {
52
- targets = (Array.isArray(config.output) ? config.output.map(item => item.dir) : [config.output?.dir])
53
- .filter(Boolean);
54
- }
55
- return null;
56
- }
57
- async function buildStart() {
58
- if (deleted) {
59
- return;
60
- }
61
- if (targets) {
62
- await Promise.all(targets.map(removeDir));
63
- }
64
- }
65
- async function renderStart(options) {
66
- if (deleted) {
67
- return;
68
- }
69
- if (!targets) {
70
- if (options.dir) {
71
- await removeDir(options.dir);
72
- }
73
- }
74
- else {
75
- await Promise.all(targets.map(removeDir));
76
- }
77
- }
78
- async function removeDir(dir) {
79
- const normalizedDir = normalizeSlash(path.normalize(dir));
80
- if (inProgress.has(normalizedDir)) {
81
- return outputPlugin && inProgress.get(normalizedDir);
82
- }
83
- let removePromise;
84
- let parentsInProgress;
85
- if (hasChildrenInProgress.has(dir)) {
86
- removePromise = Promise.resolve(hasChildrenInProgress.get(dir))
87
- .then(() => doRemove(normalizedDir));
88
- }
89
- else if ((parentsInProgress = Array.from(parentDirs(dir)).filter(item => inProgress.has(item))).length > 0) {
90
- return inProgress.get(parentsInProgress[0]);
91
- }
92
- else {
93
- removePromise = doRemove(normalizedDir);
94
- }
95
- inProgress.set(normalizedDir, removePromise);
96
- for (const parentDir of parentDirs(dir)) {
97
- if (!hasChildrenInProgress.has(parentDir)) {
98
- hasChildrenInProgress.set(parentDir, removePromise);
99
- }
100
- else {
101
- hasChildrenInProgress.set(parentDir, Promise.all([removePromise, hasChildrenInProgress.get(parentDir)]));
102
- }
103
- }
104
- return removePromise;
105
- }
106
- async function doRemove(normalizedDir) {
107
- const logger$1 = logger.createLogger(pluginName);
108
- try {
109
- logger$1.start(`cleaning '${normalizedDir}'`, verbose ? 1 /* LogLevel.info */ : 0 /* LogLevel.verbose */);
110
- await fs.rm(normalizedDir, { recursive: true });
111
- logger$1.finish(`cleaned '${normalizedDir}'`);
112
- }
113
- catch (e) { // eslint-disable-line @typescript-eslint/no-explicit-any
114
- const loglevel = e['code'] === 'ENOENT' ? undefined : 2 /* LogLevel.warn */;
115
- logger$1.finish(`failed cleaning '${normalizedDir}'`, loglevel, e);
116
- }
117
- }
118
- function cleanup() {
119
- inProgress.clear();
120
- hasChildrenInProgress.clear();
121
- deleted = deleteOnce;
122
- }
123
- }
124
- function normalizeSlash(dir) {
125
- if (dir.endsWith('/')) {
126
- return `${dir.substring(0, dir.length - 1)}`;
127
- }
128
- return dir;
129
- }
130
- function* parentDirs(dir) {
131
- while (((dir = path.dirname(dir)) !== '.') && (dir !== '/')) {
132
- yield dir;
133
- }
134
- }
135
-
136
- module.exports = index;
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { Plugin } from 'rollup';
2
- import { CleanPluginOptions } from './types';
3
- export default function (options?: CleanPluginOptions): Plugin & {
4
- api: {
5
- addInstance(): Plugin;
6
- };
7
- };
package/dist/types.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export type CleanPluginOptions = {
2
- targets?: string | string[];
3
- pluginName?: string;
4
- deleteOnce?: boolean;
5
- outputPlugin?: boolean;
6
- verbose?: boolean;
7
- } | string | string[];