@danielx/civet 0.2.14 → 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/README.md +6 -5
- package/dist/browser.js +693 -323
- package/dist/browser.js.map +3 -3
- package/dist/civet +14 -13
- package/dist/cli.js.map +4 -4
- package/dist/main.js +698 -328
- package/dist/types.d.ts +24 -5
- package/package.json +8 -1
- package/register.mjs +41 -13
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
|
|
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.
|
|
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"
|
|
@@ -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,40 +1,68 @@
|
|
|
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
|
-
|
|
10
|
+
const cache = new Map
|
|
11
|
+
|
|
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 {
|
|
17
|
+
shortCircuit: true,
|
|
15
18
|
format: "civet",
|
|
16
19
|
url: new URL(specifier, parentURL).href,
|
|
17
20
|
};
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
// Let Node.js handle all other specifiers.
|
|
21
|
-
return
|
|
24
|
+
return next(specifier, context);
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
export async function load(url, context,
|
|
25
|
-
if (
|
|
26
|
-
const
|
|
27
|
+
export async function load(url, context, next) {
|
|
28
|
+
if (context.format === "civet") {
|
|
29
|
+
const path = fileURLToPath(url)
|
|
30
|
+
const source = await readFile(path, "utf8")
|
|
31
|
+
const tsSource = compile(source)
|
|
27
32
|
|
|
28
|
-
|
|
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
|
|
29
38
|
format: "module",
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
32
53
|
}
|
|
33
54
|
|
|
34
55
|
// Let Node.js handle all other URLs.
|
|
35
|
-
return
|
|
56
|
+
return next(url, context);
|
|
36
57
|
}
|
|
37
58
|
|
|
38
|
-
//
|
|
59
|
+
// Cache our double transpiled sources (.civet -> .ts -> .js)
|
|
39
60
|
const require = createRequire(import.meta.url);
|
|
40
|
-
require
|
|
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
|
+
}
|