Skip to content

Commit

Permalink
normalize prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Dec 7, 2024
1 parent dfab639 commit dd0a227
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/alias-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
specialExportConventions,
runtimeExportConventionsFallback,
} from '../constants'
import { normalizePath } from '../utils'

function hasNoSpecialCondition(conditionNames: Set<string>) {
return [...conditionNames].every(
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions src/plugins/output-state-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/prepare/prepare-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/')
}
3 changes: 2 additions & 1 deletion test/integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
executeBunchee,
type ExcuteBuncheeResult,
} from '../testing-utils'
import { normalizePath } from 'src/utils'

export * from '../testing-utils'

Expand Down Expand Up @@ -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))
}

0 comments on commit dd0a227

Please sign in to comment.