@danielx/civet 0.2.16 → 0.3.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/dist/main.js CHANGED
@@ -4077,6 +4077,7 @@ var require_util = __commonJS({
4077
4077
 
4078
4078
  // source/main.coffee
4079
4079
  var SourceMap;
4080
+ var defaultOptions;
4080
4081
  var gen;
4081
4082
  var parse;
4082
4083
  var prune;
@@ -4084,12 +4085,13 @@ var util;
4084
4085
  ({ parse } = require_parser());
4085
4086
  ({ prune } = gen = require_generate());
4086
4087
  ({ SourceMap } = util = require_util());
4088
+ defaultOptions = {};
4087
4089
  module.exports = {
4088
4090
  parse,
4089
- compile: function(src, options) {
4091
+ compile: function(src, options = defaultOptions) {
4090
4092
  var ast, code, sm;
4091
4093
  ast = prune(parse(src, {
4092
- filename: options != null ? options.filename : void 0
4094
+ filename: options.filename
4093
4095
  }));
4094
4096
  if (options.sourceMap) {
4095
4097
  sm = SourceMap(src);
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
- "version": "0.2.16",
3
+ "version": "0.3.0",
4
4
  "description": "CoffeeScript style syntax for TypeScript",
5
5
  "main": "dist/main.js",
6
+ "exports": {
7
+ ".": "./dist/main.js",
8
+ "./esm": "./register.mjs"
9
+ },
6
10
  "types": "dist/types.d.ts",
7
11
  "bin": {
8
12
  "civet": "dist/civet"
package/register.mjs CHANGED
@@ -1,19 +1,21 @@
1
+ import { readFile } from 'fs/promises';
1
2
  import { createRequire } from 'module';
2
- import { pathToFileURL } from 'url';
3
+ import { pathToFileURL, fileURLToPath } from 'url';
3
4
 
4
5
  import { compile } from "./dist/main.js";
5
6
 
6
7
  const baseURL = pathToFileURL(process.cwd() + '/').href;
7
-
8
8
  const extensionsRegex = /\.civet$/;
9
9
 
10
+ const cache = new Map
11
+
10
12
  export async function resolve(specifier, context, next) {
11
13
  const { parentURL = baseURL } = context;
12
14
 
13
15
  if (extensionsRegex.test(specifier)) {
14
16
  return {
15
17
  shortCircuit: true,
16
- format: "module",
18
+ format: "civet",
17
19
  url: new URL(specifier, parentURL).href,
18
20
  };
19
21
  }
@@ -23,19 +25,44 @@ export async function resolve(specifier, context, next) {
23
25
  }
24
26
 
25
27
  export async function load(url, context, next) {
26
- if (extensionsRegex.test(url)) {
27
- const { source: rawSource } = await next(url, { format: "module" });
28
+ if (context.format === "civet") {
29
+ const path = fileURLToPath(url)
30
+ const source = await readFile(path, "utf8")
31
+ const tsSource = compile(source)
28
32
 
29
- return {
33
+ // NOTE: Assuming ts-node hook follows load hook
34
+ // NOTE: This causes .civet files to show up as .ts in ts-node error reporting (TODO: May be able to add a sourcemapping)
35
+ const result = await next(url.replace(extensionsRegex, ".ts"), {
36
+ // ts-node won't transpile unless this is module
37
+ // can't use commonjs since we don't rewrite imports
30
38
  format: "module",
31
- source: compile(rawSource.toString(), { js: true }),
32
- };
39
+ // NOTE: Setting the source in the context makes it available when ts-node uses defaultLoad
40
+ source: tsSource
41
+ });
42
+
43
+ // NOTE: If I don't set the format to 'commonjs' then I get
44
+ // "ReferenceError: exports is not defined in ES module scope"
45
+ // setting the format to commonjs causes the require.extensions
46
+ // handler to be invoked. So we cache the ts-node transpilation
47
+ // result, hook into require.extensions, and return the result there.
48
+ // Hopefully node loaders simplify this in the future.
49
+ result.format = "commonjs"
50
+ cache.set(path, result.source)
51
+
52
+ return result
33
53
  }
34
54
 
35
55
  // Let Node.js handle all other URLs.
36
56
  return next(url, context);
37
57
  }
38
58
 
39
- // Also transform CommonJS files.
59
+ // Cache our double transpiled sources (.civet -> .ts -> .js)
40
60
  const require = createRequire(import.meta.url);
41
- require("./register.js")
61
+ require.extensions[".civet"] = function (m, filename) {
62
+ const code = cache.get(filename)
63
+ if (!code) throw new Error(`Code for ${filename} wasn't in transpiled cache.`)
64
+
65
+ m._compile(code, filename)
66
+ // Module should be cached after the first load so we can release the memory
67
+ cache.delete(filename)
68
+ }