@remix-run/assets 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +319 -2
  3. package/dist/assets.d.ts +3 -0
  4. package/dist/assets.d.ts.map +1 -0
  5. package/dist/assets.js +1 -0
  6. package/dist/lib/access.d.ts +10 -0
  7. package/dist/lib/access.d.ts.map +1 -0
  8. package/dist/lib/access.js +14 -0
  9. package/dist/lib/asset-server.d.ts +137 -0
  10. package/dist/lib/asset-server.d.ts.map +1 -0
  11. package/dist/lib/asset-server.js +242 -0
  12. package/dist/lib/compilation-error.d.ts +33 -0
  13. package/dist/lib/compilation-error.d.ts.map +1 -0
  14. package/dist/lib/compilation-error.js +32 -0
  15. package/dist/lib/file-matcher.d.ts +6 -0
  16. package/dist/lib/file-matcher.d.ts.map +1 -0
  17. package/dist/lib/file-matcher.js +43 -0
  18. package/dist/lib/fingerprint.d.ts +12 -0
  19. package/dist/lib/fingerprint.d.ts.map +1 -0
  20. package/dist/lib/fingerprint.js +49 -0
  21. package/dist/lib/paths.d.ts +8 -0
  22. package/dist/lib/paths.d.ts.map +1 -0
  23. package/dist/lib/paths.js +50 -0
  24. package/dist/lib/routes.d.ts +13 -0
  25. package/dist/lib/routes.d.ts.map +1 -0
  26. package/dist/lib/routes.js +94 -0
  27. package/dist/lib/scripts/cjs-check.d.ts +3 -0
  28. package/dist/lib/scripts/cjs-check.d.ts.map +1 -0
  29. package/dist/lib/scripts/cjs-check.js +398 -0
  30. package/dist/lib/scripts/compiler.d.ts +62 -0
  31. package/dist/lib/scripts/compiler.d.ts.map +1 -0
  32. package/dist/lib/scripts/compiler.js +435 -0
  33. package/dist/lib/scripts/emit.d.ts +25 -0
  34. package/dist/lib/scripts/emit.d.ts.map +1 -0
  35. package/dist/lib/scripts/emit.js +63 -0
  36. package/dist/lib/scripts/resolve.d.ts +60 -0
  37. package/dist/lib/scripts/resolve.d.ts.map +1 -0
  38. package/dist/lib/scripts/resolve.js +230 -0
  39. package/dist/lib/scripts/store.d.ts +40 -0
  40. package/dist/lib/scripts/store.d.ts.map +1 -0
  41. package/dist/lib/scripts/store.js +228 -0
  42. package/dist/lib/scripts/transform.d.ts +62 -0
  43. package/dist/lib/scripts/transform.d.ts.map +1 -0
  44. package/dist/lib/scripts/transform.js +362 -0
  45. package/dist/lib/source-maps.d.ts +4 -0
  46. package/dist/lib/source-maps.d.ts.map +1 -0
  47. package/dist/lib/source-maps.js +56 -0
  48. package/dist/lib/watch.d.ts +22 -0
  49. package/dist/lib/watch.d.ts.map +1 -0
  50. package/dist/lib/watch.js +96 -0
  51. package/package.json +50 -12
  52. package/src/assets.ts +2 -0
  53. package/src/lib/access.ts +24 -0
  54. package/src/lib/asset-server.ts +415 -0
  55. package/src/lib/compilation-error.ts +61 -0
  56. package/src/lib/file-matcher.ts +62 -0
  57. package/src/lib/fingerprint.ts +65 -0
  58. package/src/lib/paths.ts +66 -0
  59. package/src/lib/routes.ts +164 -0
  60. package/src/lib/scripts/cjs-check.ts +476 -0
  61. package/src/lib/scripts/compiler.ts +622 -0
  62. package/src/lib/scripts/emit.ts +122 -0
  63. package/src/lib/scripts/resolve.ts +422 -0
  64. package/src/lib/scripts/store.ts +327 -0
  65. package/src/lib/scripts/transform.ts +594 -0
  66. package/src/lib/source-maps.ts +68 -0
  67. package/src/lib/watch.ts +136 -0
@@ -0,0 +1,136 @@
1
+ import chokidar from 'chokidar'
2
+
3
+ import { getFilePathDirectory, normalizeFilePath } from './paths.ts'
4
+
5
+ type AssetServerWatcherOptions = {
6
+ ignore?: readonly string[]
7
+ onChokidarWatcherCreated?: (watcher: ChokidarWatcher) => void
8
+ poll?: boolean
9
+ pollInterval?: number
10
+ onFileEvent(filePath: string, event: AssetServerWatchEvent): Promise<void>
11
+ rootDir: string
12
+ }
13
+
14
+ type AssetServerWatchEvent = 'add' | 'change' | 'unlink'
15
+ export type ChokidarWatcher = ReturnType<typeof chokidar.watch>
16
+
17
+ export type AssetServerWatcher = {
18
+ close(): Promise<void>
19
+ getWatchedTargets(): readonly string[]
20
+ updateWatchedDirectories(delta: { add: readonly string[]; remove: readonly string[] }): void
21
+ }
22
+
23
+ export function createAssetServerWatcher(options: AssetServerWatcherOptions): AssetServerWatcher {
24
+ let watcher = chokidar.watch([], {
25
+ ignoreInitial: true,
26
+ ignorePermissionErrors: true,
27
+ ...resolveChokidarWatchOptions(options),
28
+ })
29
+ options.onChokidarWatcherCreated?.(watcher)
30
+ let watchedDirectories = new Set<string>()
31
+ let watchedTargets = new Set<string>()
32
+
33
+ for (let event of ['add', 'change', 'unlink'] as const) {
34
+ watcher.on(event, (filePath) => {
35
+ options.onFileEvent(filePath, event)
36
+ })
37
+ }
38
+ watcher.on('error', (error) => {
39
+ console.error('Asset server file system watcher encountered an error.', error)
40
+ })
41
+
42
+ return {
43
+ async close() {
44
+ await watcher.close()
45
+ },
46
+ getWatchedTargets() {
47
+ return [...watchedTargets]
48
+ },
49
+ updateWatchedDirectories(delta) {
50
+ let nextWatchedDirectories = new Set(watchedDirectories)
51
+
52
+ for (let directory of delta.add) {
53
+ nextWatchedDirectories.add(directory)
54
+ }
55
+ for (let directory of delta.remove) {
56
+ nextWatchedDirectories.delete(directory)
57
+ }
58
+
59
+ let nextTargets = getWatchTargetsForDirectories(options.rootDir, [...nextWatchedDirectories])
60
+ let targetsToAdd = [...nextTargets].filter((target) => !watchedTargets.has(target))
61
+ let targetsToRemove = [...watchedTargets].filter((target) => !nextTargets.has(target))
62
+
63
+ if (targetsToRemove.length > 0) {
64
+ watcher.unwatch(targetsToRemove)
65
+ }
66
+ if (targetsToAdd.length > 0) {
67
+ watcher.add(targetsToAdd)
68
+ }
69
+
70
+ watchedDirectories = nextWatchedDirectories
71
+ watchedTargets = nextTargets
72
+ },
73
+ }
74
+ }
75
+
76
+ function resolveChokidarWatchOptions(
77
+ options: AssetServerWatcherOptions,
78
+ ): Exclude<Parameters<typeof chokidar.watch>[1], undefined> {
79
+ return {
80
+ awaitWriteFinish: {
81
+ pollInterval: 10,
82
+ stabilityThreshold: 10,
83
+ },
84
+ depth: 0,
85
+ ignored: ['**/.git/**', ...(options.ignore ?? [])],
86
+ interval: options.pollInterval ?? 100,
87
+ usePolling: options.poll ?? false,
88
+ }
89
+ }
90
+
91
+ function getWatchTargetsForDirectories(
92
+ rootDir: string,
93
+ directories: readonly string[],
94
+ ): Set<string> {
95
+ let normalizedRootDir = normalizeFilePath(rootDir)
96
+ let targets = new Set<string>()
97
+ let configAncestors = new Set<string>()
98
+
99
+ for (let directory of directories) {
100
+ let normalizedDirectory = normalizeFilePath(directory).replace(/\/+$/, '')
101
+
102
+ targets.add(normalizedDirectory)
103
+ if (!isSameOrDescendantPath(normalizedDirectory, normalizedRootDir)) continue
104
+
105
+ for (let ancestor of getAncestorPaths(normalizedDirectory, normalizedRootDir)) {
106
+ configAncestors.add(ancestor)
107
+ }
108
+ }
109
+
110
+ for (let ancestor of configAncestors) {
111
+ targets.add(ancestor)
112
+ }
113
+
114
+ return targets
115
+ }
116
+
117
+ function getAncestorPaths(directoryPath: string, rootDir: string): string[] {
118
+ let ancestors: string[] = []
119
+ let currentDirectory = directoryPath
120
+
121
+ while (isSameOrDescendantPath(currentDirectory, rootDir)) {
122
+ ancestors.push(currentDirectory)
123
+ if (currentDirectory === rootDir) break
124
+ let parentDirectory = getFilePathDirectory(currentDirectory)
125
+ if (parentDirectory === currentDirectory) break
126
+ currentDirectory = parentDirectory
127
+ }
128
+
129
+ return ancestors
130
+ }
131
+
132
+ function isSameOrDescendantPath(filePath: string, directoryPath: string): boolean {
133
+ let normalizedDirectoryPath = directoryPath.replace(/\/+$/, '')
134
+
135
+ return filePath === normalizedDirectoryPath || filePath.startsWith(`${normalizedDirectoryPath}/`)
136
+ }