@putout/engine-loader 9.1.0 → 9.2.1

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,14 +15,14 @@ npm i @putout/engine-loader
15
15
 
16
16
  Loader supports two kinds of plugins:
17
17
 
18
- - ☝️ [**Onerule Plugin**](#simple-plugin)
19
- - ☝️ [**Multirule Plugin**](#multirule-plugin)
18
+ - ☝️ [**Simple**](#simple-plugin)
19
+ - ☝️ [**Nested**](#nested-plugin)
20
20
 
21
- ### Onerule Plugin
21
+ ### Simple Plugin
22
22
 
23
23
  Simplest type of plugin support by [`@putout/engine-runner`](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#supported-plugin-types), contains one rule.
24
24
 
25
- ### Multirule Plugin
25
+ ### Nested Plugin
26
26
 
27
27
  Nested contains one or more rules:
28
28
 
@@ -82,6 +82,19 @@ const plugins = loadPlugins({
82
82
  });
83
83
  ```
84
84
 
85
+ When you need to use imported babel plugin use `babelPlugin`:
86
+
87
+ ```js
88
+ import {babelPlugin} from '@putout/engine-loader';
89
+ import transformBlockScoping from '@babel/plugin-transform-block-scoping';
90
+
91
+ const plugins = loadPlugins({
92
+ pluginNames: [
93
+ ['babel/transform-inline-consecutive-adds', babelPlugin(transformBlockScoping, 'Optional message')],
94
+ ],
95
+ });
96
+ ```
97
+
85
98
  ### loadProcessorsAsync
86
99
 
87
100
  ```js
@@ -104,13 +117,12 @@ Gives ability to create loader for `processor` or `formatter`.
104
117
  const {createAsyncLoader} = require('@putout/engine-loader');
105
118
  const {loadProcessor} = createAsyncLoader('processor');
106
119
 
120
+ // load @putout/processor-markdown
107
121
  await loadProcessors('markdown');
108
- // loads @putout/processor-markdown
109
-
122
+ // load @putout/processor-json using custom loader
110
123
  await loadProcess('json', () => {
111
124
  return Promise.resolve(`will be called instead of 'import'`);
112
125
  });
113
- // loads @putout/processor-json using custom loader
114
126
  ```
115
127
 
116
128
  ## License
@@ -47,4 +47,3 @@ async function cleverLoad(names, load = simpleImport) {
47
47
 
48
48
  return [e];
49
49
  }
50
-
@@ -3,8 +3,13 @@
3
3
  const addNodeModules = (a) => `${a}/node_modules`;
4
4
 
5
5
  module.exports = (path) => {
6
- const names = path.split('/').slice(1);
7
- const result = [''];
6
+ const names = path
7
+ .split('/')
8
+ .slice(1);
9
+
10
+ const result = [
11
+ '',
12
+ ];
8
13
 
9
14
  let current = '';
10
15
 
@@ -13,7 +18,5 @@ module.exports = (path) => {
13
18
  result.push(current);
14
19
  }
15
20
 
16
- return result
17
- .map(addNodeModules);
21
+ return result.map(addNodeModules);
18
22
  };
19
-
@@ -66,4 +66,3 @@ function diffLineMode(text1, text2) {
66
66
 
67
67
  return diffs;
68
68
  }
69
-
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const memo = require('nano-memoize');
3
+ const {nanomemoize} = require('nano-memoize');
4
4
 
5
5
  const isEnabled = require('./is-enabled');
6
6
  const {loadPlugin} = require('./load');
@@ -10,10 +10,12 @@ const parseProcessorNames = require('./parse-processor-names');
10
10
  const parseRules = require('./parse-rules');
11
11
  const validateRules = require('./validate-rules');
12
12
  const validatePlugin = require('./validate-plugin');
13
+ const {babelPlugin} = require('./wrap-plugin');
13
14
 
14
15
  const isString = (a) => typeof a === 'string';
15
16
 
16
17
  const defaultOptions = () => Object.create(null);
18
+
17
19
  const mergeRules = ([rule, plugin], rules) => {
18
20
  for (const currentRule of rules) {
19
21
  if (currentRule.rule !== rule)
@@ -40,12 +42,10 @@ const mergeRules = ([rule, plugin], rules) => {
40
42
  };
41
43
  };
42
44
 
43
- module.exports.loadProcessorsAsync = memo(async (options, load) => {
45
+ module.exports.loadProcessorsAsync = nanomemoize(async (options, load) => {
44
46
  check(options);
45
47
 
46
- const {
47
- processors = [],
48
- } = options;
48
+ const {processors = []} = options;
49
49
 
50
50
  const parsedProcessors = parseProcessorNames(processors);
51
51
  const loadProcessor = createAsyncLoader('processor');
@@ -66,18 +66,18 @@ module.exports.loadProcessorsAsync = memo(async (options, load) => {
66
66
 
67
67
  module.exports.createAsyncLoader = createAsyncLoader;
68
68
 
69
+ module.exports.babelPlugin = babelPlugin;
70
+
69
71
  module.exports.loadPlugins = (options) => {
70
72
  check(options);
71
73
 
72
- const {
73
- pluginNames = [],
74
- rules = {},
75
- } = options;
74
+ const {pluginNames = [], rules = {}} = options;
76
75
 
77
76
  const cookedRules = parseRules(rules);
78
77
  const loadedRules = getLoadedRules(cookedRules);
79
78
 
80
79
  const items = parsePluginNames(pluginNames);
80
+
81
81
  const plugins = loadPlugins({
82
82
  items,
83
83
  loadedRules,
@@ -99,7 +99,10 @@ module.exports.loadPlugins = (options) => {
99
99
  if (!isEnabled(name, cookedRules))
100
100
  continue;
101
101
 
102
- result.push(mergeRules([name, plugin], cookedRules));
102
+ result.push(mergeRules(
103
+ [name, plugin],
104
+ cookedRules,
105
+ ));
103
106
  }
104
107
 
105
108
  return result;
@@ -151,7 +154,10 @@ function loadPlugins({items, loadedRules}) {
151
154
  namespace,
152
155
  });
153
156
 
154
- validatePlugin({plugin, rule});
157
+ validatePlugin({
158
+ plugin,
159
+ rule,
160
+ });
155
161
 
156
162
  const {rules} = plugin;
157
163
 
@@ -160,10 +166,7 @@ function loadPlugins({items, loadedRules}) {
160
166
  continue;
161
167
  }
162
168
 
163
- plugins.push([
164
- rule,
165
- plugin,
166
- ]);
169
+ plugins.push([rule, plugin]);
167
170
  }
168
171
 
169
172
  return plugins;
@@ -192,4 +195,3 @@ function checkRule(rule) {
192
195
  if (!isString(rule))
193
196
  throw Error(`☝️ Looks like plugin name type is not 'string', but: '${typeof rule}'`);
194
197
  }
195
-
package/lib/is-enabled.js CHANGED
@@ -13,4 +13,3 @@ module.exports = (name, rules) => {
13
13
 
14
14
  return true;
15
15
  };
16
-
package/lib/load.js CHANGED
@@ -63,4 +63,3 @@ function getModulePath(name) {
63
63
 
64
64
  return [path, customRequire];
65
65
  }
66
-
@@ -24,4 +24,3 @@ module.exports = (plugins) => {
24
24
 
25
25
  return result;
26
26
  };
27
-
@@ -34,4 +34,3 @@ module.exports = (plugins) => {
34
34
 
35
35
  return result;
36
36
  };
37
-
@@ -67,7 +67,11 @@ function parseArray(rule, args) {
67
67
  const state = parseState(rule, rawState);
68
68
 
69
69
  if (args.length === 3) {
70
- const [, msg, options] = args;
70
+ const [
71
+ ,
72
+ msg,
73
+ options,
74
+ ] = args;
71
75
 
72
76
  return {
73
77
  rule,
@@ -144,4 +148,3 @@ function parseSubrules(rules) {
144
148
 
145
149
  return newRules;
146
150
  }
147
-
@@ -5,4 +5,3 @@ module.exports.simpleImport = async (url) => {
5
5
  const result = await import(url);
6
6
  return result.default || result;
7
7
  };
8
-
@@ -5,15 +5,18 @@ const {
5
5
  transformFromAstSync,
6
6
  } = require('@babel/core');
7
7
 
8
+ const isString = (a) => typeof a === 'string';
9
+
8
10
  module.exports = (ast, code, name) => {
11
+ const plugin = !isString(name) ? name : require(name);
12
+
9
13
  transformFromAstSync(ast, code, {
10
14
  cloneInputAst: false,
11
15
  plugins: [
12
16
  // globally installed modules support
13
- createConfigItem(require(name)),
17
+ createConfigItem(plugin),
14
18
  ],
15
19
  });
16
20
 
17
21
  return ast;
18
22
  };
19
-
@@ -20,4 +20,3 @@ module.exports = ({plugin, rule}) => {
20
20
 
21
21
  throw Error(`☝️ Cannot determine type of plugin '${rule}'. Here is list of supported plugins: https://git.io/JqcMn`);
22
22
  };
23
-
@@ -5,7 +5,9 @@ const parse = (rule) => {
5
5
  return rule;
6
6
 
7
7
  if (rule.includes('/'))
8
- return rule.split('/').shift();
8
+ return rule
9
+ .split('/')
10
+ .shift();
9
11
 
10
12
  return rule;
11
13
  };
@@ -32,4 +34,3 @@ module.exports = ({items, rules}) => {
32
34
  throw Error(`No plugin found for a rule: "${rule}"`);
33
35
  }
34
36
  };
35
-
@@ -7,9 +7,9 @@ const getPositions = require('./get-positions-by-diff');
7
7
 
8
8
  const babelTransform = require('./transforms/babel');
9
9
 
10
- const getMessage = (a) => a
11
- .replace(/@babel\/plugin-|babel-plugin-/, '')
12
- .replaceAll('-', ' ');
10
+ const BABEL_REG = /@babel\/plugin-|babel-plugin-/;
11
+
12
+ const getMessage = (a) => a.replace(BABEL_REG, '').replaceAll('-', ' ');
13
13
 
14
14
  const getModulePath = (name) => {
15
15
  const [, path] = tryCatch(require.resolve, name);
@@ -59,6 +59,14 @@ const getPlugin = ({name, transform, message}) => ({
59
59
  },
60
60
  });
61
61
 
62
+ module.exports.babelPlugin = (plugin, message) => {
63
+ return getPlugin({
64
+ name: plugin,
65
+ transform: babelTransform,
66
+ message,
67
+ });
68
+ };
69
+
62
70
  function getBabelPluginName(name) {
63
71
  const namespaced = getModulePath(`@babel/plugin-${name}`);
64
72
 
@@ -67,4 +75,3 @@ function getBabelPluginName(name) {
67
75
 
68
76
  return `babel-plugin-${name}`;
69
77
  }
70
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-loader",
3
- "version": "9.1.0",
3
+ "version": "9.2.1",
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",
@@ -27,7 +27,7 @@
27
27
  "@babel/core": "^7.12.3",
28
28
  "@putout/engine-parser": "^6.1.0",
29
29
  "diff-match-patch": "^1.0.4",
30
- "nano-memoize": "^2.0.0",
30
+ "nano-memoize": "^3.0.11",
31
31
  "once": "^1.4.0",
32
32
  "try-catch": "^3.0.0",
33
33
  "try-to-catch": "^3.0.1"
@@ -48,9 +48,10 @@
48
48
  "@putout/processor-javascript": "*",
49
49
  "@putout/processor-markdown": "*",
50
50
  "babel-plugin-angularjs-annotate": "https://github.com/putoutjs/babel-plugin-angularjs-annotate",
51
+ "babel-plugin-transform-inline-consecutive-adds": "^0.4.3",
51
52
  "c8": "^7.5.0",
52
53
  "eslint": "^8.0.1",
53
- "eslint-plugin-n": "^15.2.4",
54
+ "eslint-plugin-n": "^16.0.0",
54
55
  "eslint-plugin-putout": "^17.0.0",
55
56
  "estrace": "*",
56
57
  "just-camel-case": "^4.0.2",