@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,196 @@
|
|
|
1
|
+
import type { Targets as LightningCssTargets } from 'lightningcss'
|
|
2
|
+
|
|
3
|
+
const browserTargetNames = [
|
|
4
|
+
'chrome',
|
|
5
|
+
'edge',
|
|
6
|
+
'firefox',
|
|
7
|
+
'ie',
|
|
8
|
+
'ios',
|
|
9
|
+
'opera',
|
|
10
|
+
'safari',
|
|
11
|
+
'samsung',
|
|
12
|
+
] as const
|
|
13
|
+
|
|
14
|
+
const browserTargetNameSet = new Set<string>(browserTargetNames)
|
|
15
|
+
|
|
16
|
+
const lightningCssTargetNameByBrowserTargetName = {
|
|
17
|
+
chrome: 'chrome',
|
|
18
|
+
edge: 'edge',
|
|
19
|
+
firefox: 'firefox',
|
|
20
|
+
ie: 'ie',
|
|
21
|
+
ios: 'ios_saf',
|
|
22
|
+
opera: 'opera',
|
|
23
|
+
safari: 'safari',
|
|
24
|
+
samsung: 'samsung',
|
|
25
|
+
} as const satisfies Record<BrowserTargetName, keyof LightningCssTargets>
|
|
26
|
+
|
|
27
|
+
export type AssetTargetVersion =
|
|
28
|
+
| `${number}`
|
|
29
|
+
| `${number}.${number}`
|
|
30
|
+
| `${number}.${number}.${number}`
|
|
31
|
+
export type BrowserTargetName = (typeof browserTargetNames)[number]
|
|
32
|
+
export type ResolvedScriptTarget = string[]
|
|
33
|
+
export type ResolvedStyleTarget = LightningCssTargets
|
|
34
|
+
|
|
35
|
+
export interface AssetTarget {
|
|
36
|
+
chrome?: AssetTargetVersion
|
|
37
|
+
edge?: AssetTargetVersion
|
|
38
|
+
firefox?: AssetTargetVersion
|
|
39
|
+
ie?: AssetTargetVersion
|
|
40
|
+
ios?: AssetTargetVersion
|
|
41
|
+
opera?: AssetTargetVersion
|
|
42
|
+
safari?: AssetTargetVersion
|
|
43
|
+
samsung?: AssetTargetVersion
|
|
44
|
+
es?: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type NormalizedAssetTarget = Partial<Record<BrowserTargetName, AssetTargetVersion>> & {
|
|
48
|
+
es?: AssetTarget['es']
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function resolveScriptTarget(
|
|
52
|
+
target: AssetTarget | undefined,
|
|
53
|
+
): ResolvedScriptTarget | undefined {
|
|
54
|
+
let resolvedTarget = normalizeScriptTargetObject(target, 'target')
|
|
55
|
+
if (!resolvedTarget) return undefined
|
|
56
|
+
|
|
57
|
+
let oxcTarget: ResolvedScriptTarget = []
|
|
58
|
+
if (resolvedTarget.es) {
|
|
59
|
+
oxcTarget.push(resolvedTarget.es)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (let browserTargetName of browserTargetNames) {
|
|
63
|
+
let version = resolvedTarget[browserTargetName]
|
|
64
|
+
if (version == null) continue
|
|
65
|
+
oxcTarget.push(`${browserTargetName}${version}`)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return oxcTarget
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function resolveStyleTarget(
|
|
72
|
+
target: AssetTarget | undefined,
|
|
73
|
+
): ResolvedStyleTarget | undefined {
|
|
74
|
+
let resolvedTarget = normalizeStyleTargetObject(target, 'target')
|
|
75
|
+
if (!resolvedTarget) return undefined
|
|
76
|
+
|
|
77
|
+
let lightningCssTargets: ResolvedStyleTarget = {}
|
|
78
|
+
for (let browserTargetName of browserTargetNames) {
|
|
79
|
+
let version = resolvedTarget[browserTargetName]
|
|
80
|
+
if (version == null) continue
|
|
81
|
+
lightningCssTargets[lightningCssTargetNameByBrowserTargetName[browserTargetName]] =
|
|
82
|
+
toLightningCssTargetVersion(version)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return lightningCssTargets
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function normalizeScriptTargetObject(
|
|
89
|
+
target: AssetTarget | undefined,
|
|
90
|
+
optionPath: string,
|
|
91
|
+
): NormalizedAssetTarget | undefined {
|
|
92
|
+
if (target == null) return undefined
|
|
93
|
+
if (!isPlainObject(target)) {
|
|
94
|
+
throw new TypeError(`${optionPath} must be an object`)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let normalizedTarget: NormalizedAssetTarget = {}
|
|
98
|
+
|
|
99
|
+
for (let [key, value] of Object.entries(target)) {
|
|
100
|
+
if (key === 'es') {
|
|
101
|
+
normalizedTarget.es = normalizeScriptTargetVersion(value, `${optionPath}.es`)
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!browserTargetNameSet.has(key)) {
|
|
106
|
+
throw new TypeError(`${optionPath}.${key} is not a supported target`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
normalizedTarget[key as BrowserTargetName] = normalizeBrowserTargetVersion(
|
|
110
|
+
value,
|
|
111
|
+
`${optionPath}.${key}`,
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return Object.keys(normalizedTarget).length === 0 ? undefined : normalizedTarget
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function normalizeStyleTargetObject(
|
|
119
|
+
target: AssetTarget | undefined,
|
|
120
|
+
optionPath: string,
|
|
121
|
+
): NormalizedAssetTarget | undefined {
|
|
122
|
+
if (target == null) return undefined
|
|
123
|
+
if (!isPlainObject(target)) {
|
|
124
|
+
throw new TypeError(`${optionPath} must be an object`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let normalizedTarget: NormalizedAssetTarget = {}
|
|
128
|
+
|
|
129
|
+
for (let [key, value] of Object.entries(target)) {
|
|
130
|
+
if (key === 'es') {
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!browserTargetNameSet.has(key)) {
|
|
135
|
+
throw new TypeError(`${optionPath}.${key} is not a supported target`)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
normalizedTarget[key as BrowserTargetName] = normalizeBrowserTargetVersion(
|
|
139
|
+
value,
|
|
140
|
+
`${optionPath}.${key}`,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return Object.keys(normalizedTarget).length === 0 ? undefined : normalizedTarget
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function normalizeScriptTargetVersion(value: unknown, optionPath: string): AssetTarget['es'] {
|
|
148
|
+
if (typeof value !== 'string') {
|
|
149
|
+
throw new TypeError(`${optionPath} must be a string`)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let normalizedValue = value.trim()
|
|
153
|
+
if (normalizedValue.length === 0) {
|
|
154
|
+
throw new TypeError(`${optionPath} must be a non-empty string`)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!/^\d+$/.test(normalizedValue)) {
|
|
158
|
+
throw new TypeError(`${optionPath} must use a single numeric year like "2020"`)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!/^\d{4}$/.test(normalizedValue) || Number(normalizedValue) < 2015) {
|
|
162
|
+
throw new TypeError(`${optionPath} must use a four-digit year of 2015 or higher`)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return `es${normalizedValue}`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function normalizeBrowserTargetVersion(value: unknown, optionPath: string): AssetTargetVersion {
|
|
169
|
+
if (typeof value !== 'string') {
|
|
170
|
+
throw new TypeError(`${optionPath} must be a string`)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (value.trim().length === 0) {
|
|
174
|
+
throw new TypeError(`${optionPath} must be a non-empty string`)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (!/^\d+(\.\d+){0,2}$/.test(value)) {
|
|
178
|
+
throw new TypeError(`${optionPath} must use "X", "X.Y", or "X.Y.Z" version format`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let segments = value.split('.').map(Number)
|
|
182
|
+
if (segments.some((segment) => segment > 255)) {
|
|
183
|
+
throw new TypeError(`${optionPath} must use version components between 0 and 255`)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return value as AssetTargetVersion
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function toLightningCssTargetVersion(version: AssetTargetVersion): number {
|
|
190
|
+
let [major, minor = 0, patch = 0] = version.split('.').map(Number)
|
|
191
|
+
return major * 65536 + minor * 256 + patch
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
195
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
196
|
+
}
|
package/src/lib/watch.ts
ADDED
|
@@ -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
|
+
}
|