Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.4.0 #40

Merged
merged 39 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a0ca96a
fix: reexports should be, well, reexported
Dschungelabenteuer Nov 19, 2023
147b4a6
fix: jsdoc typo
Dschungelabenteuer Nov 19, 2023
2b1145f
fix: multiline import statements (#35)
Dschungelabenteuer Nov 19, 2023
89769d9
pre-refacto: just to better see amount of eliminated code after shari…
Dschungelabenteuer Nov 28, 2023
fcf46dc
mid-refacto: right before mutating runCase so that it accepts multipl…
Dschungelabenteuer Dec 3, 2023
01e5791
next
Dschungelabenteuer Dec 9, 2023
3f6af31
next
Dschungelabenteuer Dec 11, 2023
1164ae2
next
Dschungelabenteuer Dec 14, 2023
4b26082
next
Dschungelabenteuer Dec 18, 2023
dbf6265
next
Dschungelabenteuer Dec 19, 2023
b17f5e7
next
Dschungelabenteuer Dec 24, 2023
60cee15
next
Dschungelabenteuer Jan 5, 2024
1cea22b
next'
Dschungelabenteuer Jan 8, 2024
228b438
next
Dschungelabenteuer Feb 18, 2024
915c0c6
chore: ignore packages in changeset
Dschungelabenteuer Feb 19, 2024
ecae4ff
fix: restore issue-28 and remove what looks like probable dead code, …
Dschungelabenteuer Feb 19, 2024
48949c0
next
Dschungelabenteuer Mar 5, 2024
1b19a72
fix: syntaxes examples paths
Mar 5, 2024
00df864
feat: next
Dschungelabenteuer Mar 10, 2024
7397212
next
Mar 11, 2024
6fbef13
next+storybook
Dschungelabenteuer Mar 17, 2024
ab2bd3c
next
Mar 18, 2024
8d38ed4
next
Dschungelabenteuer Mar 26, 2024
b082c2e
next
Mar 26, 2024
24f2c36
next
Dschungelabenteuer Mar 29, 2024
13b0b41
feat: next
Dschungelabenteuer Mar 30, 2024
7bd921d
update github templates
Dschungelabenteuer Mar 30, 2024
0d83b35
update READMEs
Dschungelabenteuer Mar 30, 2024
c40ade7
feat: glob patterns
Mar 30, 2024
c770f3d
fix: better whitespaces (#38 #35)
Mar 30, 2024
9dde6cd
chore: prepare release
Mar 30, 2024
9c55228
chore: upgrade dependencies
Mar 30, 2024
2b59fa3
chore: upgrade dependencies
Mar 30, 2024
7169a50
fix: fix ESLint issues
Mar 30, 2024
491d773
chore: update node version used in github actions
Mar 30, 2024
c2d9e8c
chore: update github actions
Mar 30, 2024
36a9151
chore: update github actions
Mar 30, 2024
79e45b8
chore: update github actions
Mar 30, 2024
c15c9e1
chore: update github actions
Mar 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
next
  • Loading branch information
Dschungelabenteuer committed Dec 9, 2023
commit 01e579156c254bdf302f27fda6c5e837462d351c
25 changes: 19 additions & 6 deletions packages/core/src/analyze-import.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-continue */
import type MagicString from 'magic-string';
import type { ResolveFn } from 'vite';
import { normalizePath } from 'vite';
Expand Down Expand Up @@ -76,7 +77,7 @@ async function getImportsMap(
*/
const formatImportReplacement = ({ name, alias, originalName, importDefault }: ImportInput) => {
if (importDefault) return `default as ${alias ?? originalName ?? name}`;
if (originalName) return `${originalName}${alias ? ` as ${alias}` : ` as ${name}`}`;
if (originalName) return `${originalName} as ${alias ?? name}`;
return `${name}${alias ? ` as ${alias}` : ''}`;
};

Expand Down Expand Up @@ -148,18 +149,29 @@ const resolveImportedCircularEntities = async (
resolver: ResolveFn,
path: string,
) => {
const output: ImportStatement[] = [];
const entityMap = new Map<string, string[]>();
const originalEntry = entries.get(path)!;
for (const entity of imported) {
const { originalName } = entity;
const { originalName, alias } = entity;
if (originalName && originalEntry.exports.has(originalName)) {
const originalImport = originalEntry.exports.get(originalName)!;
const resolvedPath = await resolver(originalImport.path, path);
const resolvedImports = methods.formatImportReplacement(entity);
output.push(`import { ${resolvedImports} } from '${resolvedPath}'`);
if (!resolvedPath) continue;

const resolvedImports = methods.formatImportReplacement({
...originalImport,
name: originalName,
alias,
});

entityMap.set(resolvedPath, [...(entityMap.get(resolvedPath) ?? []), resolvedImports]);
}
}
return output;

const imports = [...entityMap.entries()];
return imports.map(
([p, ents]) => `import { ${ents.join(', ')} } from '${p}'`,
) as ImportStatement[];
};

/**
Expand Down Expand Up @@ -187,6 +199,7 @@ const analyzeImportStatement = async (
if (isWildCardImport) return;

const imports = methods.getImportedEntryExports(code, startPosition, endPosition);

const imported = await methods.getImportsMap(entryExports, entryPath, imports, resolver);
const replacement = await methods.getImportReplacements(imported, entryPath, entries, resolver);
src.overwrite(startPosition, endPosition + 1, `${replacement.join(';\n')};`);
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/__mocks__/unit/entry-b/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SomeName = 'some-name';
2 changes: 1 addition & 1 deletion packages/core/tests/__mocks__/unit/entry-c/Reexport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {
A_MODULE_E,
A_MODULE_A as AA,
} from '@mocks/entry-a';
} from '@mocks/unit/entry-a';
22 changes: 0 additions & 22 deletions packages/core/tests/analyze-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
MOCKS_FOLDER_UNIT,
resolveUnitEntry,
STUB_PATH,
STUB_SOURCE,
} from './utils';
import type { EntryData, ImportInput, PluginEntries, TargetImports } from '../src/types';

Expand Down Expand Up @@ -303,27 +302,6 @@ describe('resolveImportedEntities', () => {
});
});

describe('resolveImportedCircularEntities', () => {
beforeEach(() => {
vi.restoreAllMocks();
});

const entryPath = 'entryB';
const entryData: EntryData = {
exports: new Map([[MOCK_IMPORT_INPUT.name, MOCK_IMPORT_INPUT]]),
source: STUB_SOURCE,
updatedSource: STUB_SOURCE,
};

const imported: ImportInput[] = [MOCK_IMPORT_INPUT];
const entries: PluginEntries = new Map([[entryPath, entryData]]);

it('should call "formatImportReplacement" with resolved entry data', async () => {
await ImportAnalyzer.resolveImportedCircularEntities(imported, entries, resolver, entryPath);
expect(ImportAnalyzer.formatImportReplacement).toHaveBeenCalledTimes(imported.length);
});
});

describe('analyzeImportStatement', () => {
it('should ignore wildcard import statements', async () => {
vi.restoreAllMocks();
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "Some default export"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { DefaultImport } from '@test-cases/default-export/with-default-direct-reexport';
export { DefaultImportDupe } from '@test-cases/default-export/with-default-direct-reexport';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { DefaultImport } from '@test-cases/default-export/with-default-import-reexport';
export { DefaultImportDupe } from '@test-cases/default-export/with-default-import-reexport';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { DefaultImport } from '@test-cases/default-export/with-destructured-default-import';
export { DefaultImportDupe } from '@test-cases/default-export/with-destructured-default-import';
3 changes: 3 additions & 0 deletions packages/core/tests/cases/cross-entries/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testCrossEntriesImportsExports } from './setup';

testCrossEntriesImportsExports();
2 changes: 2 additions & 0 deletions packages/core/tests/cases/cross-entries/mixed-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { MixedDefault, MixedImportNamed } from '@test-cases/mixed-imports/mixed';
export default "Some default export";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { NamedExportOne, NamedExportTwo } from '@test-cases/named-export-direct-reexport/multiple';
export { NamedExportOneDupe, NamedExportTwoDupe } from '@test-cases/named-export-direct-reexport/multiple';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { NamedExportOne } from '@test-cases/named-export-direct-reexport/single';
export { NamedExportOneDupe } from '@test-cases/named-export-direct-reexport/single';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { NamedExportOne, AliasedNamedExportTwo } from '@test-cases/named-export-import-reexport-alias-via-export/multiple';
export { NamedExportOneDupe } from '@test-cases/named-export-import-reexport-alias-via-export/multiple';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AliasedNamedExportOne } from '@test-cases/named-export-import-reexport-alias-via-export/single';
export { NamedExportOneDupe } from '@test-cases/named-export-import-reexport-alias-via-export/single';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AliasedNamedExportOne, AliasedNamedExportTwo } from '@test-cases/named-export-import-reexport-alias-via-import/multiple';
export { NamedExportOneDupe } from '@test-cases/named-export-import-reexport-alias-via-import/multiple';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AliasedNamedExportOne } from '@test-cases/named-export-import-reexport-alias-via-import/single';
export { NamedExportOneDupe } from '@test-cases/named-export-import-reexport-alias-via-import/single';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { NamedExportOne, NamedExportTwo } from '@test-cases/named-export-import-reexport/multiple';
export { NamedExportOneDupe, NamedExportTwoDupe } from '@test-cases/named-export-import-reexport/multiple';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { NamedExportOne } from '@test-cases/named-export-import-reexport/single';
export { NamedExportOneDupe } from '@test-cases/named-export-import-reexport/single';
86 changes: 86 additions & 0 deletions packages/core/tests/cases/cross-entries/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe } from 'vitest';
import { createCaseTarget } from '../../utils';

import {
testNamedExportDirectReexportSingle,
testNamedExportDirectReexportMultiple,
} from '../named-export-direct-reexport/setup';

import {
testNamedExportImportReexportSingle,
testNamedExportImportReexportMultiple,
} from '../named-export-import-reexport/setup';

import {
testNamedExportImportReexportAliasViaExportSingle,
testNamedExportImportReexportAliasViaExportMultiple,
} from '../named-export-import-reexport-alias-via-export/setup';

import {
testNamedExportImportReexportAliasViaImportSingle,
testNamedExportImportReexportAliasViaImportMultiple,
} from '../named-export-import-reexport-alias-via-import/setup';

import {
testMixedImportsInTarget,
testMixedImportsOfTarget
} from '../mixed-imports/setup';

import {
testHasDefaultExport,
testWithDefaultDirectReexport,
testWithDefaultImportReexport,
testWithDestructuredDefaultImport,
} from '../default-export/setup'

const casePath = '@test-cases/cross-entries';

/**
* The following tests suppose some file imports data from a `second` target which
* exposes some code re-exported from a `first` target. When testing cross-entries
* imports/exports, we also want to make sure all supported syntaxes work properly.
* There are a lot of possible cases and so as to o reduce code duplication, let's
* use other syntax-specific cases as a `first` target.
*/
export function testCrossEntriesImportsExports() {
describe('Cross-entries imports/exports', () => {
describe('direct re-export', async () => {
const baseCasePath = `${casePath}/named-export-direct-reexport`;
testNamedExportDirectReexportSingle(await createCaseTarget(`${baseCasePath}-single`));
testNamedExportDirectReexportMultiple(await createCaseTarget(`${baseCasePath}-multiple`));
});

describe('import then re-export', async () => {
const baseCasePath = `${casePath}/named-export-import-reexport`;
testNamedExportImportReexportSingle(await createCaseTarget(`${baseCasePath}-single`));
testNamedExportImportReexportMultiple(await createCaseTarget(`${baseCasePath}-multiple`));
});

describe('import then re-export an alias set via export', async () => {
const baseCasePath = `${casePath}/named-export-import-reexport-alias-via-export`;
testNamedExportImportReexportAliasViaExportSingle(await createCaseTarget(`${baseCasePath}-single`));
testNamedExportImportReexportAliasViaExportMultiple(await createCaseTarget(`${baseCasePath}-multiple`));
});

describe('import then re-export an alias set via import', async () => {
const baseCasePath = `${casePath}/named-export-import-reexport-alias-via-import`;
testNamedExportImportReexportAliasViaImportSingle(await createCaseTarget(`${baseCasePath}-single`));
testNamedExportImportReexportAliasViaImportMultiple(await createCaseTarget(`${baseCasePath}-multiple`));
});

describe('mixed imports', async () => {
const baseCasePath = `${casePath}/mixed-imports`;
testMixedImportsInTarget(await createCaseTarget(`${baseCasePath}`));
testMixedImportsOfTarget(await createCaseTarget(`${baseCasePath}`));
});

describe('default export', async () => {
const baseCasePath = `${casePath}/default-export`;
testHasDefaultExport(await createCaseTarget(`${baseCasePath}-has-default-export`))
testWithDefaultDirectReexport(await createCaseTarget(`${baseCasePath}-with-default-direct-reexport`))
testWithDefaultImportReexport(await createCaseTarget(`${baseCasePath}-with-default-import-reexport`))
testWithDestructuredDefaultImport(await createCaseTarget(`${baseCasePath}-with-destructured-default-import`))
})
});
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import MixedDefault, { MixedImportNamed } from '../../__mocks__/modules/mixed-import';
export { MixedDefault, MixedImportNamed };
export default "Default export from target";

/** Below content should not be removed from the transformed target. */
import { ConsumedExport } from '@test-modules/consumed-export';
export const ExportDefinedFromTarget = 'ExportDefinedFromTarget';
const CodeDefinedFromTarget = `CodeDefinedFromTarget: ${ConsumedExport}`;
console.info('This is being printed from target, which means target was requested', CodeDefinedFromTarget);
export default "Default export from target";
75 changes: 2 additions & 73 deletions packages/core/tests/cases/default-export/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,3 @@
import { describe, it } from 'vitest';
import { resolvePath, resolveModule, testCase, getPathTypeDescribeLabel } from '../../utils';
import { testDefaultExport } from './setup';

const casePath = '@test-cases/default-export';

describe('Default imports/exports', () => {
describe('re-exporting default imports from target', async () => {
const testTarget = async (targetName: string) => {
const describeLabel = getPathTypeDescribeLabel(targetName);

describe(describeLabel, () => {
it(`should work when target re-exports a simple default import`, async () => {
const resolved = await resolveModule('default-export');
const target = await resolvePath(`${casePath}/${targetName}`);
const input = `import { DefaultImport } from '${target}';\n`;
const output = `import { default as DefaultImport } from '${resolved}';\n`;
await testCase(target, input, output, 3);
});

it('should work when target re-exports several aliases of a default import', async () => {
const resolved = await resolveModule('default-export');
const target = await resolvePath(`${casePath}/${targetName}`);
const input = `import { DefaultImport, DefaultImportDupe } from '${target}';\n`;
const output = `import { default as DefaultImport, default as DefaultImportDupe } from '${resolved}';\n`;
await testCase(target, input, output, 3);
});

it('should work when target re-exports an aliased default import', async () => {
// e.g. `import { default as Alias } from './some-module'`
const resolved = await resolveModule('default-export');
const target = await resolvePath(`${casePath}/${targetName}`);
const input = `import { YetAnotherDefaultImport } from '${target}';\n`;
const output = `import { default as YetAnotherDefaultImport } from '${resolved}';\n`;
await testCase(target, input, output, 3);
});
});
};

await testTarget('relative');
await testTarget('alias');
});

describe('re-exporting default export of target', async () => {
const testTarget = async (targetName: string) => {
const describeLabel = getPathTypeDescribeLabel(targetName);

describe(describeLabel, () => {
it(`should work when simply importing target's default export`, async () => {
const target = await resolvePath(`${casePath}/${targetName}`);
const input = `import DefaultImport from '${target}';\n`;
const output = `import { default as DefaultImport } from '${target}';\n`;
await testCase(target, input, output, 3);
});

it(`should work when importing target's default export with explicit default keyword`, async () => {
const target = await resolvePath(`${casePath}/${targetName}`);
const input = `import { default as Something } from '${target}';\n`;
const output = `import { default as Something } from '${target}';\n`;
await testCase(target, input, output, 3);
});

it(`should work when importing target's default export multiple times`, async () => {
const target = await resolvePath(`${casePath}/${targetName}`);
const input = `import { default as Something, default as SomethingElse } from '${target}';\n`;
const output = `import { default as Something, default as SomethingElse } from '${target}';\n`;
await testCase(target, input, output, 3);
});
});
};

await testTarget('relative');
await testTarget('alias');
});
});
testDefaultExport()
13 changes: 0 additions & 13 deletions packages/core/tests/cases/default-export/relative.ts

This file was deleted.

Loading