-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
81 lines (66 loc) · 2.06 KB
/
index.coffee
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
78
79
80
81
#!/usr/bin/env coffee
> @u6x/jsext/jsext.js
fs > existsSync readFileSync
# import { createRequire } from "module"
import { dirname, normalize, extname, join, resolve as resolvePath } from "path"
import { cwd } from "process"
import { fileURLToPath, pathToFileURL } from "url"
import CoffeeScript from "coffeescript"
import {coffee_plus} from 'coffee_plus'
compile = coffee_plus(CoffeeScript)
baseURL = pathToFileURL("#{cwd}/").href
not_coffee = (specifier)=>
specifier.slice(specifier.lastIndexOf(".") + 1) != 'coffee'
export resolve = (specifier, context, defaultResolve) =>
{ parentURL = baseURL } = context
if not_coffee(specifier)
:$
loop
if parentURL.startsWith('file://')
for prefix from ['./','../']
if specifier.startsWith prefix
file = specifier+'.coffee'
fp = normalize join dirname(parentURL[7..]),file
if existsSync fp
specifier = fp
break $
return jsext(specifier,context,defaultResolve)
{
shortCircuit: true,
url: new URL(specifier, parentURL).href
}
COMMONJS = {
format:'commonjs'
shortCircuit:true
}
export load = (url, context, defaultLoad)=>
if url.endsWith('.node')
return COMMONJS
if not_coffee(url)
return defaultLoad(url, context, defaultLoad)
format = getPackageType(fileURLToPath url)
if format == "commonjs"
return COMMONJS
{ source: rawSource } = await defaultLoad(url, { format })
transformedSource = compile(rawSource.toString(), {
bare: true,
filename: url,
inlineMap: true,
})
return {
format
shortCircuit: true
source: transformedSource,
}
getPackageType = (url) =>
isFilePath = ["js", "mjs", "coffee"].includes(extname(url)[1..])
dir = if isFilePath then dirname(url) else url
packagePath = resolvePath(dir, "package.json")
try
{type} = JSON.parse readFileSync(packagePath, { encoding: "utf8" })
catch err
if err?.code != "ENOENT"
console.error(err)
if type
return type
return dir.length > 1 and getPackageType(resolvePath(dir, ".."))