@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.
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
- "version": "0.2.16",
3
+ "version": "0.3.2",
4
4
  "description": "CoffeeScript style syntax for TypeScript",
5
5
  "main": "dist/main.js",
6
+ "exports": {
7
+ ".": "./dist/main.js",
8
+ "./esm": "./dist/esm.mjs",
9
+ "./esbuild-plugin": "./dist/esbuild-plugin.js"
10
+ },
6
11
  "types": "dist/types.d.ts",
7
12
  "bin": {
8
13
  "civet": "dist/civet"
9
14
  },
10
15
  "files": [
11
16
  "dist/",
12
- "esbuild-plugin.js",
13
17
  "register.js",
14
18
  "register.mjs"
15
19
  ],
@@ -36,11 +40,14 @@
36
40
  },
37
41
  "mocha": {
38
42
  "extension": [
43
+ "civet",
39
44
  "coffee"
40
45
  ],
41
- "require": [
42
- "coffeescript/register",
43
- "@danielx/hera/register"
46
+ "loader": [
47
+ "ts-node/esm",
48
+ "./build/coffee-esm.mjs",
49
+ "./build/hera-esm.mjs",
50
+ "./dist/esm.mjs"
44
51
  ],
45
52
  "reporter": "dot",
46
53
  "recursive": true,
package/register.mjs CHANGED
@@ -1,10 +1,11 @@
1
+ import { readFileSync } from 'fs';
2
+ import { readFile } from 'fs/promises';
1
3
  import { createRequire } from 'module';
2
- import { pathToFileURL } from 'url';
4
+ import { pathToFileURL, fileURLToPath } from 'url';
3
5
 
4
6
  import { compile } from "./dist/main.js";
5
7
 
6
8
  const baseURL = pathToFileURL(process.cwd() + '/').href;
7
-
8
9
  const extensionsRegex = /\.civet$/;
9
10
 
10
11
  export async function resolve(specifier, context, next) {
@@ -13,7 +14,7 @@ export async function resolve(specifier, context, next) {
13
14
  if (extensionsRegex.test(specifier)) {
14
15
  return {
15
16
  shortCircuit: true,
16
- format: "module",
17
+ format: "civet",
17
18
  url: new URL(specifier, parentURL).href,
18
19
  };
19
20
  }
@@ -23,19 +24,33 @@ export async function resolve(specifier, context, next) {
23
24
  }
24
25
 
25
26
  export async function load(url, context, next) {
26
- if (extensionsRegex.test(url)) {
27
- const { source: rawSource } = await next(url, { format: "module" });
28
-
29
- return {
27
+ if (context.format === "civet") {
28
+ const path = fileURLToPath(url)
29
+ const source = await readFile(path, "utf8")
30
+ const tsSource = compile(source, { filename: path })
31
+
32
+ // NOTE: Assuming ts-node hook follows load hook
33
+ // NOTE: This causes .civet files to show up as .ts in ts-node error reporting (TODO: May be able to add a sourcemapping)
34
+ const result = await next(url.replace(extensionsRegex, ".ts"), {
35
+ // ts-node won't transpile unless this is module
36
+ // can't use commonjs since we don't rewrite imports
30
37
  format: "module",
31
- source: compile(rawSource.toString(), { js: true }),
32
- };
38
+ // NOTE: Setting the source in the context makes it available when ts-node uses defaultLoad
39
+ source: tsSource
40
+ });
41
+
42
+ return result
33
43
  }
34
44
 
35
45
  // Let Node.js handle all other URLs.
36
46
  return next(url, context);
37
47
  }
38
48
 
39
- // Also transform CommonJS files.
40
49
  const require = createRequire(import.meta.url);
41
- require("./register.js")
50
+ require.extensions[".civet"] = function (m, filename) {
51
+ // We end up here when being required from cjs
52
+ const source = readFileSync(filename, "utf8")
53
+ const code = compile(source, { filename, js: true })
54
+
55
+ m._compile(code, filename)
56
+ }