@putout/plugin-esm 9.4.0 → 9.5.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.
@@ -0,0 +1,71 @@
1
+ import {join} from 'node:path';
2
+ import {operator} from 'putout';
3
+
4
+ const {readFileContent} = operator;
5
+
6
+ export const buildResolved = ({dir, imports, rootPath, crawlFile}) => {
7
+ const importsTuples = buildImports(dir, imports);
8
+ const result = [];
9
+
10
+ for (const [relative, current] of importsTuples) {
11
+ const withIndex = join(current, 'index.js');
12
+ const withJs = `${current}.js`;
13
+ const withJson = `${current}.json`;
14
+
15
+ if (crawlFile(rootPath, withIndex).length) {
16
+ if (relative.endsWith('/')) {
17
+ result.push([relative, `${relative}index.js`]);
18
+ continue;
19
+ }
20
+
21
+ result.push([relative, `${relative}/index.js`]);
22
+ continue;
23
+ }
24
+
25
+ if (crawlFile(rootPath, withJson).length) {
26
+ result.push([relative, `${relative}.json`]);
27
+ continue;
28
+ }
29
+
30
+ if (relative.startsWith('..')) {
31
+ result.push(parseMainFromDotDot({
32
+ relative,
33
+ current,
34
+ rootPath,
35
+ crawlFile,
36
+ }));
37
+ continue;
38
+ }
39
+
40
+ if (crawlFile(rootPath, withJs).length) {
41
+ result.push([relative, `${relative}.js`]);
42
+ continue;
43
+ }
44
+ }
45
+
46
+ return result.filter(Boolean);
47
+ };
48
+
49
+ function parseMainFromDotDot({relative, current, rootPath, crawlFile}) {
50
+ const withPackage = join(current, 'package.json');
51
+ const [packageJson] = crawlFile(rootPath, withPackage);
52
+
53
+ if (!packageJson)
54
+ return;
55
+
56
+ const json = readFileContent(packageJson);
57
+ const {main} = JSON.parse(json);
58
+
59
+ return [relative, join(relative, main)];
60
+ }
61
+
62
+ function buildImports(dir, imports) {
63
+ const list = [];
64
+
65
+ for (const current of imports) {
66
+ const full = join(dir, current);
67
+ list.push([current, full]);
68
+ }
69
+
70
+ return list;
71
+ }
@@ -1,4 +1,4 @@
1
- import {join, dirname} from 'node:path';
1
+ import {dirname} from 'node:path';
2
2
  import {
3
3
  parse,
4
4
  print,
@@ -7,9 +7,9 @@ import {
7
7
  } from 'putout';
8
8
  import * as changeImports from '#change-imports';
9
9
  import * as getImports from './get-imports/index.js';
10
+ import {buildResolved} from './build-resolved.js';
10
11
 
11
12
  const {
12
- findFile,
13
13
  getFilename,
14
14
  readFileContent,
15
15
  writeFileContent,
@@ -36,7 +36,7 @@ export const fix = (file, {content, ast, from, to}) => {
36
36
  writeFileContent(file, newContent);
37
37
  };
38
38
 
39
- export const scan = (rootPath, {push, trackFile}) => {
39
+ export const scan = (rootPath, {push, trackFile, crawlFile}) => {
40
40
  const mask = [
41
41
  '*.js',
42
42
  '*.mjs',
@@ -60,8 +60,12 @@ export const scan = (rootPath, {push, trackFile}) => {
60
60
  const filename = getFilename(file);
61
61
  const dir = dirname(filename);
62
62
 
63
- const importsTuples = buildImports(dir, imports);
64
- const resolvedTuples = buildResolved(rootPath, importsTuples);
63
+ const resolvedTuples = buildResolved({
64
+ dir,
65
+ imports,
66
+ rootPath,
67
+ crawlFile,
68
+ });
65
69
 
66
70
  for (const [from, to] of resolvedTuples) {
67
71
  push(file, {
@@ -73,61 +77,3 @@ export const scan = (rootPath, {push, trackFile}) => {
73
77
  }
74
78
  }
75
79
  };
76
-
77
- function buildImports(dir, imports) {
78
- const list = [];
79
-
80
- for (const current of imports) {
81
- const full = join(dir, current);
82
- list.push([current, full]);
83
- }
84
-
85
- return list;
86
- }
87
-
88
- function buildResolved(rootPath, importsTuples) {
89
- const result = [];
90
-
91
- for (const [relative, current] of importsTuples) {
92
- const withIndex = join(current, 'index.js');
93
- const withJs = `${current}.js`;
94
- const withJson = `${current}.json`;
95
-
96
- if (findFile(rootPath, withIndex).length) {
97
- if (relative.endsWith('/')) {
98
- result.push([relative, `${relative}index.js`]);
99
- continue;
100
- }
101
-
102
- result.push([relative, `${relative}/index.js`]);
103
- continue;
104
- }
105
-
106
- if (findFile(rootPath, withJson).length) {
107
- result.push([relative, `${relative}.json`]);
108
- continue;
109
- }
110
-
111
- if (relative === '..' || relative === '../') {
112
- const withPackage = join(current, 'package.json');
113
- const [packageJson] = findFile(rootPath, withPackage);
114
-
115
- if (!packageJson)
116
- continue;
117
-
118
- const json = readFileContent(packageJson);
119
- const {main} = JSON.parse(json);
120
-
121
- result.push([relative, join(relative, main)]);
122
-
123
- continue;
124
- }
125
-
126
- if (findFile(rootPath, withJs).length) {
127
- result.push([relative, `${relative}.js`]);
128
- continue;
129
- }
130
- }
131
-
132
- return result;
133
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "9.4.0",
3
+ "version": "9.5.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin improves ability to transform ESM code",