@putout/plugin-esm 9.1.0 → 9.3.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 +5 -0
- package/lib/apply-name-to-imported-file/apply-named-import/index.js +21 -2
- package/lib/apply-name-to-imported-file/get-imports/index.js +22 -2
- package/lib/apply-name-to-imported-file/index.js +6 -4
- package/lib/resolve-imported-file/change-imports/index.js +14 -2
- package/lib/resolve-imported-file/index.js +6 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -528,6 +528,7 @@ Let's consider file structure:
|
|
|
528
528
|
|
|
529
529
|
```
|
|
530
530
|
/
|
|
531
|
+
├── package.json
|
|
531
532
|
└── lib/
|
|
532
533
|
├── index.js
|
|
533
534
|
└── a.js
|
|
@@ -539,12 +540,16 @@ In this case `index.js` can be fixed:
|
|
|
539
540
|
|
|
540
541
|
```js
|
|
541
542
|
import a from './a';
|
|
543
|
+
import info from '../package';
|
|
542
544
|
```
|
|
543
545
|
|
|
544
546
|
#### ✅ Example of correct code
|
|
545
547
|
|
|
546
548
|
```js
|
|
547
549
|
import a from './a.js';
|
|
550
|
+
import info from '../package.json' with {
|
|
551
|
+
type: 'json',
|
|
552
|
+
};
|
|
548
553
|
```
|
|
549
554
|
|
|
550
555
|
### shorten-imported-file
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import {types} from 'putout';
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {getTemplateValues} = operator;
|
|
4
|
+
const {
|
|
5
|
+
isImportDefaultSpecifier,
|
|
6
|
+
isVariableDeclaration,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
const DYNAMIC = 'const __identifier__a = await import(__b)';
|
|
4
10
|
|
|
5
11
|
export const report = (path) => {
|
|
12
|
+
if (isVariableDeclaration(path)) {
|
|
13
|
+
const {
|
|
14
|
+
__identifier__a,
|
|
15
|
+
__b,
|
|
16
|
+
} = getTemplateValues(path, DYNAMIC);
|
|
17
|
+
|
|
18
|
+
const {name} = __identifier__a;
|
|
19
|
+
const source = __b.value;
|
|
20
|
+
|
|
21
|
+
return `'const ${name} = await import("${source}")' -> 'const {${name}} = await import("${source}")'`;
|
|
22
|
+
}
|
|
23
|
+
|
|
6
24
|
const source = path.node.source.value;
|
|
7
25
|
const {specifiers} = path.node;
|
|
8
26
|
const [first] = specifiers;
|
|
@@ -24,6 +42,7 @@ export const replace = ({options}) => {
|
|
|
24
42
|
const {name, source} = options;
|
|
25
43
|
|
|
26
44
|
return {
|
|
45
|
+
[`const ${name} = await import("${source}")`]: `const {${name}} = await import("${source}")`,
|
|
27
46
|
[`import ${name} from "${source}"`]: `import {${name}} from "${source}"`,
|
|
28
47
|
[`export * as ${name} from "${source}"`]: `export {${name}} from "${source}"`,
|
|
29
48
|
};
|
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import {types} from 'putout';
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {getTemplateValues} = operator;
|
|
4
|
+
const {
|
|
5
|
+
isImportDefaultSpecifier,
|
|
6
|
+
isVariableDeclaration,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
const DYNAMIC = 'const __identifier__a = await import(__b)';
|
|
4
10
|
|
|
5
11
|
export const include = () => [
|
|
6
12
|
'import __a from "__b"',
|
|
7
13
|
'export * as __a from "__b"',
|
|
14
|
+
DYNAMIC,
|
|
8
15
|
];
|
|
9
16
|
|
|
10
17
|
export const report = (path) => {
|
|
@@ -26,6 +33,19 @@ const CommentLine = (value) => ({
|
|
|
26
33
|
});
|
|
27
34
|
|
|
28
35
|
const getImport = (path) => {
|
|
36
|
+
if (isVariableDeclaration(path)) {
|
|
37
|
+
const {
|
|
38
|
+
__identifier__a,
|
|
39
|
+
__b,
|
|
40
|
+
} = getTemplateValues(path, DYNAMIC);
|
|
41
|
+
|
|
42
|
+
return [
|
|
43
|
+
__identifier__a.name,
|
|
44
|
+
__b.value,
|
|
45
|
+
'dynamic',
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
|
|
29
49
|
const source = path.node.source.value;
|
|
30
50
|
const [first] = path.node.specifiers;
|
|
31
51
|
|
|
@@ -17,11 +17,13 @@ const {
|
|
|
17
17
|
|
|
18
18
|
export const report = (file, {name, source, type}) => {
|
|
19
19
|
const filename = getFilename(file);
|
|
20
|
+
const reports = {
|
|
21
|
+
import: `Use \`import {${name}} from '${source}'\` in '${filename}'`,
|
|
22
|
+
dynamic: `Use \`const {${name}} = await import('${source}')\` in '${filename}'`,
|
|
23
|
+
export: `Use \`export {${name}} from '${source}'\` in '${filename}'`,
|
|
24
|
+
};
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
|
|
23
|
-
|
|
24
|
-
return `Use \`export {${name}} from '${source}'\` in '${filename}'`;
|
|
26
|
+
return reports[type];
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
export const fix = (file, {name, source, content, ast}) => {
|
|
@@ -8,8 +8,20 @@ export const replace = ({options}) => {
|
|
|
8
8
|
if (!from || !to)
|
|
9
9
|
return {};
|
|
10
10
|
|
|
11
|
+
if (!to.endsWith('json'))
|
|
12
|
+
return {
|
|
13
|
+
[`import __imports from '${from}'`]: `import __imports from '${to}'`,
|
|
14
|
+
[`import('${from}')`]: `import('${to}')`,
|
|
15
|
+
};
|
|
16
|
+
|
|
11
17
|
return {
|
|
12
|
-
[`import __imports from '${from}'`]: `import __imports from '${to}'
|
|
13
|
-
|
|
18
|
+
[`import __imports from '${from}'`]: `import __imports from '${to}' with {
|
|
19
|
+
type: 'json',
|
|
20
|
+
}`,
|
|
21
|
+
[`import('${from}')`]: `import('${to}', {
|
|
22
|
+
with: {
|
|
23
|
+
type: 'json',
|
|
24
|
+
}
|
|
25
|
+
})`,
|
|
14
26
|
};
|
|
15
27
|
};
|
|
@@ -91,6 +91,7 @@ function buildResolved(rootPath, importsTuples) {
|
|
|
91
91
|
for (const [relative, current] of importsTuples) {
|
|
92
92
|
const withIndex = join(current, 'index.js');
|
|
93
93
|
const withJs = `${current}.js`;
|
|
94
|
+
const withJson = `${current}.json`;
|
|
94
95
|
|
|
95
96
|
if (findFile(rootPath, withIndex).length) {
|
|
96
97
|
if (relative.endsWith('/')) {
|
|
@@ -102,6 +103,11 @@ function buildResolved(rootPath, importsTuples) {
|
|
|
102
103
|
continue;
|
|
103
104
|
}
|
|
104
105
|
|
|
106
|
+
if (findFile(rootPath, withJson).length) {
|
|
107
|
+
result.push([relative, `${relative}.json`]);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
105
111
|
if (relative === '..') {
|
|
106
112
|
const withPackage = join(current, 'package.json');
|
|
107
113
|
const [packageJson] = findFile(rootPath, withPackage);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-esm",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.3.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",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@putout/plugin-typescript": "*",
|
|
60
60
|
"@putout/test": "^15.0.0",
|
|
61
61
|
"c8": "^10.0.0",
|
|
62
|
-
"eslint": "^10.0.0
|
|
62
|
+
"eslint": "^10.0.0",
|
|
63
63
|
"eslint-plugin-n": "^17.0.0",
|
|
64
64
|
"eslint-plugin-putout": "^30.0.0",
|
|
65
65
|
"madrun": "^12.0.0",
|