Skip to content

Commit

Permalink
fix: fixed a cross-entry wildcart import issue (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dschungelabenteuer authored Sep 22, 2024
1 parent cb7ecfe commit 20b7981
Show file tree
Hide file tree
Showing 37 changed files with 222 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/wicked-dingos-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"vite-plugin-entry-shaking-debugger": patch
"vite-plugin-entry-shaking": patch
---

Fixed cross-entry wildcard import issue
1 change: 1 addition & 0 deletions examples/_template_/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/_template_/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pathToLib = resolve(__dirname, './src/lib');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [pathToLib],
Expand Down
1 change: 1 addition & 0 deletions examples/async-import/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/async-import/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pathToLib = resolve(__dirname, './src/lib');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [pathToLib],
Expand Down
1 change: 1 addition & 0 deletions examples/code-defined-within-entry/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/code-defined-within-entry/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pathToMaxDepth = resolve(__dirname, './src/max-depth');
const pathToWithImport = resolve(__dirname, './src/with-import');
const pathToWithoutImport = resolve(__dirname, './src/without-import');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [pathToMaxDepth, pathToWithImport, pathToWithoutImport],
Expand Down
1 change: 1 addition & 0 deletions examples/glob-patterns/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/glob-patterns/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const __dirname = dirname(__filename);
const pathToLib = resolve(__dirname, './src/lib');
const pathToEntries = resolve(__dirname, './src/entries');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [
Expand Down
1 change: 1 addition & 0 deletions examples/issue-28/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/issue-28/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const pathToFirst = resolve(__dirname, 'src/first/');
const pathToSecond = resolve(__dirname, 'src/second/');
const pathToThird = resolve(__dirname, 'src/third/');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: ['@first/', pathToSecond, pathToThird],
Expand Down
5 changes: 5 additions & 0 deletions examples/issue-53/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import vuePreset from '@yungezeit/eslint-vue';

export default [
...vuePreset,
];
13 changes: 13 additions & 0 deletions examples/issue-53/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>vite-plugin-entry-shaking example</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions examples/issue-53/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "vite-plugin-entry-shaking-example-issue-53",
"type": "module",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.5.7"
},
"devDependencies": {
"@types/node": "^22.5.5",
"@vitejs/plugin-vue": "^5.1.4",
"@yungezeit/eslint-vue": "^0.0.12",
"sass": "^1.79.3",
"typescript": "^5.6.2",
"vite": "^5.4.7",
"vite-plugin-entry-shaking": "workspace:*",
"vue-tsc": "^2.1.6"
}
}
6 changes: 6 additions & 0 deletions examples/issue-53/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script setup lang="ts">
</script>

<template>
<div>Example</div>
</template>
3 changes: 3 additions & 0 deletions examples/issue-53/src/lib/hello/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function helloWorld() {
console.info('Hello, world!');
}
1 change: 1 addition & 0 deletions examples/issue-53/src/lib/hello/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { helloWorld } from './hello';
3 changes: 3 additions & 0 deletions examples/issue-53/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as Hello from './hello';

export { Hello };
11 changes: 11 additions & 0 deletions examples/issue-53/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createApp } from 'vue';
import App from './App.vue';

import { Hello } from './lib';

console.info(Hello);

Hello.helloWorld();

const app = createApp(App);
app.mount('#app');
1 change: 1 addition & 0 deletions examples/issue-53/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
28 changes: 28 additions & 0 deletions examples/issue-53/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"paths": {
"@lib/*": ["./src/lib/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions examples/issue-53/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
25 changes: 25 additions & 0 deletions examples/issue-53/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineConfig } from 'vite';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import vue from '@vitejs/plugin-vue';
import EntryShakingPlugin from 'vite-plugin-entry-shaking';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pathToLib = resolve(__dirname, './src/lib');
const pathToHello = resolve(__dirname, './src/lib/hello');

export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [pathToLib, pathToHello],
debug: true,
}),
vue(),
],
resolve: {
alias: {
'@lib': pathToLib,
},
},
}));
1 change: 1 addition & 0 deletions examples/jsx/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
1 change: 1 addition & 0 deletions examples/simple/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pathToLib = resolve(__dirname, './src/lib');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [pathToLib],
Expand Down
1 change: 1 addition & 0 deletions examples/syntaxes/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/syntaxes/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const rootDir = resolve(__dirname, '../..');
const pathToTests = resolve(__dirname, './src/data/');
const pathToTestCases = resolve(pathToTests, 'cases');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: targets.map((target) => resolve(rootDir, target)),
Expand Down
1 change: 1 addition & 0 deletions examples/vue-sfc/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"strictNullChecks": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
Expand Down
2 changes: 1 addition & 1 deletion examples/vue-sfc/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pathToLib = resolve(__dirname, './src/lib');

export default defineConfig(async () => ({
export default defineConfig(() => ({
plugins: [
EntryShakingPlugin({
targets: [pathToLib],
Expand Down
18 changes: 13 additions & 5 deletions packages/core/src/analyze-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ async function getImportReplacements(
for (const [importedPath, importedEntities] of imports) {
const path = normalizePath(importedPath);
const content = await methods.resolveImportedEntities(ctx, importedEntities, entryPath, path);

replacement.push(content.join('\n') as ImportStatement);
}

Expand All @@ -260,7 +259,7 @@ async function resolveImportedEntities(
imported: ImportInput[],
entryPath: EntryPath,
path: string,
): Promise<ImportStatement[]> {
): Promise<string[]> {
// If the imported item is part of another entry point, let's resolve it from analysis.
if (path !== entryPath && ctx.entries.has(path)) {
return methods.resolveImportedCircularEntities(ctx, imported, path);
Expand All @@ -283,13 +282,22 @@ async function resolveImportedCircularEntities(
) {
const entityMap = new Map<string, string[]>();
const originalEntry = ctx.entries.get(path)!;
const wildcardImports: ImportStatement[] = [];
const circularStatements: string[] = [];

for (const entity of imported) {
const { originalName, alias, name, importDefault } = entity;

if (importDefault && name === '*') {
wildcardImports.push(`import * as ${alias} from '${path}'`);
const reexports: string[] = [];
const moduleExports = ctx.entries.get(path)!.exports;
const imports = [...moduleExports.entries()].map(([entityName, val]): ImportInput => {
const name = `${alias ?? ''}${entityName}`;
reexports.push(`${entityName}: ${name}`);
return { ...val, name, alias: name };
});

const reimports = await resolveImportedEntities(ctx, imports, '.', path);
circularStatements.push(...reimports, `export const ${alias} = { ${reexports.join(', ')} }`);
continue;
}

Expand All @@ -313,7 +321,7 @@ async function resolveImportedCircularEntities(
([p, ents]) => `import { ${ents.join(', ')} } from '${p}'`,
) as ImportStatement[];

return [...formattedImports, ...wildcardImports];
return [...formattedImports, ...circularStatements];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/cases/wildcard-export/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it } from 'vitest';

import type { CaseTarget } from '../../utils';
import { resolveModule } from '../../utils';
import { constructCircularImport, resolveModule } from '../../utils';
import { createCaseTarget, setupCase, testCase } from '../../utils';

const casePath = '@test-cases/wildcard-export';
Expand Down Expand Up @@ -62,7 +62,7 @@ export function testWildcardExportWithAlias(middleTarget?: CaseTarget) {
const otherTarget = await createCaseTarget('@test-modules/named-exports');
const { importPath, targetList } = setupCase(target, middleTarget);
const input = `import { AliasedWildcard } from '${importPath}';\n`;
const output = `import * as AliasedWildcard from '${resolved}';\n`;
const output = constructCircularImport(resolved!, ['NamedExportOne', 'NamedExportTwo'], 'AliasedWildcard');
await testCase([...targetList, otherTarget], input, output);
});

Expand All @@ -86,7 +86,7 @@ export function testWildcardExportWithAlias(middleTarget?: CaseTarget) {
const expectedPath = (await createCaseTarget(`${casePath}/wildcard-two`)).path;
const { importPath, targetList } = setupCase(target, middleTarget);
const input = `import { AliasedWildcard } from '${importPath}';\n`;
const output = `import * as AliasedWildcard from '${expectedPath}';\n`;
const output = constructCircularImport(expectedPath, ['ExportedFromWildcardTwo'], 'AliasedWildcard');
await testCase(targetList, input, output, { maxWildcardDepth: 2 });
});
});
Expand Down
Loading

0 comments on commit 20b7981

Please sign in to comment.