@remix-run/assets 0.4.4 → 0.6.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 (83) hide show
  1. package/README.md +331 -72
  2. package/dist/assets.d.ts +4 -1
  3. package/dist/assets.d.ts.map +1 -1
  4. package/dist/assets.js +2 -2
  5. package/dist/lib/access.d.ts +35 -4
  6. package/dist/lib/access.d.ts.map +1 -1
  7. package/dist/lib/access.js +332 -11
  8. package/dist/lib/asset-server.d.ts +138 -11
  9. package/dist/lib/asset-server.d.ts.map +1 -1
  10. package/dist/lib/asset-server.js +281 -30
  11. package/dist/lib/compilation-error.d.ts +1 -1
  12. package/dist/lib/compilation-error.d.ts.map +1 -1
  13. package/dist/lib/file-matcher.js +1 -1
  14. package/dist/lib/files/compiler.d.ts.map +1 -1
  15. package/dist/lib/files/compiler.js +9 -8
  16. package/dist/lib/files/config.js +1 -1
  17. package/dist/lib/hmr.d.ts +37 -0
  18. package/dist/lib/hmr.d.ts.map +1 -0
  19. package/dist/lib/hmr.js +357 -0
  20. package/dist/lib/injected-packages.d.ts +3 -2
  21. package/dist/lib/injected-packages.d.ts.map +1 -1
  22. package/dist/lib/injected-packages.js +43 -17
  23. package/dist/lib/inspection.d.ts +39 -0
  24. package/dist/lib/inspection.d.ts.map +1 -0
  25. package/dist/lib/inspection.js +160 -0
  26. package/dist/lib/loaders.d.ts +57 -0
  27. package/dist/lib/loaders.d.ts.map +1 -0
  28. package/dist/lib/loaders.js +1 -0
  29. package/dist/lib/module-store.d.ts +24 -0
  30. package/dist/lib/module-store.d.ts.map +1 -1
  31. package/dist/lib/module-store.js +136 -11
  32. package/dist/lib/routes.d.ts +11 -3
  33. package/dist/lib/routes.d.ts.map +1 -1
  34. package/dist/lib/routes.js +124 -80
  35. package/dist/lib/scripts/compiler.d.ts +24 -1
  36. package/dist/lib/scripts/compiler.d.ts.map +1 -1
  37. package/dist/lib/scripts/compiler.js +183 -29
  38. package/dist/lib/scripts/conditions.d.ts +2 -0
  39. package/dist/lib/scripts/conditions.d.ts.map +1 -0
  40. package/dist/lib/scripts/conditions.js +1 -0
  41. package/dist/lib/scripts/emit.d.ts +3 -0
  42. package/dist/lib/scripts/emit.d.ts.map +1 -1
  43. package/dist/lib/scripts/emit.js +24 -5
  44. package/dist/lib/scripts/resolve.d.ts +4 -0
  45. package/dist/lib/scripts/resolve.d.ts.map +1 -1
  46. package/dist/lib/scripts/resolve.js +75 -10
  47. package/dist/lib/scripts/transform.d.ts +9 -0
  48. package/dist/lib/scripts/transform.d.ts.map +1 -1
  49. package/dist/lib/scripts/transform.js +222 -18
  50. package/dist/lib/source-maps.js +1 -1
  51. package/dist/lib/styles/compiler.d.ts +14 -1
  52. package/dist/lib/styles/compiler.d.ts.map +1 -1
  53. package/dist/lib/styles/compiler.js +78 -14
  54. package/dist/lib/styles/emit.js +4 -4
  55. package/dist/lib/styles/resolve.d.ts.map +1 -1
  56. package/dist/lib/styles/resolve.js +16 -15
  57. package/dist/lib/styles/transform.js +5 -5
  58. package/dist/lib/target.d.ts +1 -1
  59. package/dist/lib/target.d.ts.map +1 -1
  60. package/dist/lib/watch.js +1 -1
  61. package/dist/types/hmr.d.ts +36 -0
  62. package/package.json +16 -11
  63. package/src/assets.ts +4 -1
  64. package/src/lib/access.ts +445 -11
  65. package/src/lib/asset-server.ts +466 -27
  66. package/src/lib/compilation-error.ts +4 -3
  67. package/src/lib/files/compiler.ts +9 -5
  68. package/src/lib/hmr.ts +397 -0
  69. package/src/lib/injected-packages.ts +52 -16
  70. package/src/lib/inspection.ts +229 -0
  71. package/src/lib/loaders.ts +80 -0
  72. package/src/lib/module-store.ts +190 -9
  73. package/src/lib/routes.ts +158 -126
  74. package/src/lib/scripts/compiler.ts +248 -24
  75. package/src/lib/scripts/conditions.ts +1 -0
  76. package/src/lib/scripts/emit.ts +50 -5
  77. package/src/lib/scripts/resolve.ts +129 -7
  78. package/src/lib/scripts/transform.ts +290 -19
  79. package/src/lib/styles/compiler.ts +108 -8
  80. package/src/lib/styles/emit.ts +1 -1
  81. package/src/lib/styles/resolve.ts +19 -15
  82. package/src/lib/styles/transform.ts +2 -2
  83. package/src/types/hmr.d.ts +36 -0
package/src/lib/routes.ts CHANGED
@@ -1,186 +1,218 @@
1
- import {
2
- getRoutePatternCaptures,
3
- RoutePattern,
4
- type RoutePatternCapture,
5
- } from '@remix-run/route-pattern'
6
- import { createHref } from '@remix-run/route-pattern/href'
7
- import { createMatcher, type Matcher } from '@remix-run/route-pattern/match'
1
+ import * as fs from 'node:fs'
8
2
 
9
3
  import {
10
- getRelativeFilePath,
11
4
  isAbsoluteFilePath,
12
5
  normalizeFilePath,
13
6
  normalizePathname,
14
7
  resolveFilePath,
15
8
  } from './paths.ts'
16
9
 
17
- interface AssetRouteDefinition {
18
- urlPattern: string
19
- filePattern: string
20
- }
21
-
22
- interface RouteConfig {
23
- fileMap: Readonly<Record<string, string>>
10
+ interface MountConfig {
11
+ mounts: Readonly<Record<string, string>>
24
12
  rootDir: string
25
13
  }
26
14
 
27
- interface CompiledRoute {
28
- rootDir: string
29
- urlPattern: RoutePattern
30
- urlMatcher: Matcher
31
- filePattern: RoutePattern
32
- fileMatcher: Matcher
15
+ interface CompiledMount {
16
+ fileRoot: string
17
+ fileRootValue: string
18
+ urlRoot: string
19
+ urlRootKey: string
33
20
  }
34
21
 
35
22
  export interface CompiledRoutes {
36
23
  resolveUrlPathname(pathname: string): string | null
24
+ matchUrlPathname(pathname: string): AssetRouteMatch | null
37
25
  toUrlPathname(filePath: string): string | null
26
+ matchFilePath(filePath: string): AssetRouteMatch | null
38
27
  }
39
28
 
40
- function normalizeFilePattern(pattern: string): string {
41
- if (isAbsoluteFilePath(pattern)) {
42
- throw new Error(
43
- `File route patterns must be relative to the asset server root.\nPattern: ${pattern}`,
44
- )
45
- }
46
-
47
- return normalizePathname(pattern)
29
+ export interface AssetRouteMatch {
30
+ filePath: string
31
+ fileRoot: string
32
+ urlPathname: string
33
+ urlRoot: string
48
34
  }
49
35
 
50
36
  export function compileRoutes(
51
37
  basePath: string,
52
- routeConfigs: readonly RouteConfig[],
38
+ mountConfigs: readonly MountConfig[],
53
39
  ): CompiledRoutes {
54
- if (routeConfigs.every((routeConfig) => Object.keys(routeConfig.fileMap).length === 0)) {
55
- throw new Error('createAssetServer() requires at least one configured fileMap entry.')
56
- }
57
-
58
- let compiledRoutes = routeConfigs.flatMap((routeConfig) =>
59
- Object.entries(routeConfig.fileMap).map(([urlPattern, filePattern]) =>
60
- compileRoute(
61
- {
62
- filePattern,
63
- urlPattern,
64
- },
65
- {
66
- basePath,
67
- rootDir: routeConfig.rootDir,
68
- },
69
- ),
70
- ),
71
- )
40
+ let compiledMounts = mountConfigs.flatMap((mountConfig) => {
41
+ let configMounts = Object.entries(mountConfig.mounts).map(([urlRoot, fileRoot]) =>
42
+ compileMount(urlRoot, fileRoot, {
43
+ basePath,
44
+ rootDir: mountConfig.rootDir,
45
+ }),
46
+ )
47
+ validateNoOverlappingMounts(configMounts)
48
+ return configMounts
49
+ })
50
+ validateNoOverlappingUrlRoots(compiledMounts)
72
51
 
73
52
  return {
74
53
  resolveUrlPathname(pathname) {
75
- let normalizedPathname = normalizePathname(pathname)
76
-
77
- for (let route of compiledRoutes) {
78
- let match = route.urlMatcher.match(`http://remix.run${normalizedPathname}`)
79
- if (!match) continue
80
- let relativeFilePath = decodeURIComponent(
81
- createHref(route.filePattern, match.params),
82
- ).replace(/^\/+/, '')
83
- return resolveFilePath(route.rootDir, relativeFilePath)
84
- }
85
-
86
- return null
54
+ return matchUrlPathname(pathname)?.filePath ?? null
87
55
  },
56
+ matchUrlPathname,
88
57
  toUrlPathname(filePath) {
89
- let normalizedFilePath = normalizeFilePath(filePath)
58
+ return matchFilePath(filePath)?.urlPathname ?? null
59
+ },
60
+ matchFilePath,
61
+ }
90
62
 
91
- for (let route of compiledRoutes) {
92
- let relativeFilePath = getRelativeFilePath(route.rootDir, normalizedFilePath)
93
- let match = route.fileMatcher.match(`http://remix.run/${relativeFilePath}`)
94
- if (!match) continue
95
- return normalizePathname(createHref(route.urlPattern, match.params))
63
+ function matchUrlPathname(pathname: string): AssetRouteMatch | null {
64
+ let normalizedPathname = normalizePathname(pathname)
65
+
66
+ for (let mount of compiledMounts) {
67
+ let relativePathname = getPathWithinRoot(mount.urlRoot, normalizedPathname)
68
+ if (relativePathname === null) continue
69
+ let filePath = resolveFilePath(mount.fileRoot, decodeURIComponent(relativePathname))
70
+ if (getPathWithinRoot(mount.fileRoot, filePath) === null) return null
71
+
72
+ return {
73
+ filePath,
74
+ fileRoot: mount.fileRootValue,
75
+ urlPathname: normalizedPathname,
76
+ urlRoot: mount.urlRoot,
96
77
  }
78
+ }
97
79
 
98
- return null
99
- },
80
+ return null
81
+ }
82
+
83
+ function matchFilePath(filePath: string): AssetRouteMatch | null {
84
+ let normalizedFilePath = normalizeFilePath(filePath)
85
+
86
+ for (let mount of compiledMounts) {
87
+ let relativeFilePath = getPathWithinRoot(mount.fileRoot, normalizedFilePath)
88
+ if (relativeFilePath === null) continue
89
+ let encodedFilePath = relativeFilePath.split('/').map(encodeURIComponent).join('/')
90
+
91
+ return {
92
+ filePath: normalizedFilePath,
93
+ fileRoot: mount.fileRootValue,
94
+ urlPathname: joinUrlPath(mount.urlRoot, encodedFilePath),
95
+ urlRoot: mount.urlRoot,
96
+ }
97
+ }
98
+
99
+ return null
100
100
  }
101
101
  }
102
102
 
103
- function compileRoute(
104
- route: AssetRouteDefinition,
103
+ function compileMount(
104
+ urlRoot: string,
105
+ fileRoot: string,
105
106
  options: {
106
107
  basePath: string
107
108
  rootDir: string
108
109
  },
109
- ): CompiledRoute {
110
- let basePath = normalizePathname(options.basePath).replace(/\/+$/, '') || '/'
111
- let relativeUrlPattern = normalizePathname(route.urlPattern)
112
- let urlPatternSource = normalizePathname(
113
- `${basePath.replace(/\/+$/, '')}/${relativeUrlPattern.replace(/^\/+/, '')}`,
114
- )
115
- let filePatternSource = normalizeFilePattern(route.filePattern)
116
-
117
- let urlPattern = RoutePattern.parse(urlPatternSource)
118
- let filePattern = RoutePattern.parse(filePatternSource)
119
-
120
- validateNoUnnamedWildcards(urlPattern, 'URL')
121
- validateNoUnnamedWildcards(filePattern, 'File')
122
- validateRoutePatterns(urlPattern, filePattern)
110
+ ): CompiledMount {
111
+ if (isAbsoluteFilePath(fileRoot)) {
112
+ throw new TypeError(`mounts values must be relative to rootDir. Received "${fileRoot}".`)
113
+ }
123
114
 
124
115
  return {
125
- rootDir: normalizeFilePath(options.rootDir).replace(/\/+$/, ''),
126
- urlPattern,
127
- urlMatcher: createMatcher(urlPattern),
128
- filePattern,
129
- fileMatcher: createMatcher(stripDotSegments(filePatternSource)),
116
+ fileRoot: resolveMountFileRoot(options.rootDir, fileRoot),
117
+ fileRootValue: fileRoot,
118
+ urlRoot: joinUrlPath(normalizeMountUrlRoot(options.basePath), normalizeMountUrlRoot(urlRoot)),
119
+ urlRootKey: urlRoot,
130
120
  }
131
121
  }
132
122
 
133
- function stripDotSegments(pattern: string): string {
134
- let segments: string[] = []
123
+ function normalizeMountUrlRoot(urlRoot: string): string {
124
+ let normalizedRoot = normalizePathname(urlRoot).replace(/\/+$/, '') || '/'
125
+ let url = new URL(normalizedRoot, 'http://remix.run')
135
126
 
136
- for (let segment of pattern.split('/')) {
137
- if (segment === '' || segment === '.') continue
138
- if (segment === '..') {
139
- segments.pop()
140
- continue
141
- }
142
- segments.push(segment)
127
+ if (
128
+ url.search !== '' ||
129
+ url.hash !== '' ||
130
+ getUrlPathSegmentCount(url.pathname) !== getUrlPathSegmentCount(normalizedRoot)
131
+ ) {
132
+ throw new TypeError(
133
+ `mounts keys must be URL pathnames without query strings, fragments, or encoded dot segments. Received "${urlRoot}".`,
134
+ )
143
135
  }
144
136
 
145
- return segments.join('/')
137
+ return url.pathname.replace(/\/+$/, '') || '/'
146
138
  }
147
139
 
148
- function validateRoutePatterns(urlPattern: RoutePattern, filePattern: RoutePattern): void {
149
- let urlCaptures = getPathnameCaptures(urlPattern)
150
- let fileCaptures = getPathnameCaptures(filePattern)
151
- if (urlCaptures.length !== fileCaptures.length) {
152
- throw new Error(
153
- `Route patterns must have matching capture structure.\nURL: ${urlPattern}\nFile: ${filePattern}`,
154
- )
140
+ function resolveMountFileRoot(rootDir: string, fileRoot: string): string {
141
+ let resolvedRoot = resolveFilePath(rootDir, fileRoot)
142
+
143
+ try {
144
+ resolvedRoot = normalizeFilePath(fs.realpathSync(resolvedRoot))
145
+ } catch (error) {
146
+ if (!isUnresolvedPathError(error, resolvedRoot)) throw error
155
147
  }
156
148
 
157
- for (let i = 0; i < urlCaptures.length; i++) {
158
- let urlCapture = urlCaptures[i]
159
- let fileCapture = fileCaptures[i]
160
- if (urlCapture.type !== fileCapture.type || urlCapture.name !== fileCapture.name) {
161
- throw new Error(
162
- `Route patterns must have matching capture structure.\nURL: ${urlPattern}\nFile: ${filePattern}`,
163
- )
149
+ return resolvedRoot.replace(/\/+$/, '') || '/'
150
+ }
151
+
152
+ function getUrlPathSegmentCount(pathname: string): number {
153
+ return pathname.split('/').filter((segment) => segment !== '').length
154
+ }
155
+
156
+ function joinUrlPath(root: string, path: string): string {
157
+ if (path === '' || path === '/') return normalizeMountUrlRoot(root)
158
+ return normalizePathname(`${root.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`)
159
+ }
160
+
161
+ function getPathWithinRoot(root: string, value: string): string | null {
162
+ if (value === root) return ''
163
+ if (root === '/') return value.slice(1)
164
+ if (!value.startsWith(`${root}/`)) return null
165
+ return value.slice(root.length + 1)
166
+ }
167
+
168
+ function validateNoOverlappingMounts(mounts: readonly CompiledMount[]): void {
169
+ for (let index = 0; index < mounts.length; index++) {
170
+ let mount = mounts[index]
171
+
172
+ for (let otherIndex = index + 1; otherIndex < mounts.length; otherIndex++) {
173
+ let otherMount = mounts[otherIndex]
174
+
175
+ if (rootsOverlap(mount.fileRoot, otherMount.fileRoot)) {
176
+ throw new TypeError(
177
+ `mounts values must not overlap. Received "${mount.fileRootValue}" and "${otherMount.fileRootValue}", resolving to "${mount.fileRoot}" and "${otherMount.fileRoot}".`,
178
+ )
179
+ }
164
180
  }
165
181
  }
166
182
  }
167
183
 
168
- function validateNoUnnamedWildcards(pattern: RoutePattern, label: string): void {
169
- if (
170
- getRoutePatternCaptures(pattern).some(
171
- (capture) => capture.part === 'pathname' && capture.type === '*' && capture.name === '*',
172
- )
173
- ) {
174
- throw new Error(
175
- `${label} route patterns must use named wildcards for reversible mapping.\nPattern: ${pattern}`,
176
- )
184
+ function validateNoOverlappingUrlRoots(mounts: readonly CompiledMount[]): void {
185
+ for (let index = 0; index < mounts.length; index++) {
186
+ let mount = mounts[index]
187
+
188
+ for (let otherIndex = index + 1; otherIndex < mounts.length; otherIndex++) {
189
+ let otherMount = mounts[otherIndex]
190
+ if (!rootsOverlap(mount.urlRoot, otherMount.urlRoot)) continue
191
+
192
+ throw new TypeError(
193
+ `mounts keys must not overlap. Received "${mount.urlRootKey}" and "${otherMount.urlRootKey}".`,
194
+ )
195
+ }
177
196
  }
178
197
  }
179
198
 
180
- type PathnameCapture = RoutePatternCapture & { readonly part: 'pathname' }
199
+ function rootsOverlap(root: string, otherRoot: string): boolean {
200
+ return (
201
+ root === otherRoot ||
202
+ root === '/' ||
203
+ otherRoot === '/' ||
204
+ root.startsWith(`${otherRoot}/`) ||
205
+ otherRoot.startsWith(`${root}/`)
206
+ )
207
+ }
181
208
 
182
- function getPathnameCaptures(pattern: RoutePattern): Array<PathnameCapture> {
183
- return getRoutePatternCaptures(pattern).filter(
184
- (capture): capture is PathnameCapture => capture.part === 'pathname',
209
+ function isUnresolvedPathError(error: unknown, filePath: string): boolean {
210
+ // Windows reports UNKNOWN rather than ENOENT when a UNC share cannot be reached.
211
+ return (
212
+ error instanceof Error &&
213
+ 'code' in error &&
214
+ (error.code === 'ENOENT' ||
215
+ error.code === 'ENOTDIR' ||
216
+ (error.code === 'UNKNOWN' && filePath.startsWith('//')))
185
217
  )
186
218
  }