@danielx/civet 0.2.16 → 0.3.2

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.
File without changes
package/dist/esm.mjs ADDED
@@ -0,0 +1,61 @@
1
+ import { readFileSync } from 'fs';
2
+ import { createRequire } from 'module';
3
+ import { pathToFileURL, fileURLToPath } from 'url';
4
+
5
+ //@ts-ignore Note: this is the compiled name
6
+ import { compile } from "./main.js";
7
+
8
+ const baseURL = pathToFileURL(process.cwd() + '/').href;
9
+ const extensionsRegex = /\.civet$/;
10
+
11
+ export function resolve(specifier, context, next) {
12
+ const { parentURL = baseURL } = context;
13
+
14
+ if (extensionsRegex.test(specifier)) {
15
+ return {
16
+ shortCircuit: true,
17
+ format: "civet",
18
+ url: new URL(specifier, parentURL).href,
19
+ };
20
+ };
21
+
22
+ // Let Node.js handle all other specifiers.
23
+ return next(specifier, context);
24
+ };
25
+
26
+ export function load(url, context, next) {
27
+ if (context.format === "civet") {
28
+ const path = fileURLToPath(url);
29
+ const source = readFileSync(path, "utf8");
30
+ const tsSource = compile(source, {
31
+ filename: path,
32
+ });
33
+
34
+ // NOTE: Assuming ts-node hook follows load hook
35
+ // NOTE: This causes .civet files to show up as .ts in ts-node error reporting (TODO: May be able to add a sourcemapping)
36
+ return next(url.replace(extensionsRegex, ".ts"), {
37
+ // ts-node won't transpile unless this is module
38
+ // can't use commonjs since we don't rewrite imports
39
+ format: "module",
40
+ // NOTE: Setting the source in the context makes it available when ts-node uses defaultLoad
41
+ source: tsSource,
42
+ });
43
+ };
44
+
45
+ // Let Node.js handle all other URLs.
46
+ return next(url, context);
47
+ };
48
+
49
+
50
+ // commonjs hook
51
+ const require = createRequire(import.meta.url);
52
+ require.extensions[".civet"] = function(m, filename) {
53
+ const source = readFileSync(filename, "utf8");
54
+ const code = compile(source, {
55
+ filename,
56
+ js: true,
57
+ });
58
+
59
+ //@ts-ignore TODO: Figure out how to load types from inculde folders in Civet LSP
60
+ m._compile(code, filename);
61
+ };