-
-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathisScriptFile.ts
More file actions
77 lines (68 loc) · 2.06 KB
/
isScriptFile.ts
File metadata and controls
77 lines (68 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
export { isScriptFile }
export { isPlainScriptFile }
export { isPlainJavaScriptFile }
export { isTemplateFile }
export { scriptFileExtensionList }
export { scriptFileExtensionPattern }
// We can't use a RegExp:
// - Needs to work with Micromatch: https://github.com/micromatch/micromatch because:
// - Vite's `import.meta.glob()` uses Micromatch
// - We need this to be a allowlist because:
// - A pattern `*([a-zA-Z0-9]` doesn't work.
// - Because of ReScript: `.res` are ReScript source files which need to be ignored. (The ReScript compiler generates `.js` files alongside `.res` files.)
// - Block listing doesn't work.
// - We cannot implement a blocklist with a glob pattern.
// - A post `import.meta.glob()` blocklist filtering doesn't work because Vite would still process the files (e.g. including them in the bundle).
// prettier-ignore
// biome-ignore format:
const extJs = [
'js',
'cjs',
'mjs',
] as const
// prettier-ignore
// biome-ignore format:
const extTs = [
'ts',
'cts',
'mts',
] as const
const extJsOrTs = [...extJs, ...extTs] as const
// prettier-ignore
// biome-ignore format:
const extJsx = [
'jsx',
'cjsx',
'mjsx',
] as const
// prettier-ignore
// biome-ignore format:
const extTsx = [
'tsx',
'ctsx',
'mtsx'
] as const
const extJsxOrTsx = [...extJsx, ...extTsx] as const
// prettier-ignore
// biome-ignore format:
const extTemplates = [
'vue',
'svelte',
'marko',
'md',
'mdx'
] as const
const scriptFileExtensionList = [...extJsOrTs, ...extJsxOrTsx, ...extTemplates] as const
const scriptFileExtensionPattern = '{' + scriptFileExtensionList.join(',') + '}'
function isScriptFile(filePath: string): boolean {
return scriptFileExtensionList.some((ext) => filePath.endsWith('.' + ext))
}
function isPlainScriptFile(filePath: string) {
return extJsOrTs.some((ext) => filePath.endsWith('.' + ext))
}
function isPlainJavaScriptFile(filePath: string) {
return extJs.some((ext) => filePath.endsWith('.' + ext))
}
function isTemplateFile(filePath: string) {
return extTemplates.some((ext) => filePath.endsWith('.' + ext))
}