mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 05:12:31 +02:00
fix(v2): avoid duplicated imports in npm2yarn plugin (#4964)
* fix(v2): avoid duplicated imports in npm2yarn plugin * Better testing
This commit is contained in:
parent
c8b9061f6c
commit
ace285b3b5
5 changed files with 91 additions and 1 deletions
|
@ -1,5 +1,61 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`npm2yarn plugin test: already imported tabs components above are not re-imported 1`] = `
|
||||||
|
"import Tabs from '@theme/Tabs';
|
||||||
|
|
||||||
|
import TabItem from '@theme/TabItem';
|
||||||
|
|
||||||
|
<Tabs defaultValue=\\"npm\\" values={[
|
||||||
|
{ label: 'npm', value: 'npm', },
|
||||||
|
{ label: 'Yarn', value: 'yarn', },
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<TabItem value=\\"npm\\">
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
$ npm install --global docusaurus
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value=\\"yarn\\">
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
$ yarn add --global docusaurus
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`npm2yarn plugin test: already imported tabs components below are not re-imported 1`] = `
|
||||||
|
"<Tabs defaultValue=\\"npm\\" values={[
|
||||||
|
{ label: 'npm', value: 'npm', },
|
||||||
|
{ label: 'Yarn', value: 'yarn', },
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<TabItem value=\\"npm\\">
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
$ npm install --global docusaurus
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value=\\"yarn\\">
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
$ yarn add --global docusaurus
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
import Tabs from '@theme/Tabs';
|
||||||
|
|
||||||
|
import TabItem from '@theme/TabItem';
|
||||||
|
"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`npm2yarn plugin test: installation file 1`] = `
|
exports[`npm2yarn plugin test: installation file 1`] = `
|
||||||
"import Tabs from '@theme/Tabs';
|
"import Tabs from '@theme/Tabs';
|
||||||
import TabItem from '@theme/TabItem';
|
import TabItem from '@theme/TabItem';
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
import Tabs from '@theme/Tabs';
|
||||||
|
|
||||||
|
import TabItem from '@theme/TabItem';
|
||||||
|
|
||||||
|
```bash npm2yarn
|
||||||
|
$ npm install --global docusaurus
|
||||||
|
```
|
|
@ -0,0 +1,7 @@
|
||||||
|
```bash npm2yarn
|
||||||
|
$ npm install --global docusaurus
|
||||||
|
```
|
||||||
|
|
||||||
|
import Tabs from '@theme/Tabs';
|
||||||
|
|
||||||
|
import TabItem from '@theme/TabItem';
|
|
@ -50,4 +50,20 @@ describe('npm2yarn plugin', () => {
|
||||||
|
|
||||||
expect(result).toMatchSnapshot();
|
expect(result).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('test: already imported tabs components above are not re-imported', async () => {
|
||||||
|
const result = await processFixture('import-tabs-above', {
|
||||||
|
staticDir,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('test: already imported tabs components below are not re-imported', async () => {
|
||||||
|
const result = await processFixture('import-tabs-below', {
|
||||||
|
staticDir,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -57,7 +57,11 @@ const nodeForImport = {
|
||||||
module.exports = (options = {}) => {
|
module.exports = (options = {}) => {
|
||||||
const {sync = false} = options;
|
const {sync = false} = options;
|
||||||
let transformed = false;
|
let transformed = false;
|
||||||
|
let alreadyImported = false;
|
||||||
const transformer = (node) => {
|
const transformer = (node) => {
|
||||||
|
if (node.type === 'import' && node.value.includes('@theme/Tabs')) {
|
||||||
|
alreadyImported = true;
|
||||||
|
}
|
||||||
if (matchNode(node)) {
|
if (matchNode(node)) {
|
||||||
transformed = true;
|
transformed = true;
|
||||||
return transformNode(node, sync);
|
return transformNode(node, sync);
|
||||||
|
@ -74,7 +78,7 @@ module.exports = (options = {}) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node.type === 'root' && transformed) {
|
if (node.type === 'root' && transformed && !alreadyImported) {
|
||||||
node.children.unshift(nodeForImport);
|
node.children.unshift(nodeForImport);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue