Skip to content

Commit

Permalink
fix: preview entries on Windows particularly for Vite (#9143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjiang authored Aug 28, 2024
1 parent f1e50ac commit de20d74
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions scopes/preview/preview/generate-link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ComponentMap } from '@teambit/component';
import { join } from 'path';
import { outputFileSync } from 'fs-extra';
import normalizePath from 'normalize-path';
import objectHash from 'object-hash';
import camelcase from 'camelcase';
import { toWindowsCompatiblePath } from '@teambit/toolbox.path.to-windows-compatible-path';
Expand Down Expand Up @@ -42,18 +43,18 @@ export function generateLink(
componentIdentifier: component.id.fullName,
modules: modulePath.map((path, pathIdx) => ({
varName: moduleVarName(compIdx, pathIdx),
resolveFrom: toWindowsCompatiblePath(path),
resolveFrom: normalizePath(path),
})),
}));

const moduleLinks: ModuleLink[] = Object.entries(mainModulesMap || {}).map(([envId, path]) => {
const resolveFrom = toWindowsCompatiblePath(path);
const resolveFrom = normalizePath(path);
const varName = getEnvVarName(envId);
return { envId, varName, resolveFrom };
});

const contents = `
import { linkModules } from '${toWindowsCompatiblePath(join(previewDistDir, 'preview.preview.runtime.js'))}';
import { linkModules } from '${normalizePath(join(previewDistDir, 'preview.preview.runtime.js'))}';
${getModuleImports(moduleLinks, tempPackageDir)}
Expand Down Expand Up @@ -101,7 +102,9 @@ function getModuleImports(moduleLinks: ModuleLink[] = [], tempPackageDir?: strin
.map((module) => `export * as ${module.varName} from "${module.resolveFrom}";`)
.join('\n');
outputFileSync(tempFilePath, tempFileContents);
return `import {${moduleLinks.map((moduleLink) => moduleLink.varName).join(', ')}} from "${tempFilePath}";`;
return `import {${moduleLinks.map((moduleLink) => moduleLink.varName).join(', ')}} from "${normalizePath(
tempFilePath
)}";`;
}

function getComponentImports(componentLinks: ComponentLink[] = []): string {
Expand Down
4 changes: 2 additions & 2 deletions scopes/preview/preview/pre-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getIdentifiers,
} from '@teambit/harmony.modules.harmony-root-generator';
import { sha1 } from '@teambit/toolbox.crypto.sha1';
import { toWindowsCompatiblePath } from '@teambit/toolbox.path.to-windows-compatible-path';
import normalizePath from 'normalize-path';
import webpack from 'webpack';
import { promisify } from 'util';
import { PreviewAspect } from './preview.aspect';
Expand Down Expand Up @@ -125,7 +125,7 @@ export async function generateBundlePreviewEntry(rootAspectId: string, previewPr
const manifest = readJsonSync(manifestPath);
const imports = manifest.entrypoints
.map((entry: string) => {
const entryPath = toWindowsCompatiblePath(join(previewPreBundlePath, entry));
const entryPath = normalizePath(join(previewPreBundlePath, entry));
return entry.endsWith('.js') || entry.endsWith('.cjs') || entry.endsWith('.mjs')
? `import { run } from '${entryPath}';`
: `import '${entryPath}';`;
Expand Down

0 comments on commit de20d74

Please sign in to comment.