@putout/engine-loader 4.11.0 → 4.12.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.
Files changed (2) hide show
  1. package/lib/load.js +39 -25
  2. package/package.json +1 -1
package/lib/load.js CHANGED
@@ -7,46 +7,60 @@ const once = require('once');
7
7
 
8
8
  const wrapPlugin = require('./wrap-plugin');
9
9
 
10
- // yarn PnP API
11
- // https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
12
- const {PUTOUT_YARN_PNP = 'putout'} = process.env;
13
- const createCustomRequire = once(() => createRequire(require.resolve(PUTOUT_YARN_PNP)));
14
-
15
10
  const bigFirst = (a) => `${a[0].toUpperCase()}${a.slice(1)}`;
16
11
 
17
- const getModulePath = (name) => {
18
- const customRequire = createCustomRequire();
12
+ const load = (type) => ({name, namespace}) => {
13
+ if (namespace !== 'putout')
14
+ return wrapPlugin(name, namespace);
15
+
16
+ const [pluginPath, customRequire] = getPath(namespace, type, name);
19
17
 
20
- const [, path] = tryCatch(customRequire.resolve, name);
21
- return path;
18
+ if (!pluginPath)
19
+ throw Error(`${bigFirst(type)} "${namespace}-${type}-${name}" could not be found!`);
20
+
21
+ return customRequire(pluginPath);
22
22
  };
23
23
 
24
+ module.exports.loadPlugin = load('plugin');
25
+ module.exports.loadProcessor = load('processor');
26
+
24
27
  function getPath(namespace, type, name) {
25
- let path = getModulePath(`@${namespace}/${type}-${name}`);
28
+ let [path, customRequire] = getModulePath(`@${namespace}/${type}-${name}`);
26
29
 
27
30
  if (!path)
28
- path = getModulePath(`${namespace}-${type}-${name}`);
31
+ [path, customRequire] = getModulePath(`${namespace}-${type}-${name}`);
29
32
 
30
33
  if (!path)
31
- path = getModulePath(name);
34
+ [path, customRequire] = getModulePath(name);
32
35
 
33
- return path;
36
+ return [path, customRequire];
34
37
  }
35
38
 
36
- const load = (type) => ({name, namespace}) => {
37
- if (namespace !== 'putout')
38
- return wrapPlugin(name, namespace);
39
+ const {PUTOUT_YARN_PNP = 'putout'} = process.env;
40
+
41
+ const createCustomRequire = once(() => createRequire(require.resolve(PUTOUT_YARN_PNP)));
42
+ const createPutoutRequire = once(() => createRequire(require.resolve('putout')));
43
+
44
+ // That's all for Yarn P'n'P
45
+ //
46
+ // We need to create a couple version of require for plugins, formatters and processors:
47
+ // - declared in 🐊Putout package.json;
48
+ // - declared in module that want to extend 🐊Putout;
49
+ //
50
+ // https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
51
+ function getModulePath(name) {
52
+ let path;
39
53
 
40
- const pluginPath = getPath(namespace, type, name);
54
+ const customRequire = createCustomRequire();
55
+ const putoutRequire = createPutoutRequire();
41
56
 
42
- if (!pluginPath)
43
- throw Error(`${bigFirst(type)} "${namespace}-${type}-${name}" could not be found!`);
57
+ [, path] = tryCatch(putoutRequire.resolve, name);
44
58
 
45
- const customRequire = createCustomRequire();
59
+ if (path)
60
+ return [path, putoutRequire];
46
61
 
47
- return customRequire(pluginPath);
48
- };
49
-
50
- module.exports.loadPlugin = load('plugin');
51
- module.exports.loadProcessor = load('processor');
62
+ [, path] = tryCatch(customRequire.resolve, name);
63
+
64
+ return [path, customRequire];
65
+ }
52
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-loader",
3
- "version": "4.11.0",
3
+ "version": "4.12.0",
4
4
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
5
5
  "description": "load plugins and prepare them to run",
6
6
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-loader",