@putout/engine-loader 11.1.1 → 11.2.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
@@ -81,6 +81,57 @@ const plugins = await loadPluginsAsync({
81
81
  });
82
82
  ```
83
83
 
84
+ #### `import`
85
+
86
+ You can also use schema like this one:
87
+
88
+ ```
89
+ import:escover/plugin
90
+ ```
91
+
92
+ ```js
93
+ const plugins = await loadPluginsAsync({
94
+ pluginNames: [
95
+ 'import:escover/plugin',
96
+ ],
97
+ });
98
+ ```
99
+
100
+ Or when used `putoutAsync`:
101
+
102
+ ```js
103
+ import {putoutAsync} from 'putout';
104
+
105
+ await putoutAsync(`module.exports.hello = 'world'`, {
106
+ plugins: [
107
+ 'import:escover/plugin',
108
+ ],
109
+ });
110
+ ```
111
+
112
+ Which is the same as:
113
+
114
+ ```js
115
+ import {putoutAsync} from 'putout';
116
+ import * as plugin from 'escover/plugin';
117
+
118
+ await putoutAsync(`module.exports.hello = 'world'`, {
119
+ plugins: [
120
+ ['escover/plugin', plugin],
121
+ ],
122
+ });
123
+ ```
124
+
125
+ Or used inside `.putout.json`:
126
+
127
+ ```json
128
+ {
129
+ "plugins": [
130
+ "import:escover/plugin"
131
+ ]
132
+ }
133
+ ```
134
+
84
135
  ### loadProcessorsAsync
85
136
 
86
137
  ```js
@@ -1,22 +1,24 @@
1
1
  'use strict';
2
2
 
3
+ const {nanomemoize} = require('nano-memoize');
3
4
  const tryToCatch = require('try-to-catch');
4
5
  const {simpleImport} = require('./simple-import');
5
6
 
6
7
  const {assign} = Object;
7
8
  const stub = () => () => {};
8
9
 
9
- module.exports.createAsyncLoader = (type) => async (name, load) => {
10
+ module.exports.createAsyncLoader = (type) => nanomemoize(async (name, load) => {
10
11
  if (name === 'none')
11
12
  return stub();
12
13
 
13
- const [e, reporter] = await cleverLoad([`@putout/${type}-${name}`, `putout-${type}-${name}`], load);
14
-
15
- if (e)
16
- throw e;
14
+ if (name.startsWith('import:')) {
15
+ const shortName = name.replace('import:', '');
16
+
17
+ return await cleverLoad([shortName], load);
18
+ }
17
19
 
18
- return reporter;
19
- };
20
+ return await cleverLoad([`@putout/${type}-${name}`, `putout-${type}-${name}`], load);
21
+ });
20
22
 
21
23
  async function cleverLoad(names, load = simpleImport) {
22
24
  let e;
@@ -26,7 +28,7 @@ async function cleverLoad(names, load = simpleImport) {
26
28
  [e, reporter] = await tryToCatch(load, name);
27
29
 
28
30
  if (!e)
29
- return [null, reporter];
31
+ return reporter;
30
32
 
31
33
  if (e.code === 'ERR_MODULE_NOT_FOUND')
32
34
  continue;
@@ -35,12 +37,12 @@ async function cleverLoad(names, load = simpleImport) {
35
37
  message: `${name}: ${e.message}`,
36
38
  });
37
39
 
38
- return [e];
40
+ throw e;
39
41
  }
40
42
 
41
43
  assign(e, {
42
44
  message: e.message.replace(/\simported.*/, ''),
43
45
  });
44
46
 
45
- return [e];
47
+ throw e;
46
48
  }
@@ -2,16 +2,16 @@
2
2
 
3
3
  const isEnabled = require('./is-enabled');
4
4
  const parseRules = require('./parse-rules');
5
- const {nanomemoize} = require('nano-memoize');
6
5
  const {createAsyncLoader} = require('./async-loader');
7
6
  const parsePluginNames = require('./parse-plugin-names');
8
7
  const validateRules = require('./validate-rules');
9
8
  const validatePlugin = require('./validate-plugin');
10
9
  const {mergeRules} = require('./merge-rules');
11
10
 
11
+ const loadPluginAsync = createAsyncLoader('plugin');
12
12
  const isString = (a) => typeof a === 'string';
13
13
 
14
- module.exports.loadPluginsAsync = nanomemoize(async (options) => {
14
+ module.exports.loadPluginsAsync = async (options) => {
15
15
  check(options);
16
16
 
17
17
  const {pluginNames = [], rules = {}} = options;
@@ -49,14 +49,13 @@ module.exports.loadPluginsAsync = nanomemoize(async (options) => {
49
49
  }
50
50
 
51
51
  return result;
52
- });
52
+ };
53
53
 
54
54
  function splitRule(rule) {
55
55
  return [rule, 'putout'];
56
56
  }
57
57
 
58
58
  async function loadPlugins({items, loadedRules}) {
59
- const loadPlugin = createAsyncLoader('plugin');
60
59
  const promises = [];
61
60
  const enabledRules = [];
62
61
 
@@ -67,7 +66,7 @@ async function loadPlugins({items, loadedRules}) {
67
66
  checkRule(rule);
68
67
 
69
68
  const [name] = splitRule(rule);
70
- const plugin = itemPlugin || loadPlugin(name);
69
+ const plugin = itemPlugin || loadPluginAsync(name);
71
70
 
72
71
  enabledRules.push(rule);
73
72
  promises.push(plugin);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-loader",
3
- "version": "11.1.1",
3
+ "version": "11.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "load plugins and prepare them to run",