@putout/plugin-esm 9.9.0 → 9.11.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 +38 -0
- package/lib/apply-import-attributes/index.js +44 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
20
20
|
|
|
21
21
|
- ✅ [apply-default-import](#apply-default-import);
|
|
22
22
|
- ✅ [apply-export-from](#apply-export-from);
|
|
23
|
+
- ✅ [apply-import-import](#apply-import-attributes);
|
|
23
24
|
- ✅ [convert-assert-to-with](#convert-assert-to-with);
|
|
24
25
|
- ✅ [declare-imports-first](#declare-imports-first);
|
|
25
26
|
- ✅ [group-imports-by-source](#group-imports-by-source);
|
|
@@ -48,6 +49,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
48
49
|
"rules": {
|
|
49
50
|
"esm/apply-default-import": "on",
|
|
50
51
|
"esm/apply-export-from": "on",
|
|
52
|
+
"esm/apply-import-attirbutes": "on",
|
|
51
53
|
"esm/declare-imports-first": "on",
|
|
52
54
|
"esm/group-imports-by-source": "on",
|
|
53
55
|
"esm/merge-duplicate-imports": "on",
|
|
@@ -122,6 +124,42 @@ export * as ns from 'x';
|
|
|
122
124
|
export {createAsyncLoader} from './load/async-loader.js';
|
|
123
125
|
```
|
|
124
126
|
|
|
127
|
+
### apply-import-attributes
|
|
128
|
+
|
|
129
|
+
> The **import attributes** feature instructs the runtime about how a module should be loaded, including the behavior of module resolution, fetching, parsing, and evaluation.
|
|
130
|
+
>
|
|
131
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with)
|
|
132
|
+
|
|
133
|
+
Check out 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d8d428c2555bacd69d530f84a41ab98e/15d41cf9153b3d2513c2bdf37d36c5276c6f6f95).
|
|
134
|
+
|
|
135
|
+
#### ❌ Example of incorrect code
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
import a from '../package.json';
|
|
139
|
+
|
|
140
|
+
export __export from './package.json';
|
|
141
|
+
|
|
142
|
+
await import('./package.json');
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### ✅ Example of correct code
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
import a from '../package.json' with {
|
|
149
|
+
type: 'json',
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export __export from './package.json' with {
|
|
153
|
+
type: 'json',
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
await import('./package.json', {
|
|
157
|
+
with: {
|
|
158
|
+
type: 'json',
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
125
163
|
### merge-declaration-with-export
|
|
126
164
|
|
|
127
165
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3fad517d76942d0a1f51d7f58a2799af/7a052ab18a31fa382228d6513c412be37091cfb8).
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {isStringLiteral} = types;
|
|
4
|
+
const TYPES = {
|
|
5
|
+
ImportDeclaration: 'import',
|
|
6
|
+
ExportNamedDeclaration: 'export',
|
|
7
|
+
ImportExpression: 'import',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const report = (path) => {
|
|
11
|
+
const type = TYPES[path.type];
|
|
12
|
+
return `Use \`${type} with {type: 'json'}\``;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const match = () => ({
|
|
16
|
+
'import __imports from "__a"': isJSONSource,
|
|
17
|
+
'export __exports from "__a"': isJSONSource,
|
|
18
|
+
'export * from "__a"': isJSONSource,
|
|
19
|
+
'import(__a)': isJSONSource,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const replace = () => ({
|
|
23
|
+
'import __imports from "__a"': `import __imports from "__a" with {
|
|
24
|
+
type: 'json',
|
|
25
|
+
}`,
|
|
26
|
+
'export * from "__a"': `export * from "__a" with {
|
|
27
|
+
type: 'json',
|
|
28
|
+
}`,
|
|
29
|
+
'export __exports from "__a"': `export __exports from "__a" with {
|
|
30
|
+
type: 'json',
|
|
31
|
+
}`,
|
|
32
|
+
'import(__a)': `import(__a, {
|
|
33
|
+
with: {
|
|
34
|
+
type: 'json',
|
|
35
|
+
}
|
|
36
|
+
})`,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
function isJSONSource({__a}) {
|
|
40
|
+
if (!isStringLiteral(__a))
|
|
41
|
+
return false;
|
|
42
|
+
|
|
43
|
+
return __a.value.endsWith('.json');
|
|
44
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as applyImportAttributes from './apply-import-attributes/index.js';
|
|
1
2
|
import * as applyDefaultImport from './apply-default-import/index.js';
|
|
2
3
|
import * as applyExportFrom from './apply-export-from/index.js';
|
|
3
4
|
import * as applyJsImportedFile from './apply-js-imported-file/index.js';
|
|
@@ -39,4 +40,5 @@ export const rules = {
|
|
|
39
40
|
'apply-privately-imported-file': ['off', applyPrivatelyImportedFile],
|
|
40
41
|
'apply-js-imported-file': ['off', applyJsImportedFile],
|
|
41
42
|
'apply-default-import': applyDefaultImport,
|
|
43
|
+
'apply-import-attributes': applyImportAttributes,
|
|
42
44
|
};
|
package/package.json
CHANGED