@putout/plugin-esm 7.5.1 → 7.7.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.
@@ -6,6 +6,7 @@ import putout, {
6
6
  transform,
7
7
  operator,
8
8
  } from 'putout';
9
+ import {createGetPrivateImports} from '#private-imports';
9
10
  import * as isESMPlugin from './is-esm/index.js';
10
11
  import * as hasExportDefaultPlugin from './has-export-default/index.js';
11
12
  import * as applyNamespaceImportPlugin from './apply-namespace-import/index.js';
@@ -51,6 +52,8 @@ export const scan = (rootPath, {push, trackFile}) => {
51
52
  '*.mjs',
52
53
  ];
53
54
 
55
+ const getPrivateImports = createGetPrivateImports();
56
+
54
57
  for (const file of trackFile(rootPath, mask)) {
55
58
  const content = readFileContent(file);
56
59
  const [error, ast] = tryCatch(parse, content);
@@ -60,8 +63,18 @@ export const scan = (rootPath, {push, trackFile}) => {
60
63
 
61
64
  const importsTuples = getImports(file, content, ast);
62
65
 
66
+ const [, privateImports] = getPrivateImports(file, {
67
+ aliasBased: true,
68
+ });
69
+
63
70
  for (const [name, source, importedFilename] of importsTuples) {
64
- if (hasImportDefault(rootPath, importedFilename))
71
+ const is = hasImportDefault({
72
+ rootPath,
73
+ importedFilename,
74
+ privateImports,
75
+ });
76
+
77
+ if (is)
65
78
  continue;
66
79
 
67
80
  push(file, {
@@ -93,8 +106,20 @@ function getImports(file, content, ast) {
93
106
  return buildImports(dir, imports);
94
107
  }
95
108
 
96
- function hasImportDefault(rootPath, importedFilename) {
97
- const [importedFile] = findFile(rootPath, importedFilename);
109
+ function parseImportedFilename({importedFilename, privateImports}) {
110
+ if (privateImports.has(importedFilename))
111
+ return privateImports.get(importedFilename);
112
+
113
+ return importedFilename;
114
+ }
115
+
116
+ function hasImportDefault({rootPath, importedFilename, privateImports}) {
117
+ const parsedName = parseImportedFilename({
118
+ importedFilename,
119
+ privateImports,
120
+ });
121
+
122
+ const [importedFile] = findFile(rootPath, parsedName);
98
123
 
99
124
  if (!importedFile)
100
125
  return true;
@@ -118,12 +143,19 @@ function hasImportDefault(rootPath, importedFilename) {
118
143
  return !esm.length;
119
144
  }
120
145
 
146
+ function parseFull(dir, source) {
147
+ if (source.startsWith('#'))
148
+ return source;
149
+
150
+ return join(dir, source);
151
+ }
152
+
121
153
  function buildImports(dir, imports) {
122
154
  const list = [];
123
155
 
124
156
  for (const current of imports) {
125
157
  const [name, source] = current.split(' <- ');
126
- const full = join(dir, source);
158
+ const full = parseFull(dir, source);
127
159
 
128
160
  list.push([name, source, full]);
129
161
  }
@@ -1,9 +1,4 @@
1
- import {
2
- join,
3
- dirname,
4
- resolve,
5
- } from 'node:path';
6
- import {tryCatch} from 'try-catch';
1
+ import {join, dirname} from 'node:path';
7
2
  import {
8
3
  parse,
9
4
  print,
@@ -12,19 +7,14 @@ import {
12
7
  } from 'putout';
13
8
  import * as getImports from '#get-imports';
14
9
  import * as changeImports from '#change-imports';
15
-
16
- const isString = (a) => typeof a === 'string';
10
+ import {createGetPrivateImports} from '#private-imports';
17
11
 
18
12
  const {
19
13
  getFilename,
20
14
  readFileContent,
21
15
  writeFileContent,
22
- findFileUp,
23
16
  } = operator;
24
17
 
25
- const {entries} = Object;
26
- const {parse: parseJson} = JSON;
27
-
28
18
  const getMessage = (a) => a.message;
29
19
 
30
20
  export const report = (file, {from, to}) => `Apply privately imported source: '${from}' -> '${to}'`;
@@ -91,58 +81,3 @@ export const scan = (rootPath, {push, trackFile}) => {
91
81
  }
92
82
  }
93
83
  };
94
-
95
- const createGetPrivateImports = (importsCache = new Map(), emptyMap = new Map()) => (file) => {
96
- const filename = getFilename(file);
97
- const dir = dirname(filename);
98
-
99
- if (importsCache.has(dir))
100
- return [dir, importsCache.get(dir)];
101
-
102
- const [packageDirectory, packagePath] = findFileUp(file, 'package.json');
103
-
104
- if (!packagePath) {
105
- importsCache.set(dir, {});
106
- return ['', emptyMap];
107
- }
108
-
109
- const packageContent = readFileContent(packagePath);
110
- const [error, packageJson] = tryCatch(parseJson, packageContent);
111
-
112
- if (error) {
113
- importsCache.set(dir, emptyMap);
114
-
115
- return ['', emptyMap];
116
- }
117
-
118
- const {imports = {}} = packageJson;
119
- const importsEntries = new Map();
120
-
121
- for (const [alias, property] of entries(imports)) {
122
- const filePath = parseProperty(property);
123
-
124
- if (!filePath)
125
- continue;
126
-
127
- const resolvedPath = resolve(packageDirectory, filePath);
128
-
129
- importsEntries.set(resolvedPath, alias);
130
- }
131
-
132
- importsCache.set(dir, [packageDirectory, importsEntries]);
133
-
134
- return [packageDirectory, importsEntries];
135
- };
136
-
137
- function parseProperty(property) {
138
- if (isString(property))
139
- return property;
140
-
141
- const {
142
- default: filePath,
143
- node,
144
- browser,
145
- } = property;
146
-
147
- return filePath || node || browser;
148
- }
@@ -0,0 +1,81 @@
1
+ import {dirname, resolve} from 'node:path';
2
+ import {tryCatch} from 'try-catch';
3
+ import {operator} from 'putout';
4
+
5
+ const {
6
+ getFilename,
7
+ readFileContent,
8
+ findFileUp,
9
+ } = operator;
10
+
11
+ const {entries} = Object;
12
+ const {parse: parseJson} = JSON;
13
+ const isString = (a) => typeof a === 'string';
14
+
15
+ function insert(a, b, {importsEntries, aliasBased}) {
16
+ if (aliasBased) {
17
+ importsEntries.set(b, a);
18
+ return;
19
+ }
20
+
21
+ importsEntries.set(a, b);
22
+ }
23
+
24
+ export const createGetPrivateImports = (importsCache = new Map(), emptyMap = new Map()) => (file, options = {}) => {
25
+ const filename = getFilename(file);
26
+ const dir = dirname(filename);
27
+ const {aliasBased = false} = options;
28
+
29
+ if (importsCache.has(dir))
30
+ return [dir, importsCache.get(dir)];
31
+
32
+ const [packageDirectory, packagePath] = findFileUp(file, 'package.json');
33
+
34
+ if (!packagePath) {
35
+ importsCache.set(dir, emptyMap);
36
+ return ['', emptyMap];
37
+ }
38
+
39
+ const packageContent = readFileContent(packagePath);
40
+ const [error, packageJson] = tryCatch(parseJson, packageContent);
41
+
42
+ if (error) {
43
+ importsCache.set(dir, emptyMap);
44
+
45
+ return ['', emptyMap];
46
+ }
47
+
48
+ const {imports = {}} = packageJson;
49
+ const importsEntries = new Map();
50
+
51
+ for (const [alias, property] of entries(imports)) {
52
+ const filePath = parseProperty(property);
53
+
54
+ if (!filePath)
55
+ continue;
56
+
57
+ const resolvedPath = resolve(packageDirectory, filePath);
58
+
59
+ insert(resolvedPath, alias, {
60
+ importsEntries,
61
+ aliasBased,
62
+ });
63
+ }
64
+
65
+ importsCache.set(packageDirectory, importsEntries);
66
+
67
+ return [packageDirectory, importsEntries];
68
+ };
69
+
70
+ function parseProperty(property) {
71
+ if (isString(property))
72
+ return property;
73
+
74
+ const {
75
+ default: filePath,
76
+ node,
77
+ browser,
78
+ } = property;
79
+
80
+ return filePath || node || browser;
81
+ }
@@ -93,6 +93,11 @@ function buildResolved(rootPath, importsTuples) {
93
93
  const withJs = `${current}.js`;
94
94
 
95
95
  if (findFile(rootPath, withIndex).length) {
96
+ if (relative.endsWith('/')) {
97
+ result.push([relative, `${relative}index.js`]);
98
+ continue;
99
+ }
100
+
96
101
  result.push([relative, `${relative}/index.js`]);
97
102
  continue;
98
103
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "7.5.1",
3
+ "version": "7.7.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",
@@ -12,7 +12,8 @@
12
12
  },
13
13
  "imports": {
14
14
  "#get-imports": "./lib/shorten-imported-file/get-imports/index.js",
15
- "#change-imports": "./lib/resolve-imported-file/change-imports/index.js"
15
+ "#change-imports": "./lib/resolve-imported-file/change-imports/index.js",
16
+ "#private-imports": "./lib/apply-privately-imported-file/private-imports.js"
16
17
  },
17
18
  "release": false,
18
19
  "tag": false,
@@ -55,7 +56,7 @@
55
56
  "c8": "^10.0.0",
56
57
  "eslint": "^10.0.0-alpha.0",
57
58
  "eslint-plugin-n": "^17.0.0",
58
- "eslint-plugin-putout": "^29.0.0",
59
+ "eslint-plugin-putout": "^30.0.0",
59
60
  "madrun": "^12.0.0",
60
61
  "montag": "^1.2.1",
61
62
  "nodemon": "^3.0.1",