Skip to content

Commit

Permalink
fix: windows paths handling (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 authored Apr 21, 2024
1 parent 2c6f314 commit 5f6de24
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-roses-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"hot-hook": patch
---

`--import=hot-hook/register` was broken due to a bug in path handling that wasn't cross platform. This has been fixed and also added a Windows CI to prevent this from happening again.

9 changes: 7 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ jobs:
run: pnpm typecheck

tests:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 10

steps:
Expand All @@ -72,4 +75,6 @@ jobs:
pnpm -r build
- name: Run tests
run: FORCE_COLOR=1 pnpm test
env:
FORCE_COLOR: 1
run: pnpm test
1 change: 1 addition & 0 deletions packages/hot_hook/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class HotHookLoader {
*/
const isReloadable = this.#dependencyTree.isReloadable(realFilePath)
if (!isReloadable) {
debug('Full reload %s', realFilePath)
return this.#messagePort?.postMessage({ type: 'hot-hook:full-reload', path: realFilePath })
}

Expand Down
24 changes: 12 additions & 12 deletions packages/hot_hook/src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ import { resolve } from 'node:path'
import picomatch from 'picomatch'

export class Matcher {
#rootDirectory: string
#matcher: picomatch.Matcher

constructor(rootDirectory: string, patterns: picomatch.Glob = []) {
this.#rootDirectory = rootDirectory

patterns = Array.isArray(patterns) ? patterns : [patterns]

const absolutePatterns = patterns
.map((pattern) => {
if (pattern.startsWith('../')) return resolve(rootDirectory, pattern)
return pattern
/**
* Do not resolve double star patterns because they are not relative to the root
*/
if (pattern.startsWith('**')) return pattern

/**
* Resolve the pattern to the root directory. All patterns are relative to the root
*/
return resolve(rootDirectory, pattern)
})
.map((path) => path.replace(/\\/g, '/'))

this.#matcher = picomatch(absolutePatterns || [], { dot: true })
this.#matcher = picomatch(absolutePatterns || [], { dot: true, posixSlashes: true })
}

/**
* Check if a path matches the patterns
*/
match(filePath: string) {
filePath = filePath.replace(/\\/g, '/')
if (filePath.startsWith(this.#rootDirectory)) {
filePath = filePath.slice(this.#rootDirectory.length).replace(/^\//, '')
}

return this.#matcher(filePath)
return this.#matcher(resolve(filePath))
}
}

0 comments on commit 5f6de24

Please sign in to comment.