@remix-run/assets 0.0.0 → 0.2.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/LICENSE +21 -0
- package/README.md +325 -2
- package/dist/assets.d.ts +3 -0
- package/dist/assets.d.ts.map +1 -0
- package/dist/assets.js +1 -0
- package/dist/lib/access.d.ts +10 -0
- package/dist/lib/access.d.ts.map +1 -0
- package/dist/lib/access.js +14 -0
- package/dist/lib/asset-server.d.ts +139 -0
- package/dist/lib/asset-server.d.ts.map +1 -0
- package/dist/lib/asset-server.js +338 -0
- package/dist/lib/compilation-error.d.ts +33 -0
- package/dist/lib/compilation-error.d.ts.map +1 -0
- package/dist/lib/compilation-error.js +32 -0
- package/dist/lib/file-matcher.d.ts +6 -0
- package/dist/lib/file-matcher.d.ts.map +1 -0
- package/dist/lib/file-matcher.js +44 -0
- package/dist/lib/fingerprint.d.ts +12 -0
- package/dist/lib/fingerprint.d.ts.map +1 -0
- package/dist/lib/fingerprint.js +49 -0
- package/dist/lib/module-store.d.ts +41 -0
- package/dist/lib/module-store.d.ts.map +1 -0
- package/dist/lib/module-store.js +230 -0
- package/dist/lib/paths.d.ts +8 -0
- package/dist/lib/paths.d.ts.map +1 -0
- package/dist/lib/paths.js +50 -0
- package/dist/lib/routes.d.ts +13 -0
- package/dist/lib/routes.d.ts.map +1 -0
- package/dist/lib/routes.js +94 -0
- package/dist/lib/scripts/cjs-check.d.ts +3 -0
- package/dist/lib/scripts/cjs-check.d.ts.map +1 -0
- package/dist/lib/scripts/cjs-check.js +398 -0
- package/dist/lib/scripts/compiler.d.ts +62 -0
- package/dist/lib/scripts/compiler.d.ts.map +1 -0
- package/dist/lib/scripts/compiler.js +439 -0
- package/dist/lib/scripts/emit.d.ts +25 -0
- package/dist/lib/scripts/emit.d.ts.map +1 -0
- package/dist/lib/scripts/emit.js +63 -0
- package/dist/lib/scripts/resolve.d.ts +50 -0
- package/dist/lib/scripts/resolve.d.ts.map +1 -0
- package/dist/lib/scripts/resolve.js +236 -0
- package/dist/lib/scripts/transform.d.ts +64 -0
- package/dist/lib/scripts/transform.d.ts.map +1 -0
- package/dist/lib/scripts/transform.js +373 -0
- package/dist/lib/source-maps.d.ts +4 -0
- package/dist/lib/source-maps.d.ts.map +1 -0
- package/dist/lib/source-maps.js +62 -0
- package/dist/lib/styles/compiler.d.ts +52 -0
- package/dist/lib/styles/compiler.d.ts.map +1 -0
- package/dist/lib/styles/compiler.js +272 -0
- package/dist/lib/styles/emit.d.ts +25 -0
- package/dist/lib/styles/emit.d.ts.map +1 -0
- package/dist/lib/styles/emit.js +78 -0
- package/dist/lib/styles/resolve.d.ts +48 -0
- package/dist/lib/styles/resolve.d.ts.map +1 -0
- package/dist/lib/styles/resolve.js +188 -0
- package/dist/lib/styles/transform.d.ts +47 -0
- package/dist/lib/styles/transform.d.ts.map +1 -0
- package/dist/lib/styles/transform.js +131 -0
- package/dist/lib/target.d.ts +21 -0
- package/dist/lib/target.d.ts.map +1 -0
- package/dist/lib/target.js +127 -0
- package/dist/lib/watch.d.ts +22 -0
- package/dist/lib/watch.d.ts.map +1 -0
- package/dist/lib/watch.js +96 -0
- package/package.json +55 -12
- package/src/assets.ts +2 -0
- package/src/lib/access.ts +24 -0
- package/src/lib/asset-server.ts +537 -0
- package/src/lib/compilation-error.ts +61 -0
- package/src/lib/file-matcher.ts +63 -0
- package/src/lib/fingerprint.ts +65 -0
- package/src/lib/module-store.ts +340 -0
- package/src/lib/paths.ts +66 -0
- package/src/lib/routes.ts +164 -0
- package/src/lib/scripts/cjs-check.ts +476 -0
- package/src/lib/scripts/compiler.ts +640 -0
- package/src/lib/scripts/emit.ts +122 -0
- package/src/lib/scripts/resolve.ts +433 -0
- package/src/lib/scripts/transform.ts +609 -0
- package/src/lib/source-maps.ts +75 -0
- package/src/lib/styles/compiler.ts +400 -0
- package/src/lib/styles/emit.ts +137 -0
- package/src/lib/styles/resolve.ts +316 -0
- package/src/lib/styles/transform.ts +226 -0
- package/src/lib/target.ts +196 -0
- package/src/lib/watch.ts +136 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { getFilePathDirectory } from "./paths.js";
|
|
2
|
+
export function createModuleStore(options = {}) {
|
|
3
|
+
let recordsByIdentityPath = new Map();
|
|
4
|
+
let recordsByTrackedFile = new Map();
|
|
5
|
+
let watchDirectoryRefCountByPath = new Map();
|
|
6
|
+
return {
|
|
7
|
+
get(identityPath) {
|
|
8
|
+
let existing = recordsByIdentityPath.get(identityPath);
|
|
9
|
+
if (existing)
|
|
10
|
+
return existing;
|
|
11
|
+
let record = {
|
|
12
|
+
identityPath,
|
|
13
|
+
invalidationVersion: 0,
|
|
14
|
+
trackedFiles: new Set(),
|
|
15
|
+
trackedDirectories: new Set(),
|
|
16
|
+
};
|
|
17
|
+
recordsByIdentityPath.set(identityPath, record);
|
|
18
|
+
return record;
|
|
19
|
+
},
|
|
20
|
+
clearTransformed(identityPath, tracking) {
|
|
21
|
+
let record = getOrCreateMutableRecord(identityPath);
|
|
22
|
+
record.transformed = undefined;
|
|
23
|
+
record.resolved = undefined;
|
|
24
|
+
record.emitted = undefined;
|
|
25
|
+
record.emittedSnapshot = undefined;
|
|
26
|
+
record.staleEmitted = undefined;
|
|
27
|
+
record.staleEmittedSnapshot = undefined;
|
|
28
|
+
setTracking(record, tracking);
|
|
29
|
+
},
|
|
30
|
+
setTransformed(identityPath, transformed, tracking) {
|
|
31
|
+
let record = getOrCreateMutableRecord(identityPath);
|
|
32
|
+
record.transformed = transformed;
|
|
33
|
+
record.resolved = undefined;
|
|
34
|
+
record.emitted = undefined;
|
|
35
|
+
record.emittedSnapshot = undefined;
|
|
36
|
+
record.staleEmitted = undefined;
|
|
37
|
+
record.staleEmittedSnapshot = undefined;
|
|
38
|
+
setTracking(record, tracking);
|
|
39
|
+
},
|
|
40
|
+
setResolved(identityPath, resolved, tracking) {
|
|
41
|
+
let record = getOrCreateMutableRecord(identityPath);
|
|
42
|
+
record.resolved = resolved;
|
|
43
|
+
record.emitted = undefined;
|
|
44
|
+
record.emittedSnapshot = undefined;
|
|
45
|
+
record.staleEmitted = undefined;
|
|
46
|
+
record.staleEmittedSnapshot = undefined;
|
|
47
|
+
setTracking(record, tracking);
|
|
48
|
+
},
|
|
49
|
+
clearResolved(identityPath, tracking) {
|
|
50
|
+
let record = getOrCreateMutableRecord(identityPath);
|
|
51
|
+
record.resolved = undefined;
|
|
52
|
+
record.emitted = undefined;
|
|
53
|
+
record.emittedSnapshot = undefined;
|
|
54
|
+
record.staleEmitted = undefined;
|
|
55
|
+
record.staleEmittedSnapshot = undefined;
|
|
56
|
+
setTracking(record, tracking);
|
|
57
|
+
},
|
|
58
|
+
setEmitted(identityPath, emitted, snapshot) {
|
|
59
|
+
let record = getOrCreateMutableRecord(identityPath);
|
|
60
|
+
record.emitted = emitted;
|
|
61
|
+
record.emittedSnapshot = snapshot ?? undefined;
|
|
62
|
+
record.staleEmitted = undefined;
|
|
63
|
+
record.staleEmittedSnapshot = undefined;
|
|
64
|
+
},
|
|
65
|
+
invalidateForFileEvent(filePath, event) {
|
|
66
|
+
let affected = new Set(recordsByTrackedFile.get(filePath) ?? []);
|
|
67
|
+
if (event !== 'change') {
|
|
68
|
+
for (let record of recordsByIdentityPath.values()) {
|
|
69
|
+
if (matchesTrackedDirectory(record.trackedDirectories, filePath)) {
|
|
70
|
+
affected.add(record.identityPath);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (let identityPath of affected) {
|
|
75
|
+
let record = recordsByIdentityPath.get(identityPath);
|
|
76
|
+
if (record)
|
|
77
|
+
invalidateRecord(record, { retainStale: event === 'change' });
|
|
78
|
+
}
|
|
79
|
+
if (event === 'unlink') {
|
|
80
|
+
let deletedRecord = recordsByIdentityPath.get(filePath);
|
|
81
|
+
if (deletedRecord) {
|
|
82
|
+
clearTracking(deletedRecord);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
invalidateAll() {
|
|
87
|
+
for (let record of recordsByIdentityPath.values()) {
|
|
88
|
+
invalidateRecord(record, { retainStale: false });
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
function getOrCreateMutableRecord(identityPath) {
|
|
93
|
+
let existing = recordsByIdentityPath.get(identityPath);
|
|
94
|
+
if (existing)
|
|
95
|
+
return existing;
|
|
96
|
+
let record = {
|
|
97
|
+
identityPath,
|
|
98
|
+
invalidationVersion: 0,
|
|
99
|
+
trackedFiles: new Set(),
|
|
100
|
+
trackedDirectories: new Set(),
|
|
101
|
+
};
|
|
102
|
+
recordsByIdentityPath.set(identityPath, record);
|
|
103
|
+
return record;
|
|
104
|
+
}
|
|
105
|
+
function invalidateRecord(record, options) {
|
|
106
|
+
if (!options.retainStale) {
|
|
107
|
+
record.staleEmitted = undefined;
|
|
108
|
+
record.staleEmittedSnapshot = undefined;
|
|
109
|
+
}
|
|
110
|
+
else if (record.emitted && record.emittedSnapshot) {
|
|
111
|
+
record.staleEmitted = record.emitted;
|
|
112
|
+
record.staleEmittedSnapshot = record.emittedSnapshot;
|
|
113
|
+
}
|
|
114
|
+
else if (!record.staleEmitted || !record.staleEmittedSnapshot) {
|
|
115
|
+
record.staleEmitted = undefined;
|
|
116
|
+
record.staleEmittedSnapshot = undefined;
|
|
117
|
+
}
|
|
118
|
+
record.emitted = undefined;
|
|
119
|
+
record.emittedSnapshot = undefined;
|
|
120
|
+
record.resolved = undefined;
|
|
121
|
+
record.transformed = undefined;
|
|
122
|
+
record.invalidationVersion += 1;
|
|
123
|
+
}
|
|
124
|
+
function setTracking(record, tracking) {
|
|
125
|
+
let previousWatchedDirectories = getWatchedDirectories(record);
|
|
126
|
+
removeIndexes(record);
|
|
127
|
+
let normalizedTracking = mergeTracking(tracking);
|
|
128
|
+
record.trackedFiles = normalizedTracking.trackedFiles;
|
|
129
|
+
record.trackedDirectories = normalizedTracking.trackedDirectories;
|
|
130
|
+
for (let trackedFile of record.trackedFiles) {
|
|
131
|
+
addToIndexedSet(recordsByTrackedFile, trackedFile, record.identityPath);
|
|
132
|
+
}
|
|
133
|
+
let nextWatchedDirectories = getWatchedDirectories(record);
|
|
134
|
+
let delta = updateWatchDirectoryRefCounts(previousWatchedDirectories, nextWatchedDirectories);
|
|
135
|
+
emitWatchDirectoryDelta(delta);
|
|
136
|
+
}
|
|
137
|
+
function clearTracking(record) {
|
|
138
|
+
setTracking(record, []);
|
|
139
|
+
}
|
|
140
|
+
function removeIndexes(record) {
|
|
141
|
+
for (let trackedFile of record.trackedFiles) {
|
|
142
|
+
removeFromIndexedSet(recordsByTrackedFile, trackedFile, record.identityPath);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function updateWatchDirectoryRefCounts(previousWatchedDirectories, nextWatchedDirectories) {
|
|
146
|
+
let add = [];
|
|
147
|
+
let remove = [];
|
|
148
|
+
for (let directory of previousWatchedDirectories) {
|
|
149
|
+
if (nextWatchedDirectories.has(directory))
|
|
150
|
+
continue;
|
|
151
|
+
let previousCount = watchDirectoryRefCountByPath.get(directory);
|
|
152
|
+
if (!previousCount)
|
|
153
|
+
continue;
|
|
154
|
+
if (previousCount === 1) {
|
|
155
|
+
watchDirectoryRefCountByPath.delete(directory);
|
|
156
|
+
remove.push(directory);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
watchDirectoryRefCountByPath.set(directory, previousCount - 1);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
for (let directory of nextWatchedDirectories) {
|
|
163
|
+
if (previousWatchedDirectories.has(directory))
|
|
164
|
+
continue;
|
|
165
|
+
let previousCount = watchDirectoryRefCountByPath.get(directory) ?? 0;
|
|
166
|
+
watchDirectoryRefCountByPath.set(directory, previousCount + 1);
|
|
167
|
+
if (previousCount === 0) {
|
|
168
|
+
add.push(directory);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return { add, remove };
|
|
172
|
+
}
|
|
173
|
+
function emitWatchDirectoryDelta(delta) {
|
|
174
|
+
if (!options.onWatchDirectoriesChange)
|
|
175
|
+
return;
|
|
176
|
+
if (delta.add.length === 0 && delta.remove.length === 0)
|
|
177
|
+
return;
|
|
178
|
+
options.onWatchDirectoriesChange(delta);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function addToIndexedSet(map, key, value) {
|
|
182
|
+
let existing = map.get(key) ?? new Set();
|
|
183
|
+
existing.add(value);
|
|
184
|
+
map.set(key, existing);
|
|
185
|
+
}
|
|
186
|
+
function removeFromIndexedSet(map, key, value) {
|
|
187
|
+
let existing = map.get(key);
|
|
188
|
+
if (!existing)
|
|
189
|
+
return;
|
|
190
|
+
existing.delete(value);
|
|
191
|
+
if (existing.size === 0) {
|
|
192
|
+
map.delete(key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function matchesTrackedDirectory(trackedDirectories, filePath) {
|
|
196
|
+
for (let trackedDirectory of trackedDirectories) {
|
|
197
|
+
if (filePath === trackedDirectory || filePath.startsWith(`${trackedDirectory}/`))
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
function normalizeTrackedDirectory(trackedDirectory) {
|
|
203
|
+
return trackedDirectory.replace(/\/+$/, '') || '/';
|
|
204
|
+
}
|
|
205
|
+
function mergeTracking(tracking) {
|
|
206
|
+
let trackedFiles = new Set();
|
|
207
|
+
let trackedDirectories = new Set();
|
|
208
|
+
for (let fragment of tracking) {
|
|
209
|
+
for (let trackedFile of fragment.trackedFiles) {
|
|
210
|
+
trackedFiles.add(trackedFile);
|
|
211
|
+
}
|
|
212
|
+
for (let trackedDirectory of fragment.trackedDirectories ?? []) {
|
|
213
|
+
trackedDirectories.add(normalizeTrackedDirectory(trackedDirectory));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
trackedFiles,
|
|
218
|
+
trackedDirectories,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function getWatchedDirectories(record) {
|
|
222
|
+
let watchedDirectories = new Set();
|
|
223
|
+
for (let trackedFile of record.trackedFiles) {
|
|
224
|
+
watchedDirectories.add(getFilePathDirectory(trackedFile));
|
|
225
|
+
}
|
|
226
|
+
for (let trackedDirectory of record.trackedDirectories) {
|
|
227
|
+
watchedDirectories.add(trackedDirectory);
|
|
228
|
+
}
|
|
229
|
+
return watchedDirectories;
|
|
230
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function normalizeWindowsPath(filePath: string): string;
|
|
2
|
+
export declare function normalizePathname(pathname: string): string;
|
|
3
|
+
export declare function isAbsoluteFilePath(filePath: string): boolean;
|
|
4
|
+
export declare function normalizeFilePath(filePath: string): string;
|
|
5
|
+
export declare function resolveFilePath(rootDir: string, filePath: string): string;
|
|
6
|
+
export declare function getFilePathDirectory(filePath: string): string;
|
|
7
|
+
export declare function getFilePathBaseName(filePath: string): string;
|
|
8
|
+
//# sourceMappingURL=paths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/lib/paths.ts"],"names":[],"mappings":"AAKA,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAI7D;AAED,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAQ1D;AAED,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAG5D;AAED,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAmB1D;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMzE;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE5D"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
const windowsDriveLetterRE = /^[A-Za-z]:\//;
|
|
3
|
+
const uncPrefixRE = /^\/\/[^/]+\/[^/]+/;
|
|
4
|
+
export function normalizeWindowsPath(filePath) {
|
|
5
|
+
return filePath
|
|
6
|
+
.replace(/\\/g, '/')
|
|
7
|
+
.replace(windowsDriveLetterRE, (prefix) => `${prefix[0].toUpperCase()}${prefix.slice(1)}`);
|
|
8
|
+
}
|
|
9
|
+
export function normalizePathname(pathname) {
|
|
10
|
+
let normalized = path.posix.normalize(normalizeWindowsPath(pathname));
|
|
11
|
+
if (!normalized.startsWith('/')) {
|
|
12
|
+
normalized = `/${normalized}`;
|
|
13
|
+
}
|
|
14
|
+
return normalized;
|
|
15
|
+
}
|
|
16
|
+
export function isAbsoluteFilePath(filePath) {
|
|
17
|
+
let normalized = normalizeWindowsPath(filePath);
|
|
18
|
+
return normalized.startsWith('/') || windowsDriveLetterRE.test(normalized);
|
|
19
|
+
}
|
|
20
|
+
export function normalizeFilePath(filePath) {
|
|
21
|
+
let normalized = normalizeWindowsPath(filePath);
|
|
22
|
+
let uncRoot = getUncRoot(normalized);
|
|
23
|
+
if (uncRoot) {
|
|
24
|
+
let remainder = normalized.slice(uncRoot.length);
|
|
25
|
+
let normalizedRemainder = path.posix.normalize(remainder || '/');
|
|
26
|
+
return `${uncRoot}${normalizedRemainder === '/' ? '' : normalizedRemainder}`;
|
|
27
|
+
}
|
|
28
|
+
if (windowsDriveLetterRE.test(normalized)) {
|
|
29
|
+
return path.posix.normalize(normalized);
|
|
30
|
+
}
|
|
31
|
+
if (normalized.startsWith('/')) {
|
|
32
|
+
return path.posix.normalize(normalized);
|
|
33
|
+
}
|
|
34
|
+
return path.posix.normalize(normalizeWindowsPath(path.resolve(normalized)));
|
|
35
|
+
}
|
|
36
|
+
export function resolveFilePath(rootDir, filePath) {
|
|
37
|
+
if (isAbsoluteFilePath(filePath)) {
|
|
38
|
+
return normalizeFilePath(filePath);
|
|
39
|
+
}
|
|
40
|
+
return normalizeFilePath(`${rootDir.replace(/\/+$/, '')}/${normalizeWindowsPath(filePath)}`);
|
|
41
|
+
}
|
|
42
|
+
export function getFilePathDirectory(filePath) {
|
|
43
|
+
return path.posix.dirname(normalizeWindowsPath(filePath));
|
|
44
|
+
}
|
|
45
|
+
export function getFilePathBaseName(filePath) {
|
|
46
|
+
return path.posix.basename(normalizeWindowsPath(filePath));
|
|
47
|
+
}
|
|
48
|
+
function getUncRoot(filePath) {
|
|
49
|
+
return filePath.startsWith('//') ? (filePath.match(uncPrefixRE)?.[0] ?? null) : null;
|
|
50
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface AssetRouteDefinition {
|
|
2
|
+
urlPattern: string;
|
|
3
|
+
filePattern: string;
|
|
4
|
+
}
|
|
5
|
+
export interface CompiledRoutes {
|
|
6
|
+
resolveUrlPathname(pathname: string): string | null;
|
|
7
|
+
toUrlPathname(filePath: string): string | null;
|
|
8
|
+
}
|
|
9
|
+
export declare function compileRoutes(options: {
|
|
10
|
+
fileMap: Readonly<Record<string, string>>;
|
|
11
|
+
rootDir: string;
|
|
12
|
+
}): CompiledRoutes;
|
|
13
|
+
//# sourceMappingURL=routes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/lib/routes.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;CACpB;AAQD,MAAM,WAAW,cAAc;IAC7B,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;IACnD,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAC/C;AAYD,wBAAgB,aAAa,CAAC,OAAO,EAAE;IACrC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACzC,OAAO,EAAE,MAAM,CAAA;CAChB,GAAG,cAAc,CA0CjB"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import { RoutePattern } from '@remix-run/route-pattern';
|
|
3
|
+
import { isAbsoluteFilePath, normalizeFilePath, normalizePathname, resolveFilePath, } from "./paths.js";
|
|
4
|
+
function normalizeFilePattern(pattern) {
|
|
5
|
+
if (isAbsoluteFilePath(pattern)) {
|
|
6
|
+
throw new Error(`File route patterns must be relative to the asset server root.\nPattern: ${pattern}`);
|
|
7
|
+
}
|
|
8
|
+
return normalizePathname(pattern);
|
|
9
|
+
}
|
|
10
|
+
export function compileRoutes(options) {
|
|
11
|
+
if (Object.keys(options.fileMap).length === 0) {
|
|
12
|
+
throw new Error('createAssetServer() requires at least one configured fileMap entry.');
|
|
13
|
+
}
|
|
14
|
+
let compiledRoutes = Object.entries(options.fileMap).map(([urlPattern, filePattern]) => compileRoute({
|
|
15
|
+
urlPattern,
|
|
16
|
+
filePattern,
|
|
17
|
+
}, { rootDir: options.rootDir }));
|
|
18
|
+
return {
|
|
19
|
+
resolveUrlPathname(pathname) {
|
|
20
|
+
let normalizedPathname = normalizePathname(pathname);
|
|
21
|
+
for (let route of compiledRoutes) {
|
|
22
|
+
let match = route.urlPattern.match(`http://remix.run${normalizedPathname}`);
|
|
23
|
+
if (!match)
|
|
24
|
+
continue;
|
|
25
|
+
let relativeFilePath = route.filePattern.href(match.params).replace(/^\/+/, '');
|
|
26
|
+
return resolveFilePath(route.rootDir, relativeFilePath);
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
},
|
|
30
|
+
toUrlPathname(filePath) {
|
|
31
|
+
let normalizedFilePath = normalizeFilePath(filePath);
|
|
32
|
+
for (let route of compiledRoutes) {
|
|
33
|
+
let relativeFilePath = getRelativeFilePath(normalizedFilePath, route.rootDir);
|
|
34
|
+
if (relativeFilePath === null)
|
|
35
|
+
continue;
|
|
36
|
+
let match = route.filePattern.ast.pathname.match(relativeFilePath);
|
|
37
|
+
if (!match)
|
|
38
|
+
continue;
|
|
39
|
+
return normalizePathname(route.urlPattern.href(getPathnameParams(route.filePattern, match)));
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function compileRoute(route, options) {
|
|
46
|
+
let urlPatternSource = normalizePathname(route.urlPattern);
|
|
47
|
+
let filePatternSource = normalizeFilePattern(route.filePattern);
|
|
48
|
+
let urlPattern = new RoutePattern(urlPatternSource);
|
|
49
|
+
let filePattern = new RoutePattern(filePatternSource);
|
|
50
|
+
validateNoUnnamedWildcards(urlPattern, 'URL');
|
|
51
|
+
validateNoUnnamedWildcards(filePattern, 'File');
|
|
52
|
+
validateRoutePatterns(urlPattern, filePattern);
|
|
53
|
+
return {
|
|
54
|
+
rootDir: normalizeFilePath(options.rootDir).replace(/\/+$/, ''),
|
|
55
|
+
urlPattern,
|
|
56
|
+
filePattern,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function getRelativeFilePath(filePath, rootDir) {
|
|
60
|
+
if (filePath[1] === ':' && rootDir[1] === ':' && filePath[0] !== rootDir[0])
|
|
61
|
+
return null;
|
|
62
|
+
return path.posix.relative(rootDir, filePath);
|
|
63
|
+
}
|
|
64
|
+
function getPathnameParams(pattern, match) {
|
|
65
|
+
let params = {};
|
|
66
|
+
for (let param of pattern.ast.pathname.params) {
|
|
67
|
+
if (param.name === '*')
|
|
68
|
+
continue;
|
|
69
|
+
params[param.name] = undefined;
|
|
70
|
+
}
|
|
71
|
+
for (let param of match) {
|
|
72
|
+
if (param.name === '*')
|
|
73
|
+
continue;
|
|
74
|
+
params[param.name] = param.value;
|
|
75
|
+
}
|
|
76
|
+
return params;
|
|
77
|
+
}
|
|
78
|
+
function validateRoutePatterns(urlPattern, filePattern) {
|
|
79
|
+
let urlParams = urlPattern.ast.pathname.params.map((param) => `${param.type}:${param.name}`);
|
|
80
|
+
let fileParams = filePattern.ast.pathname.params.map((param) => `${param.type}:${param.name}`);
|
|
81
|
+
if (urlParams.length !== fileParams.length) {
|
|
82
|
+
throw new Error(`Route patterns must have matching capture structure.\nURL: ${urlPattern}\nFile: ${filePattern}`);
|
|
83
|
+
}
|
|
84
|
+
for (let i = 0; i < urlParams.length; i++) {
|
|
85
|
+
if (urlParams[i] !== fileParams[i]) {
|
|
86
|
+
throw new Error(`Route patterns must have matching capture structure.\nURL: ${urlPattern}\nFile: ${filePattern}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function validateNoUnnamedWildcards(pattern, label) {
|
|
91
|
+
if (pattern.ast.pathname.params.some((param) => param.type === '*' && param.name === '*')) {
|
|
92
|
+
throw new Error(`${label} route patterns must use named wildcards for reversible mapping.\nPattern: ${pattern}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cjs-check.d.ts","sourceRoot":"","sources":["../../../src/lib/scripts/cjs-check.ts"],"names":[],"mappings":"AAoBA,wBAAgB,+BAA+B,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEvE;AAGD,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAalD"}
|