@putout/plugin-esm 6.3.2 → 6.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.
- package/README.md +35 -3
- package/lib/apply-namespace-import-to-file/index.js +1 -1
- package/lib/index.js +2 -0
- package/lib/merge-export-declarations/index.js +6 -2
- package/lib/shorten-imported-file/get-imports/index.js +16 -0
- package/lib/shorten-imported-file/index.js +82 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -36,6 +36,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
36
36
|
|
|
37
37
|
- ✅ [apply-namespace-import-file](#resolve-imported-file);
|
|
38
38
|
- ✅ [resolve-imported-file](#resolve-imported-file);
|
|
39
|
+
- ✅ [shorten-imported-file](#shorten-imported-file);
|
|
39
40
|
|
|
40
41
|
## Config
|
|
41
42
|
|
|
@@ -47,8 +48,8 @@ npm i putout @putout/plugin-esm -D
|
|
|
47
48
|
"esm/declare-imports-first": "on",
|
|
48
49
|
"esm/group-imports-by-source": "on",
|
|
49
50
|
"esm/merge-duplicate-imports": "on",
|
|
50
|
-
"esm/merge-declaration-with-export": "
|
|
51
|
-
"esm/merge-export-declaration": "
|
|
51
|
+
"esm/merge-declaration-with-export": "on",
|
|
52
|
+
"esm/merge-export-declaration": "on",
|
|
52
53
|
"esm/remove-quotes-from-import-assertions": "on",
|
|
53
54
|
"esm/remove-empty-export": "on",
|
|
54
55
|
"esm/remove-empty-import": ["on", {
|
|
@@ -56,6 +57,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
56
57
|
}],
|
|
57
58
|
"esm/sort-imports-by-specifiers": "on",
|
|
58
59
|
"esm/resolve-imported-file": "off",
|
|
60
|
+
"esm/shorten-imported-file": "off",
|
|
59
61
|
"esm/apply-namespace-of-file": "off",
|
|
60
62
|
"esm/remove-useless-export-specifiers": "off"
|
|
61
63
|
}
|
|
@@ -417,6 +419,7 @@ Check out in 🐊**Putout Editor**:
|
|
|
417
419
|
- ✅ [`get-imports`](https://putout.cloudcmd.io/#/gist/5d7687215e9fbdf705935c444503dded/75a98d2db9d3847c73017e41637924b1cfd5a598);
|
|
418
420
|
- ✅ [`has-export-default`](https://putout.cloudcmd.io/#/gist/b50ccfe5cc8c0c97e2fc98b37903ade4/fbc026e6f1027581f7aa4879dcafcaa7754bf8f4);
|
|
419
421
|
- ✅ [`apply-namespace-import`](https://putout.cloudcmd.io/#/gist/23a6dc6741b772c03fbed95feda2b451/1fbecac6fc40282bcda0593aa666a8c213ef85b7);
|
|
422
|
+
- ✅ [`is-esm`](https://putout.cloudcmd.io/#/gist/fa080be2bf3a6560e289d84b5873c2bc/2601091f6bf97148843767968c3afcb36dde31de);
|
|
420
423
|
|
|
421
424
|
Let's consider file structure:
|
|
422
425
|
|
|
@@ -447,7 +450,6 @@ Check out in 🐊**Putout Editor**:
|
|
|
447
450
|
|
|
448
451
|
- ✅ [`resolve-imported-file`](https://putout.cloudcmd.io/#/gist/241489cb2781dd37ec96baf0115cde4e/83c2f2e9f490850b7fda432f8d25ae6a64ed07e3);
|
|
449
452
|
- ✅ [`get-imports`](https://putout.cloudcmd.io/#/gist/ee10100fed86e4db926885dd54298668/7538bca7a9ae006d976f41261c0ed4c0e1902ace);
|
|
450
|
-
- ✅ [`is-esm`](https://putout.cloudcmd.io/#/gist/fa080be2bf3a6560e289d84b5873c2bc/2601091f6bf97148843767968c3afcb36dde31de);
|
|
451
453
|
- ✅ [`change-imports`](https://putout.cloudcmd.io/#/gist/23a6dc6741b772c03fbed95feda2b451/1fbecac6fc40282bcda0593aa666a8c213ef85b7);
|
|
452
454
|
|
|
453
455
|
Let's consider file structure:
|
|
@@ -473,6 +475,36 @@ import a from './a';
|
|
|
473
475
|
import a from './a.js';
|
|
474
476
|
```
|
|
475
477
|
|
|
478
|
+
### shorten-imported-file
|
|
479
|
+
|
|
480
|
+
Check out in 🐊**Putout Editor**:
|
|
481
|
+
|
|
482
|
+
- ✅ [`get-imports`](https://putout.cloudcmd.io/#/gist/ee10100fed86e4db926885dd54298668/7538bca7a9ae006d976f41261c0ed4c0e1902ace);
|
|
483
|
+
- ✅ [`change-imports`](https://putout.cloudcmd.io/#/gist/23a6dc6741b772c03fbed95feda2b451/1fbecac6fc40282bcda0593aa666a8c213ef85b7);
|
|
484
|
+
|
|
485
|
+
Let's consider file structure:
|
|
486
|
+
|
|
487
|
+
```
|
|
488
|
+
/
|
|
489
|
+
|-- processors/
|
|
490
|
+
| `-- index.js
|
|
491
|
+
| `-- parse-prosessor-names.js
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
In this case `index.js` can be fixed:
|
|
495
|
+
|
|
496
|
+
#### ❌ Example of incorrect code
|
|
497
|
+
|
|
498
|
+
```js
|
|
499
|
+
import {parseProcessorNames} from '../processors/parse-processor-names.js';
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
#### ✅ Example of correct code
|
|
503
|
+
|
|
504
|
+
```js
|
|
505
|
+
import {parseProcessorNames} from './parse-processor-names.js';
|
|
506
|
+
```
|
|
507
|
+
|
|
476
508
|
## License
|
|
477
509
|
|
|
478
510
|
MIT
|
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as removeUselessExportSpecifiers from './remove-useless-export-specifie
|
|
|
4
4
|
import * as mergeDeclarationWithExport from './merge-declaration-with-export/index.js';
|
|
5
5
|
import * as applyNamespaceImportToFile from './apply-namespace-import-to-file/index.js';
|
|
6
6
|
import * as resolveImportedFile from './resolve-imported-file/index.js';
|
|
7
|
+
import * as shortenImportedFile from './shorten-imported-file/index.js';
|
|
7
8
|
import * as addIndexToImport from './add-index-to-import/index.js';
|
|
8
9
|
import * as declareImportsFirst from './declare-imports-first/index.js';
|
|
9
10
|
import * as groupImportsBySource from './group-imports-by-source/index.js';
|
|
@@ -26,6 +27,7 @@ export const rules = {
|
|
|
26
27
|
'remove-empty-export': removeEmptyExport,
|
|
27
28
|
'sort-imports-by-specifiers': sortImportsBySpecifiers,
|
|
28
29
|
'resolve-imported-file': ['off', resolveImportedFile],
|
|
30
|
+
'shorten-imported-file': ['off', shortenImportedFile],
|
|
29
31
|
'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
|
|
30
32
|
'merge-declaration-with-export': mergeDeclarationWithExport,
|
|
31
33
|
'remove-useless-export-specifiers': removeUselessExportSpecifiers,
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const report = (path) => path.node.source.value;
|
|
2
|
+
|
|
3
|
+
export const fix = () => {};
|
|
4
|
+
|
|
5
|
+
export const include = () => [
|
|
6
|
+
'ImportDeclaration',
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
export const filter = (path) => {
|
|
10
|
+
const {value} = path.node.source;
|
|
11
|
+
|
|
12
|
+
if (!value.startsWith('.'))
|
|
13
|
+
return false;
|
|
14
|
+
|
|
15
|
+
return /\.m?js/.test(value);
|
|
16
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
join,
|
|
3
|
+
dirname,
|
|
4
|
+
basename,
|
|
5
|
+
} from 'node:path';
|
|
6
|
+
import {
|
|
7
|
+
parse,
|
|
8
|
+
print,
|
|
9
|
+
transform,
|
|
10
|
+
operator,
|
|
11
|
+
} from 'putout';
|
|
12
|
+
import * as getImports from './get-imports/index.js';
|
|
13
|
+
import * as changeImports from '../resolve-imported-file/change-imports/index.js';
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
getFilename,
|
|
17
|
+
readFileContent,
|
|
18
|
+
writeFileContent,
|
|
19
|
+
} = operator;
|
|
20
|
+
|
|
21
|
+
const getMessage = (a) => a.message;
|
|
22
|
+
|
|
23
|
+
export const report = (file, {from, to}) => `Shorten import source: '${from}' -> '${to}'`;
|
|
24
|
+
export const fix = (file, {content, ast, from, to}) => {
|
|
25
|
+
transform(ast, content, {
|
|
26
|
+
rules: {
|
|
27
|
+
'change-imports': ['on', {
|
|
28
|
+
from,
|
|
29
|
+
to,
|
|
30
|
+
}],
|
|
31
|
+
},
|
|
32
|
+
plugins: [
|
|
33
|
+
['change-imports', changeImports],
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const newContent = print(ast);
|
|
38
|
+
|
|
39
|
+
writeFileContent(file, newContent);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const scan = (rootPath, {push, trackFile}) => {
|
|
43
|
+
const mask = [
|
|
44
|
+
'*.js',
|
|
45
|
+
'*.mjs',
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
for (const file of trackFile(rootPath, mask)) {
|
|
49
|
+
const content = readFileContent(file);
|
|
50
|
+
|
|
51
|
+
if (!content.includes('import'))
|
|
52
|
+
continue;
|
|
53
|
+
|
|
54
|
+
const ast = parse(content);
|
|
55
|
+
|
|
56
|
+
const places = transform(ast, content, {
|
|
57
|
+
plugins: [
|
|
58
|
+
['get-imports', getImports],
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const imports = places.map(getMessage);
|
|
63
|
+
const filename = getFilename(file);
|
|
64
|
+
const dir = dirname(filename);
|
|
65
|
+
const baseDir = basename(dir);
|
|
66
|
+
|
|
67
|
+
for (const from of imports) {
|
|
68
|
+
if (!from.startsWith(`../${baseDir}/`))
|
|
69
|
+
continue;
|
|
70
|
+
|
|
71
|
+
const to = join(dir, from).replace(dir, '.');
|
|
72
|
+
|
|
73
|
+
if (to.length < from.length)
|
|
74
|
+
push(file, {
|
|
75
|
+
from,
|
|
76
|
+
to,
|
|
77
|
+
content,
|
|
78
|
+
ast,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-esm",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.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",
|
|
@@ -47,22 +47,22 @@
|
|
|
47
47
|
"@putout/plugin-reuse-duplicate-init": "*",
|
|
48
48
|
"@putout/plugin-tape": "*",
|
|
49
49
|
"@putout/plugin-typescript": "*",
|
|
50
|
-
"@putout/test": "^
|
|
50
|
+
"@putout/test": "^15.0.0",
|
|
51
51
|
"c8": "^10.0.0",
|
|
52
52
|
"eslint": "^10.0.0-alpha.0",
|
|
53
53
|
"eslint-plugin-n": "^17.0.0",
|
|
54
54
|
"eslint-plugin-putout": "^29.0.0",
|
|
55
|
-
"madrun": "^
|
|
55
|
+
"madrun": "^12.0.0",
|
|
56
56
|
"montag": "^1.2.1",
|
|
57
57
|
"nodemon": "^3.0.1",
|
|
58
|
-
"supertape": "^
|
|
58
|
+
"supertape": "^12.0.0"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
61
|
"putout": ">=41"
|
|
62
62
|
},
|
|
63
63
|
"license": "MIT",
|
|
64
64
|
"engines": {
|
|
65
|
-
"node": ">=
|
|
65
|
+
"node": ">=22"
|
|
66
66
|
},
|
|
67
67
|
"publishConfig": {
|
|
68
68
|
"access": "public"
|