@rollup-extras/plugin-clean 1.0.1 → 1.0.2

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
@@ -15,15 +15,74 @@ Plugin runs on `renderStart` per each output and uses output.dir as a default ta
15
15
 
16
16
  Uses [`@niceties/logger`](https://github.com/kshutkin/niceties/blob/main/logger/README.md) to log messages, can be configured through `@niceties/logger` API.
17
17
 
18
+ [Changlelog](./CHANGELOG.md)
19
+
20
+ ## Installation
21
+
22
+ ```
23
+ npm install --save-dev @rollup-extras/plugin-clean
24
+ ```
25
+
26
+ ### Examples
27
+
28
+ Normal usage:
29
+
30
+ ```javascript
31
+ import clean from '@rollup-extras/plugin-clean';
32
+
33
+ export default {
34
+ input: 'src/index.js',
35
+
36
+ output: {
37
+ format: 'es',
38
+ dir: 'dest'
39
+ },
40
+
41
+ plugins: [clean()],
42
+ }
43
+ ```
44
+
45
+ To override / provide target directory to clean use:
46
+ ```javascript
47
+ clean('dir')
48
+ ```
49
+ or
50
+
51
+ ```javascript
52
+ clean(['dir1', 'dir2'])
53
+ ```
54
+ or
55
+
56
+ ```javascript
57
+ clean({targets: 'dir1'})
58
+ ```
59
+
60
+ or
61
+
62
+ ```javascript
63
+ clean({targets: ['dir1', 'dir2']})
64
+ ```
65
+
66
+ Other supporded fields in options object: `pluginName`, `deleteOnce`, `outputPlugin` and `verbose`.
67
+
68
+ `pluginName` is just for debugging purpose so you can understand which instance of plugin responsible for an error.
69
+
70
+ `deleteOnce` can be set to `false` if you want to clean directory every rebuild.
71
+
72
+ `outputPlugin` can be set to `false` if you want plugin to trigger earlier (use with caution, you may want to define `targets` youself in this mode)
73
+
74
+ `verbose` is to get more messages in console.
75
+
18
76
  ## Configuration
19
77
 
20
78
  ```typescript
21
- export type CleanPluginOptions = {
79
+ type CleanPluginOptions = {
22
80
  targets?: string | string[], // defaulted to output.dir per output
23
81
  pluginName?: string, // for debugging purposes, default is `@rollup-extras/plugin-clean`
24
- runOnce?: boolean, // default true
82
+ deleteOnce?: boolean, // default true
83
+ outputPlugin?: boolean, // default true
25
84
  verbose?: boolean // default false
26
- };
85
+ } | string | string[];
27
86
  ```
28
87
 
29
88
  ## Prior Art
package/dist/index.cjs CHANGED
@@ -9,34 +9,55 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
9
9
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
10
10
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
11
11
 
12
- function index ({ targets, pluginName, runOnce, verbose } = {}) {
13
- const deleted = new Set();
14
- pluginName !== null && pluginName !== void 0 ? pluginName : (pluginName = '@rollup-extras/plugin-clean');
15
- runOnce !== null && runOnce !== void 0 ? runOnce : (runOnce = true);
16
- return {
17
- name: pluginName,
18
- async renderStart(options) {
19
- if (!targets) {
20
- if (options.dir) {
21
- await removeDir(options.dir);
22
- }
23
- }
24
- else {
25
- if (Array.isArray(targets)) {
26
- await Promise.all(targets.map(removeDir));
27
- }
28
- else {
29
- await removeDir(targets);
30
- }
12
+ function index (options = {}) {
13
+ const deleted = new Map();
14
+ const normailizedOptions = normalizeOptions(options);
15
+ const { pluginName, deleteOnce, verbose, outputPlugin } = normailizedOptions;
16
+ let { targets } = normailizedOptions;
17
+ const pluginInstance = {
18
+ name: pluginName
19
+ };
20
+ if (outputPlugin) {
21
+ pluginInstance.renderStart = renderStart;
22
+ }
23
+ else {
24
+ pluginInstance.buildStart = buildStart;
25
+ pluginInstance.options = optionsHook;
26
+ }
27
+ return pluginInstance;
28
+ async function optionsHook(config) {
29
+ var _a;
30
+ if (!targets && config) {
31
+ targets = (Array.isArray(config.output) ? config.output.map(item => item.dir) : [(_a = config.output) === null || _a === void 0 ? void 0 : _a.dir])
32
+ .filter(Boolean);
33
+ }
34
+ return null;
35
+ }
36
+ async function buildStart() {
37
+ if (targets) {
38
+ await Promise.all(targets.map(removeDir));
39
+ }
40
+ }
41
+ async function renderStart(options) {
42
+ if (!targets) {
43
+ if (options.dir) {
44
+ await removeDir(options.dir);
31
45
  }
32
46
  }
33
- };
47
+ else {
48
+ await Promise.all(targets.map(removeDir));
49
+ }
50
+ }
34
51
  async function removeDir(dir) {
35
52
  const normalizedDir = normalizeSlash(path__default["default"].normalize(dir));
36
- if (runOnce && deleted.has(normalizedDir)) {
37
- return;
53
+ if (deleteOnce && deleted.has(normalizedDir)) {
54
+ return deleted.get(normalizedDir);
38
55
  }
39
- deleted.add(normalizedDir);
56
+ const removePromise = doRemove(normalizedDir);
57
+ deleted.set(normalizedDir, removePromise);
58
+ return removePromise;
59
+ }
60
+ async function doRemove(normalizedDir) {
40
61
  const logger$1 = logger.createLogger(pluginName);
41
62
  try {
42
63
  logger$1.start(`cleaning '${normalizedDir}'`, verbose ? 1 /* info */ : 0 /* verbose */);
@@ -54,6 +75,29 @@ function normalizeSlash(dir) {
54
75
  return `${dir.substring(0, dir.length - 1)}`;
55
76
  }
56
77
  return dir;
78
+ }
79
+ function normalizeOptions(userOptions) {
80
+ var _a, _b, _c, _d;
81
+ const options = {
82
+ pluginName: (_a = userOptions.pluginName) !== null && _a !== void 0 ? _a : '@rollup-extras/plugin-clean',
83
+ deleteOnce: (_b = userOptions.deleteOnce) !== null && _b !== void 0 ? _b : true,
84
+ verbose: (_c = userOptions.verbose) !== null && _c !== void 0 ? _c : false,
85
+ outputPlugin: (_d = userOptions.outputPlugin) !== null && _d !== void 0 ? _d : true,
86
+ targets: getTargets(userOptions)
87
+ };
88
+ return options;
89
+ }
90
+ function getTargets(userOptions) {
91
+ if (typeof userOptions === 'string') {
92
+ return [userOptions];
93
+ }
94
+ if (Array.isArray(userOptions)) {
95
+ return userOptions;
96
+ }
97
+ if (typeof userOptions === 'object') {
98
+ return 'targets' in userOptions ? getTargets(userOptions.targets) : undefined;
99
+ }
100
+ return [];
57
101
  }
58
102
 
59
103
  module.exports = index;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { PluginHooks } from 'rollup';
2
2
  import { CleanPluginOptions } from './types';
3
- export default function ({ targets, pluginName, runOnce, verbose }?: CleanPluginOptions): Partial<PluginHooks>;
3
+ export default function (options?: CleanPluginOptions): Partial<PluginHooks>;
package/dist/index.mjs CHANGED
@@ -2,34 +2,55 @@ import fs from 'fs/promises';
2
2
  import path from 'path';
3
3
  import { createLogger } from '@niceties/logger';
4
4
 
5
- function index ({ targets, pluginName, runOnce, verbose } = {}) {
6
- const deleted = new Set();
7
- pluginName !== null && pluginName !== void 0 ? pluginName : (pluginName = '@rollup-extras/plugin-clean');
8
- runOnce !== null && runOnce !== void 0 ? runOnce : (runOnce = true);
9
- return {
10
- name: pluginName,
11
- async renderStart(options) {
12
- if (!targets) {
13
- if (options.dir) {
14
- await removeDir(options.dir);
15
- }
16
- }
17
- else {
18
- if (Array.isArray(targets)) {
19
- await Promise.all(targets.map(removeDir));
20
- }
21
- else {
22
- await removeDir(targets);
23
- }
5
+ function index (options = {}) {
6
+ const deleted = new Map();
7
+ const normailizedOptions = normalizeOptions(options);
8
+ const { pluginName, deleteOnce, verbose, outputPlugin } = normailizedOptions;
9
+ let { targets } = normailizedOptions;
10
+ const pluginInstance = {
11
+ name: pluginName
12
+ };
13
+ if (outputPlugin) {
14
+ pluginInstance.renderStart = renderStart;
15
+ }
16
+ else {
17
+ pluginInstance.buildStart = buildStart;
18
+ pluginInstance.options = optionsHook;
19
+ }
20
+ return pluginInstance;
21
+ async function optionsHook(config) {
22
+ var _a;
23
+ if (!targets && config) {
24
+ targets = (Array.isArray(config.output) ? config.output.map(item => item.dir) : [(_a = config.output) === null || _a === void 0 ? void 0 : _a.dir])
25
+ .filter(Boolean);
26
+ }
27
+ return null;
28
+ }
29
+ async function buildStart() {
30
+ if (targets) {
31
+ await Promise.all(targets.map(removeDir));
32
+ }
33
+ }
34
+ async function renderStart(options) {
35
+ if (!targets) {
36
+ if (options.dir) {
37
+ await removeDir(options.dir);
24
38
  }
25
39
  }
26
- };
40
+ else {
41
+ await Promise.all(targets.map(removeDir));
42
+ }
43
+ }
27
44
  async function removeDir(dir) {
28
45
  const normalizedDir = normalizeSlash(path.normalize(dir));
29
- if (runOnce && deleted.has(normalizedDir)) {
30
- return;
46
+ if (deleteOnce && deleted.has(normalizedDir)) {
47
+ return deleted.get(normalizedDir);
31
48
  }
32
- deleted.add(normalizedDir);
49
+ const removePromise = doRemove(normalizedDir);
50
+ deleted.set(normalizedDir, removePromise);
51
+ return removePromise;
52
+ }
53
+ async function doRemove(normalizedDir) {
33
54
  const logger = createLogger(pluginName);
34
55
  try {
35
56
  logger.start(`cleaning '${normalizedDir}'`, verbose ? 1 /* info */ : 0 /* verbose */);
@@ -47,6 +68,29 @@ function normalizeSlash(dir) {
47
68
  return `${dir.substring(0, dir.length - 1)}`;
48
69
  }
49
70
  return dir;
71
+ }
72
+ function normalizeOptions(userOptions) {
73
+ var _a, _b, _c, _d;
74
+ const options = {
75
+ pluginName: (_a = userOptions.pluginName) !== null && _a !== void 0 ? _a : '@rollup-extras/plugin-clean',
76
+ deleteOnce: (_b = userOptions.deleteOnce) !== null && _b !== void 0 ? _b : true,
77
+ verbose: (_c = userOptions.verbose) !== null && _c !== void 0 ? _c : false,
78
+ outputPlugin: (_d = userOptions.outputPlugin) !== null && _d !== void 0 ? _d : true,
79
+ targets: getTargets(userOptions)
80
+ };
81
+ return options;
82
+ }
83
+ function getTargets(userOptions) {
84
+ if (typeof userOptions === 'string') {
85
+ return [userOptions];
86
+ }
87
+ if (Array.isArray(userOptions)) {
88
+ return userOptions;
89
+ }
90
+ if (typeof userOptions === 'object') {
91
+ return 'targets' in userOptions ? getTargets(userOptions.targets) : undefined;
92
+ }
93
+ return [];
50
94
  }
51
95
 
52
96
  export { index as default };
package/dist/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export declare type CleanPluginOptions = {
2
2
  targets?: string | string[];
3
3
  pluginName?: string;
4
- runOnce?: boolean;
4
+ deleteOnce?: boolean;
5
+ outputPlugin?: boolean;
5
6
  verbose?: boolean;
6
- };
7
+ } | string | string[];
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.1",
2
+ "version": "1.0.2",
3
3
  "license": "MIT",
4
4
  "name": "@rollup-extras/plugin-clean",
5
5
  "author": {