@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.
- package/README.md +331 -72
- package/dist/assets.d.ts +4 -1
- package/dist/assets.d.ts.map +1 -1
- package/dist/assets.js +2 -2
- package/dist/lib/access.d.ts +35 -4
- package/dist/lib/access.d.ts.map +1 -1
- package/dist/lib/access.js +332 -11
- package/dist/lib/asset-server.d.ts +138 -11
- package/dist/lib/asset-server.d.ts.map +1 -1
- package/dist/lib/asset-server.js +281 -30
- package/dist/lib/compilation-error.d.ts +1 -1
- package/dist/lib/compilation-error.d.ts.map +1 -1
- package/dist/lib/file-matcher.js +1 -1
- package/dist/lib/files/compiler.d.ts.map +1 -1
- package/dist/lib/files/compiler.js +9 -8
- package/dist/lib/files/config.js +1 -1
- package/dist/lib/hmr.d.ts +37 -0
- package/dist/lib/hmr.d.ts.map +1 -0
- package/dist/lib/hmr.js +357 -0
- package/dist/lib/injected-packages.d.ts +3 -2
- package/dist/lib/injected-packages.d.ts.map +1 -1
- package/dist/lib/injected-packages.js +43 -17
- package/dist/lib/inspection.d.ts +39 -0
- package/dist/lib/inspection.d.ts.map +1 -0
- package/dist/lib/inspection.js +160 -0
- package/dist/lib/loaders.d.ts +57 -0
- package/dist/lib/loaders.d.ts.map +1 -0
- package/dist/lib/loaders.js +1 -0
- package/dist/lib/module-store.d.ts +24 -0
- package/dist/lib/module-store.d.ts.map +1 -1
- package/dist/lib/module-store.js +136 -11
- package/dist/lib/routes.d.ts +11 -3
- package/dist/lib/routes.d.ts.map +1 -1
- package/dist/lib/routes.js +124 -80
- package/dist/lib/scripts/compiler.d.ts +24 -1
- package/dist/lib/scripts/compiler.d.ts.map +1 -1
- package/dist/lib/scripts/compiler.js +183 -29
- package/dist/lib/scripts/conditions.d.ts +2 -0
- package/dist/lib/scripts/conditions.d.ts.map +1 -0
- package/dist/lib/scripts/conditions.js +1 -0
- package/dist/lib/scripts/emit.d.ts +3 -0
- package/dist/lib/scripts/emit.d.ts.map +1 -1
- package/dist/lib/scripts/emit.js +24 -5
- package/dist/lib/scripts/resolve.d.ts +4 -0
- package/dist/lib/scripts/resolve.d.ts.map +1 -1
- package/dist/lib/scripts/resolve.js +75 -10
- package/dist/lib/scripts/transform.d.ts +9 -0
- package/dist/lib/scripts/transform.d.ts.map +1 -1
- package/dist/lib/scripts/transform.js +222 -18
- package/dist/lib/source-maps.js +1 -1
- package/dist/lib/styles/compiler.d.ts +14 -1
- package/dist/lib/styles/compiler.d.ts.map +1 -1
- package/dist/lib/styles/compiler.js +78 -14
- package/dist/lib/styles/emit.js +4 -4
- package/dist/lib/styles/resolve.d.ts.map +1 -1
- package/dist/lib/styles/resolve.js +16 -15
- package/dist/lib/styles/transform.js +5 -5
- package/dist/lib/target.d.ts +1 -1
- package/dist/lib/target.d.ts.map +1 -1
- package/dist/lib/watch.js +1 -1
- package/dist/types/hmr.d.ts +36 -0
- package/package.json +16 -11
- package/src/assets.ts +4 -1
- package/src/lib/access.ts +445 -11
- package/src/lib/asset-server.ts +466 -27
- package/src/lib/compilation-error.ts +4 -3
- package/src/lib/files/compiler.ts +9 -5
- package/src/lib/hmr.ts +397 -0
- package/src/lib/injected-packages.ts +52 -16
- package/src/lib/inspection.ts +229 -0
- package/src/lib/loaders.ts +80 -0
- package/src/lib/module-store.ts +190 -9
- package/src/lib/routes.ts +158 -126
- package/src/lib/scripts/compiler.ts +248 -24
- package/src/lib/scripts/conditions.ts +1 -0
- package/src/lib/scripts/emit.ts +50 -5
- package/src/lib/scripts/resolve.ts +129 -7
- package/src/lib/scripts/transform.ts +290 -19
- package/src/lib/styles/compiler.ts +108 -8
- package/src/lib/styles/emit.ts +1 -1
- package/src/lib/styles/resolve.ts +19 -15
- package/src/lib/styles/transform.ts +2 -2
- 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
|
|
18
|
-
|
|
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
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
38
|
+
mountConfigs: readonly MountConfig[],
|
|
53
39
|
): CompiledRoutes {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
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
|
-
|
|
58
|
+
return matchFilePath(filePath)?.urlPathname ?? null
|
|
59
|
+
},
|
|
60
|
+
matchFilePath,
|
|
61
|
+
}
|
|
90
62
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
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
|
|
104
|
-
|
|
103
|
+
function compileMount(
|
|
104
|
+
urlRoot: string,
|
|
105
|
+
fileRoot: string,
|
|
105
106
|
options: {
|
|
106
107
|
basePath: string
|
|
107
108
|
rootDir: string
|
|
108
109
|
},
|
|
109
|
-
):
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
|
134
|
-
let
|
|
123
|
+
function normalizeMountUrlRoot(urlRoot: string): string {
|
|
124
|
+
let normalizedRoot = normalizePathname(urlRoot).replace(/\/+$/, '') || '/'
|
|
125
|
+
let url = new URL(normalizedRoot, 'http://remix.run')
|
|
135
126
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
|
137
|
+
return url.pathname.replace(/\/+$/, '') || '/'
|
|
146
138
|
}
|
|
147
139
|
|
|
148
|
-
function
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
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
|
|
183
|
-
|
|
184
|
-
|
|
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
|
}
|