@docusaurus/remark-plugin-npm2yarn 2.4.0 → 3.0.0-alpha.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/lib/index.d.ts +6 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +103 -36
- package/lib/index.js.map +1 -1
- package/package.json +7 -5
- package/src/index.ts +149 -57
package/lib/index.d.ts
CHANGED
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
type Plugin<T> = any;
|
|
8
|
+
type KnownConverter = 'yarn' | 'pnpm';
|
|
9
|
+
type CustomConverter = [name: string, cb: (npmCode: string) => string];
|
|
10
|
+
type Converter = CustomConverter | KnownConverter;
|
|
11
|
+
type PluginOptions = {
|
|
10
12
|
sync?: boolean;
|
|
11
|
-
converters?:
|
|
13
|
+
converters?: Converter[];
|
|
12
14
|
};
|
|
13
15
|
declare const plugin: Plugin<[PluginOptions?]>;
|
|
14
16
|
export = plugin;
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,KAAK,MAAM,CAAC,CAAC,IAAI,GAAG,CAAC;AAErB,KAAK,cAAc,GAAG,MAAM,GAAG,MAAM,CAAC;AAEtC,KAAK,eAAe,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;AAEvE,KAAK,SAAS,GAAG,eAAe,GAAG,cAAc,CAAC;AAElD,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;CAC1B,CAAC;AA4IF,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC,CAgCpC,CAAC;AAIF,SAAS,MAAM,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -8,62 +8,129 @@
|
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
|
|
10
10
|
const npm_to_yarn_1 = tslib_1.__importDefault(require("npm-to-yarn"));
|
|
11
|
-
function
|
|
12
|
-
return
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
function createAttribute(attributeName, attributeValue) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'mdxJsxAttribute',
|
|
14
|
+
name: attributeName,
|
|
15
|
+
value: attributeValue,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function createTabItem({ code, node, value, label, }) {
|
|
19
|
+
return {
|
|
20
|
+
type: 'mdxJsxFlowElement',
|
|
21
|
+
name: 'TabItem',
|
|
22
|
+
attributes: [
|
|
23
|
+
createAttribute('value', value),
|
|
24
|
+
label && createAttribute('label', label),
|
|
25
|
+
].filter((attr) => Boolean(attr)),
|
|
26
|
+
children: [
|
|
27
|
+
{
|
|
28
|
+
type: node.type,
|
|
29
|
+
lang: node.lang,
|
|
30
|
+
value: code,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
27
34
|
}
|
|
28
35
|
const transformNode = (node, isSync, converters) => {
|
|
29
|
-
const groupIdProp = isSync
|
|
36
|
+
const groupIdProp = isSync
|
|
37
|
+
? {
|
|
38
|
+
type: 'mdxJsxAttribute',
|
|
39
|
+
name: 'groupId',
|
|
40
|
+
value: 'npm2yarn',
|
|
41
|
+
}
|
|
42
|
+
: undefined;
|
|
30
43
|
const npmCode = node.value;
|
|
44
|
+
function createConvertedTabItem(converter) {
|
|
45
|
+
if (typeof converter === 'string') {
|
|
46
|
+
return createTabItem({
|
|
47
|
+
code: (0, npm_to_yarn_1.default)(npmCode, converter),
|
|
48
|
+
node,
|
|
49
|
+
value: converter,
|
|
50
|
+
label: converter === 'yarn' ? 'Yarn' : converter,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const [converterName, converterFn] = converter;
|
|
54
|
+
return createTabItem({
|
|
55
|
+
code: converterFn(npmCode),
|
|
56
|
+
node,
|
|
57
|
+
value: converterName,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
31
60
|
return [
|
|
32
61
|
{
|
|
33
|
-
type: '
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
{
|
|
41
|
-
type: 'jsx',
|
|
42
|
-
value: '</Tabs>',
|
|
62
|
+
type: 'mdxJsxFlowElement',
|
|
63
|
+
name: 'Tabs',
|
|
64
|
+
attributes: [groupIdProp].filter(Boolean),
|
|
65
|
+
children: [
|
|
66
|
+
createTabItem({ code: npmCode, node, value: 'npm' }),
|
|
67
|
+
...converters.flatMap(createConvertedTabItem),
|
|
68
|
+
],
|
|
43
69
|
},
|
|
44
70
|
];
|
|
45
71
|
};
|
|
46
|
-
const
|
|
72
|
+
const isMdxEsmLiteral = (node) => node.type === 'mdxjsEsm';
|
|
73
|
+
// TODO legacy approximation, good-enough for now but not 100% accurate
|
|
74
|
+
const isTabsImport = (node) => isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');
|
|
47
75
|
const isParent = (node) => Array.isArray(node.children);
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
76
|
+
const isNpm2Yarn = (node) => node.type === 'code' && node.meta === 'npm2yarn';
|
|
77
|
+
function createImportNode() {
|
|
78
|
+
return {
|
|
79
|
+
type: 'mdxjsEsm',
|
|
80
|
+
value: "import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'",
|
|
81
|
+
data: {
|
|
82
|
+
estree: {
|
|
83
|
+
type: 'Program',
|
|
84
|
+
body: [
|
|
85
|
+
{
|
|
86
|
+
type: 'ImportDeclaration',
|
|
87
|
+
specifiers: [
|
|
88
|
+
{
|
|
89
|
+
type: 'ImportDefaultSpecifier',
|
|
90
|
+
local: { type: 'Identifier', name: 'Tabs' },
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
source: {
|
|
94
|
+
type: 'Literal',
|
|
95
|
+
value: '@theme/Tabs',
|
|
96
|
+
raw: "'@theme/Tabs'",
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
type: 'ImportDeclaration',
|
|
101
|
+
specifiers: [
|
|
102
|
+
{
|
|
103
|
+
type: 'ImportDefaultSpecifier',
|
|
104
|
+
local: { type: 'Identifier', name: 'TabItem' },
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
source: {
|
|
108
|
+
type: 'Literal',
|
|
109
|
+
value: '@theme/TabItem',
|
|
110
|
+
raw: "'@theme/TabItem'",
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
sourceType: 'module',
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
53
119
|
const plugin = (options = {}) => {
|
|
120
|
+
// @ts-expect-error: todo temporary
|
|
54
121
|
const { sync = false, converters = ['yarn', 'pnpm'] } = options;
|
|
55
122
|
return (root) => {
|
|
56
123
|
let transformed = false;
|
|
57
124
|
let alreadyImported = false;
|
|
58
125
|
(0, unist_util_visit_1.default)(root, (node) => {
|
|
59
|
-
if (
|
|
126
|
+
if (isTabsImport(node)) {
|
|
60
127
|
alreadyImported = true;
|
|
61
128
|
}
|
|
62
129
|
if (isParent(node)) {
|
|
63
130
|
let index = 0;
|
|
64
131
|
while (index < node.children.length) {
|
|
65
132
|
const child = node.children[index];
|
|
66
|
-
if (
|
|
133
|
+
if (isNpm2Yarn(child)) {
|
|
67
134
|
const result = transformNode(child, sync, converters);
|
|
68
135
|
node.children.splice(index, 1, ...result);
|
|
69
136
|
index += result.length;
|
|
@@ -76,7 +143,7 @@ const plugin = (options = {}) => {
|
|
|
76
143
|
}
|
|
77
144
|
});
|
|
78
145
|
if (transformed && !alreadyImported) {
|
|
79
|
-
root.children.unshift(
|
|
146
|
+
root.children.unshift(createImportNode());
|
|
80
147
|
}
|
|
81
148
|
};
|
|
82
149
|
};
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,gFAAqC;AACrC,sEAAoC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,gFAAqC;AACrC,sEAAoC;AA0BpC,SAAS,eAAe,CACtB,aAAqB,EACrB,cAAwC;IAExC,OAAO;QACL,IAAI,EAAE,iBAAiB;QACvB,IAAI,EAAE,aAAa;QACnB,KAAK,EAAE,cAAc;KACtB,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,EACrB,IAAI,EACJ,IAAI,EACJ,KAAK,EACL,KAAK,GAMN;IACC,OAAO;QACL,IAAI,EAAE,mBAAmB;QACzB,IAAI,EAAE,SAAS;QACf,UAAU,EAAE;YACV,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC;YAC/B,KAAK,IAAI,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC;SACzC,CAAC,MAAM,CAAC,CAAC,IAAI,EAA2B,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC1D,QAAQ,EAAE;YACR;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BAAS,MAAM,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/remark-plugin-npm2yarn",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
4
|
"description": "Remark plugin for converting npm commands to Yarn commands as tabs.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -17,18 +17,20 @@
|
|
|
17
17
|
},
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"dependencies": {
|
|
20
|
+
"mdast-util-mdx": "^2.0.0",
|
|
20
21
|
"npm-to-yarn": "^2.0.0",
|
|
21
|
-
"tslib": "^2.
|
|
22
|
+
"tslib": "^2.5.0",
|
|
23
|
+
"unified": "^10.1.2",
|
|
22
24
|
"unist-util-visit": "^2.0.3"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
25
27
|
"@types/mdast": "^3.0.10",
|
|
26
|
-
"remark": "^
|
|
27
|
-
"remark-mdx": "^1.
|
|
28
|
+
"remark": "^14.0.2",
|
|
29
|
+
"remark-mdx": "^2.1.5",
|
|
28
30
|
"to-vfile": "^6.1.0"
|
|
29
31
|
},
|
|
30
32
|
"engines": {
|
|
31
33
|
"node": ">=16.14"
|
|
32
34
|
},
|
|
33
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "7327f7ff880ed97ad7855744e59c9c55d467a950"
|
|
34
36
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,95 +7,186 @@
|
|
|
7
7
|
|
|
8
8
|
import visit from 'unist-util-visit';
|
|
9
9
|
import npmToYarn from 'npm-to-yarn';
|
|
10
|
-
import type {Code,
|
|
11
|
-
|
|
10
|
+
import type {Code, Literal} from 'mdast';
|
|
11
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
12
|
+
import type {MdxJsxFlowElement, MdxJsxAttribute} from 'mdast-util-mdx';
|
|
12
13
|
import type {Node, Parent} from 'unist';
|
|
14
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
15
|
+
import type {Transformer} from 'unified';
|
|
16
|
+
|
|
17
|
+
// TODO as of April 2023, no way to import/re-export this ESM type easily :/
|
|
18
|
+
// This might change soon, likely after TS 5.2
|
|
19
|
+
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
|
|
20
|
+
// import type {Plugin} from 'unified';
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
22
|
+
type Plugin<T> = any; // TODO fix this asap
|
|
23
|
+
|
|
24
|
+
type KnownConverter = 'yarn' | 'pnpm';
|
|
13
25
|
|
|
14
26
|
type CustomConverter = [name: string, cb: (npmCode: string) => string];
|
|
15
27
|
|
|
28
|
+
type Converter = CustomConverter | KnownConverter;
|
|
29
|
+
|
|
16
30
|
type PluginOptions = {
|
|
17
31
|
sync?: boolean;
|
|
18
|
-
converters?:
|
|
32
|
+
converters?: Converter[];
|
|
19
33
|
};
|
|
20
34
|
|
|
21
|
-
function
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
function createAttribute(
|
|
36
|
+
attributeName: string,
|
|
37
|
+
attributeValue: MdxJsxAttribute['value'],
|
|
38
|
+
): MdxJsxAttribute {
|
|
39
|
+
return {
|
|
40
|
+
type: 'mdxJsxAttribute',
|
|
41
|
+
name: attributeName,
|
|
42
|
+
value: attributeValue,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createTabItem({
|
|
47
|
+
code,
|
|
48
|
+
node,
|
|
49
|
+
value,
|
|
50
|
+
label,
|
|
51
|
+
}: {
|
|
52
|
+
code: string;
|
|
53
|
+
node: Code;
|
|
54
|
+
value: string;
|
|
55
|
+
label?: string;
|
|
56
|
+
}): MdxJsxFlowElement {
|
|
57
|
+
return {
|
|
58
|
+
type: 'mdxJsxFlowElement',
|
|
59
|
+
name: 'TabItem',
|
|
60
|
+
attributes: [
|
|
61
|
+
createAttribute('value', value),
|
|
62
|
+
label && createAttribute('label', label),
|
|
63
|
+
].filter((attr): attr is MdxJsxAttribute => Boolean(attr)),
|
|
64
|
+
children: [
|
|
65
|
+
{
|
|
66
|
+
type: node.type,
|
|
67
|
+
lang: node.lang,
|
|
68
|
+
value: code,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
};
|
|
42
72
|
}
|
|
43
73
|
|
|
44
74
|
const transformNode = (
|
|
45
75
|
node: Code,
|
|
46
76
|
isSync: boolean,
|
|
47
|
-
converters:
|
|
77
|
+
converters: Converter[],
|
|
48
78
|
) => {
|
|
49
|
-
const groupIdProp = isSync
|
|
79
|
+
const groupIdProp = isSync
|
|
80
|
+
? {
|
|
81
|
+
type: 'mdxJsxAttribute',
|
|
82
|
+
name: 'groupId',
|
|
83
|
+
value: 'npm2yarn',
|
|
84
|
+
}
|
|
85
|
+
: undefined;
|
|
50
86
|
const npmCode = node.value;
|
|
87
|
+
|
|
88
|
+
function createConvertedTabItem(converter: Converter) {
|
|
89
|
+
if (typeof converter === 'string') {
|
|
90
|
+
return createTabItem({
|
|
91
|
+
code: npmToYarn(npmCode, converter),
|
|
92
|
+
node,
|
|
93
|
+
value: converter,
|
|
94
|
+
label: converter === 'yarn' ? 'Yarn' : converter,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const [converterName, converterFn] = converter;
|
|
98
|
+
return createTabItem({
|
|
99
|
+
code: converterFn(npmCode),
|
|
100
|
+
node,
|
|
101
|
+
value: converterName,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
51
105
|
return [
|
|
52
106
|
{
|
|
53
|
-
type: '
|
|
54
|
-
|
|
107
|
+
type: 'mdxJsxFlowElement',
|
|
108
|
+
name: 'Tabs',
|
|
109
|
+
attributes: [groupIdProp].filter(Boolean),
|
|
110
|
+
children: [
|
|
111
|
+
createTabItem({code: npmCode, node, value: 'npm'}),
|
|
112
|
+
...converters.flatMap(createConvertedTabItem),
|
|
113
|
+
],
|
|
55
114
|
},
|
|
56
|
-
|
|
57
|
-
...converters.flatMap((converter) =>
|
|
58
|
-
typeof converter === 'string'
|
|
59
|
-
? createTabItem(
|
|
60
|
-
npmToYarn(npmCode, converter),
|
|
61
|
-
node,
|
|
62
|
-
converter,
|
|
63
|
-
converter === 'yarn' ? 'Yarn' : converter,
|
|
64
|
-
)
|
|
65
|
-
: createTabItem(converter[1](npmCode), node, converter[0]),
|
|
66
|
-
),
|
|
67
|
-
{
|
|
68
|
-
type: 'jsx',
|
|
69
|
-
value: '</Tabs>',
|
|
70
|
-
},
|
|
71
|
-
] as Content[];
|
|
115
|
+
] as any[];
|
|
72
116
|
};
|
|
73
117
|
|
|
74
|
-
const
|
|
118
|
+
const isMdxEsmLiteral = (node: Node): node is Literal =>
|
|
119
|
+
node.type === 'mdxjsEsm';
|
|
120
|
+
// TODO legacy approximation, good-enough for now but not 100% accurate
|
|
121
|
+
const isTabsImport = (node: Node): boolean =>
|
|
122
|
+
isMdxEsmLiteral(node) && node.value.includes('@theme/Tabs');
|
|
123
|
+
|
|
75
124
|
const isParent = (node: Node): node is Parent =>
|
|
76
125
|
Array.isArray((node as Parent).children);
|
|
77
|
-
const
|
|
126
|
+
const isNpm2Yarn = (node: Node): node is Code =>
|
|
78
127
|
node.type === 'code' && (node as Code).meta === 'npm2yarn';
|
|
79
|
-
const nodeForImport: Literal = {
|
|
80
|
-
type: 'import',
|
|
81
|
-
value:
|
|
82
|
-
"import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';",
|
|
83
|
-
};
|
|
84
128
|
|
|
85
|
-
|
|
129
|
+
function createImportNode() {
|
|
130
|
+
return {
|
|
131
|
+
type: 'mdxjsEsm',
|
|
132
|
+
value:
|
|
133
|
+
"import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'",
|
|
134
|
+
data: {
|
|
135
|
+
estree: {
|
|
136
|
+
type: 'Program',
|
|
137
|
+
body: [
|
|
138
|
+
{
|
|
139
|
+
type: 'ImportDeclaration',
|
|
140
|
+
specifiers: [
|
|
141
|
+
{
|
|
142
|
+
type: 'ImportDefaultSpecifier',
|
|
143
|
+
local: {type: 'Identifier', name: 'Tabs'},
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
source: {
|
|
147
|
+
type: 'Literal',
|
|
148
|
+
value: '@theme/Tabs',
|
|
149
|
+
raw: "'@theme/Tabs'",
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
type: 'ImportDeclaration',
|
|
154
|
+
specifiers: [
|
|
155
|
+
{
|
|
156
|
+
type: 'ImportDefaultSpecifier',
|
|
157
|
+
local: {type: 'Identifier', name: 'TabItem'},
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
source: {
|
|
161
|
+
type: 'Literal',
|
|
162
|
+
value: '@theme/TabItem',
|
|
163
|
+
raw: "'@theme/TabItem'",
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
sourceType: 'module',
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const plugin: Plugin<[PluginOptions?]> = (options = {}): Transformer => {
|
|
174
|
+
// @ts-expect-error: todo temporary
|
|
86
175
|
const {sync = false, converters = ['yarn', 'pnpm']} = options;
|
|
87
176
|
return (root) => {
|
|
88
|
-
let transformed = false
|
|
89
|
-
let alreadyImported = false
|
|
177
|
+
let transformed = false;
|
|
178
|
+
let alreadyImported = false;
|
|
179
|
+
|
|
90
180
|
visit(root, (node: Node) => {
|
|
91
|
-
if (
|
|
181
|
+
if (isTabsImport(node)) {
|
|
92
182
|
alreadyImported = true;
|
|
93
183
|
}
|
|
184
|
+
|
|
94
185
|
if (isParent(node)) {
|
|
95
186
|
let index = 0;
|
|
96
187
|
while (index < node.children.length) {
|
|
97
188
|
const child = node.children[index]!;
|
|
98
|
-
if (
|
|
189
|
+
if (isNpm2Yarn(child)) {
|
|
99
190
|
const result = transformNode(child, sync, converters);
|
|
100
191
|
node.children.splice(index, 1, ...result);
|
|
101
192
|
index += result.length;
|
|
@@ -106,8 +197,9 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
|
|
|
106
197
|
}
|
|
107
198
|
}
|
|
108
199
|
});
|
|
200
|
+
|
|
109
201
|
if (transformed && !alreadyImported) {
|
|
110
|
-
(root as Parent).children.unshift(
|
|
202
|
+
(root as Parent).children.unshift(createImportNode());
|
|
111
203
|
}
|
|
112
204
|
};
|
|
113
205
|
};
|