@danielx/civet 0.2.15 → 0.3.1

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/types.d.ts CHANGED
@@ -1,18 +1,37 @@
1
1
  declare module "@danielx/civet" {
2
- export type CivetAST = unknown;
2
+ export type CivetAST = unknown
3
3
  export type CompileOptions = {
4
4
  filename?: string
5
5
  js?: boolean
6
+ sourceMap?: boolean
6
7
  }
7
8
 
8
- export function compile(source: string, options?: CompileOptions): string
9
+ export type SourceMapping = [number] | [number, number, number, number]
10
+
11
+ export interface SourceMap {
12
+ updateSourceMap?(outputStr: string, inputPos: number): void
13
+ json(srcFileName: string, outFileName: string): unknown
14
+ data: {
15
+ lines: SourceMapping[][]
16
+ }
17
+ }
18
+
19
+ export function compile<T extends CompileOptions>(source: string, options?: T): T extends { sourceMap: true } ? {
20
+ code: string,
21
+ sourceMap: SourceMap,
22
+ } : string
9
23
  export function parse(source: string): CivetAST
10
24
  export function generate(ast: CivetAST, options?: CompileOptions): string
11
25
 
12
26
  const Civet: {
13
- compile: typeof compile;
14
- parse: typeof parse;
15
- generate: typeof generate;
27
+ compile: typeof compile
28
+ parse: typeof parse
29
+ generate: typeof generate
30
+ util: {
31
+ locationTable(input: string): number[]
32
+ lookupLineColumn(table: number[], pos: number): [number, number]
33
+ SourceMap(input: string): SourceMap
34
+ }
16
35
  }
17
36
 
18
37
  export default Civet;
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
- "version": "0.2.15",
3
+ "version": "0.3.1",
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"
@@ -13,6 +17,9 @@
13
17
  "register.js",
14
18
  "register.mjs"
15
19
  ],
20
+ "engines": {
21
+ "node": "^18.6.0 || ^16.17.0"
22
+ },
16
23
  "scripts": {
17
24
  "prepublishOnly": "yarn build && yarn test",
18
25
  "build": "bash ./build/build.sh",
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
+ }