From dd0a227328126fc0070f0d5fd3874134712ca8f1 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sat, 7 Dec 2024 14:19:17 +0100 Subject: [PATCH] normalize prepare --- src/lint.ts | 9 +++++++-- src/plugins/alias-plugin.ts | 5 ++++- src/plugins/output-state-plugin.ts | 11 ++++++----- src/prepare/prepare-entries.ts | 4 ++-- src/utils.ts | 4 ++++ test/integration/utils.ts | 3 ++- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/lint.ts b/src/lint.ts index 30a5d689..bd8741d3 100644 --- a/src/lint.ts +++ b/src/lint.ts @@ -2,7 +2,12 @@ import path from 'path' import { parseExports } from './exports' import { logger } from './logger' import { PackageMetadata } from './types' -import { hasCjsExtension, isESModulePackage, isTypeFile } from './utils' +import { + hasCjsExtension, + isESModulePackage, + isTypeFile, + normalizePath, +} from './utils' import { matchFile } from './lib/file-match' type BadExportItem = { @@ -46,7 +51,7 @@ function validateFilesField(packageJson: PackageMetadata) { } const exportedPaths = resolveExportsPaths(exportsField).map((p) => - path.normalize(p), + normalizePath(path.normalize(p)), ) const commonFields = ['main', 'module', 'types', 'module-sync'] for (const field of commonFields) { diff --git a/src/plugins/alias-plugin.ts b/src/plugins/alias-plugin.ts index fe49a186..110d96bd 100644 --- a/src/plugins/alias-plugin.ts +++ b/src/plugins/alias-plugin.ts @@ -7,6 +7,7 @@ import { specialExportConventions, runtimeExportConventionsFallback, } from '../constants' +import { normalizePath } from '../utils' function hasNoSpecialCondition(conditionNames: Set) { return [...conditionNames].every( @@ -150,7 +151,9 @@ export function aliasEntries({ srcBundle && resolvedModuleBundle ) { - const absoluteBundlePath = posix.resolve(cwd, srcBundle) + const absoluteBundlePath = normalizePath( + posix.resolve(cwd, srcBundle), + ) const absoluteImportBundlePath = posix.resolve( cwd, resolvedModuleBundle, diff --git a/src/plugins/output-state-plugin.ts b/src/plugins/output-state-plugin.ts index c02e63a5..36b9c1fb 100644 --- a/src/plugins/output-state-plugin.ts +++ b/src/plugins/output-state-plugin.ts @@ -9,11 +9,12 @@ import { getSpecialExportTypeFromComposedExportPath, normalizeExportPath, } from '../entries' -import { isBinExportPath, isPrivateExportPath, isTypeFile } from '../utils' - -function normalizePath(filePath: string) { - return filePath.replace(/\\/g, '/') -} +import { + isBinExportPath, + isPrivateExportPath, + isTypeFile, + normalizePath, +} from '../utils' // [filename, sourceFileName, size] type FileState = [string, string, number] diff --git a/src/prepare/prepare-entries.ts b/src/prepare/prepare-entries.ts index e3ae3677..807ba669 100644 --- a/src/prepare/prepare-entries.ts +++ b/src/prepare/prepare-entries.ts @@ -3,7 +3,7 @@ import { glob } from 'glob' import { BINARY_TAG, availableExtensions } from '../constants' import { collectSourceEntriesByExportPath } from '../entries' -import { sourceFilenameToExportFullPath } from '../utils' +import { normalizePath, sourceFilenameToExportFullPath } from '../utils' // For `prepare` command export async function collectSourceEntries(sourceFolderPath: string) { @@ -35,7 +35,7 @@ export async function collectSourceEntries(sourceFolderPath: string) { for (const file of binMatches) { // convert relative path to export path - const exportPath = sourceFilenameToExportFullPath(file) + const exportPath = sourceFilenameToExportFullPath(normalizePath(file)) const binExportPath = exportPath.replace(/^\.[\//]bin/, BINARY_TAG) await collectSourceEntriesByExportPath( sourceFolderPath, diff --git a/src/utils.ts b/src/utils.ts index fa13bca3..dbc18855 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -256,3 +256,7 @@ export function sourceFilenameToExportFullPath(filename: string) { export function isPrivateExportPath(exportPath: string) { return /\/_/.test(exportPath) } + +export function normalizePath(filePath: string) { + return filePath.replace(/\\/g, '/') +} diff --git a/test/integration/utils.ts b/test/integration/utils.ts index 9944f6b4..654484cc 100644 --- a/test/integration/utils.ts +++ b/test/integration/utils.ts @@ -4,6 +4,7 @@ import { executeBunchee, type ExcuteBuncheeResult, } from '../testing-utils' +import { normalizePath } from 'src/utils' export * from '../testing-utils' @@ -83,5 +84,5 @@ export async function getFileNamesFromDirectory(directory: string) { cwd: directory, }) - return files.sort().map((file) => file.replace(/\\/g, '/')) + return files.sort().map((file) => normalizePath(file)) }