@putout/plugin-esm 9.9.0 → 9.10.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 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,42 @@
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"': ({__a}) => {
17
+ return __a.value.endsWith('.json');
18
+ },
19
+ 'export __exports from "__a"': ({__a}) => {
20
+ return __a.value.endsWith('.json');
21
+ },
22
+ 'import(__a)': ({__a}) => {
23
+ if (!isStringLiteral(__a))
24
+ return false;
25
+
26
+ return __a.value.endsWith('.json');
27
+ },
28
+ });
29
+
30
+ export const replace = () => ({
31
+ 'import __imports from "__a"': `import __imports from "__a" with {
32
+ type: 'json',
33
+ }`,
34
+ 'export __exports from "__a"': `export __exports from "__a" with {
35
+ type: 'json',
36
+ }`,
37
+ 'import(__a)': `import(__a, {
38
+ with: {
39
+ type: 'json',
40
+ }
41
+ })`,
42
+ });
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "9.9.0",
3
+ "version": "9.10.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",