@danielx/civet 0.3.15 → 0.4.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/civet CHANGED
@@ -1,20 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var ast, encoding, fs, generate, input, js, output, parse, prune;
3
+ var ast, compile, encoding, fs, generate, inlineMap, input, js, output, parse, prune;
4
4
  if (process.argv.includes("--version")) {
5
5
  process.stdout.write(require("../package.json").version + "\n");
6
6
  process.exit(0);
7
7
  }
8
- ({ parse, generate } = require("./main"));
8
+ ({ parse, compile, generate } = require("./main"));
9
9
  ({ prune } = generate);
10
10
  encoding = "utf8";
11
11
  fs = require("fs");
12
12
  input = fs.readFileSync(process.stdin.fd, encoding);
13
- ast = prune(parse(input));
13
+ process.argv.includes("--ast");
14
14
  js = process.argv.includes("--js");
15
- if (process.argv.includes("--ast")) {
15
+ inlineMap = process.argv.includes("--inline-map");
16
+ if (ast) {
17
+ ast = prune(parse(input));
16
18
  process.stdout.write(JSON.stringify(ast, null, 2));
17
- return;
19
+ process.exit(0);
18
20
  }
19
- output = generate(ast, { js });
21
+ output = compile(input, { js, inlineMap });
20
22
  process.stdout.write(output);
@@ -1,28 +1,29 @@
1
1
  const { readFile } = require('fs/promises');
2
2
  const path = require('path');
3
- // This is the path to the file when both are in /dist
4
- const { compile } = require("./main.js");
3
+
4
+ // NOTE: this references the built version of the module, not the source
5
+ const { compile } = require("../dist/main.js");
5
6
 
6
7
  module.exports = {
7
8
  name: 'civet',
8
9
  setup(build) {
9
10
  build.onLoad({
10
- filter: /\.civet/,
11
- }, function(args) {
12
- return readFile(args.path, 'utf8')
13
- .then(function(source) {
11
+ filter: /\.civet$/,
12
+ }, async function(args) {
13
+ try {
14
+ const source = await readFile(args.path, 'utf8');
14
15
  const filename = path.relative(process.cwd(), args.path);
15
16
  return {
16
17
  contents: compile(source, { filename, js: true }),
17
18
  };
18
- })
19
- .catch(function(e) {
19
+ }
20
+ catch (e) {
20
21
  return {
21
22
  errors: [{
22
23
  text: e.message,
23
24
  }],
24
25
  };
25
- });
26
+ };
26
27
  });
27
28
  },
28
29
  };
package/dist/esm.mjs CHANGED
@@ -1,13 +1,65 @@
1
- import { readFileSync } from 'fs';
2
- import { createRequire } from 'module';
3
- import { pathToFileURL, fileURLToPath } from 'url';
1
+ /**
2
+ @file Civet ESM loader.
4
3
 
5
- //@ts-ignore Note: this is the compiled name
6
- import { compile } from "./main.js";
4
+ Currently depends on ts-node esm loader being downstream
5
+
6
+ @example
7
+ ```bash
8
+ node --loader ts-node/esm --loader @danielx/civet/esm
9
+ ```
10
+ */
11
+
12
+ import { readFileSync } from "fs";
13
+ import { createRequire } from "module";
14
+ import { pathToFileURL, fileURLToPath } from "url";
15
+
16
+ import sourceMapSupport from "@cspotcode/source-map-support";
17
+
18
+ // NOTE: this references the built version of the module, not the source
19
+ import Civet from "../dist/main.js";
20
+ const { compile, util } = Civet;
21
+ const { SourceMap } = util;
7
22
 
8
23
  const baseURL = pathToFileURL(process.cwd() + '/').href;
9
24
  const extensionsRegex = /\.civet$/;
10
25
 
26
+ let registered = false;
27
+ const outputCache = new Map;
28
+
29
+ const directorySeparator = '/';
30
+ const backslashRegExp = /\\/g;
31
+ /**
32
+ * Replace backslashes with forward slashes.
33
+ */
34
+ function normalizeSlashes(value) {
35
+ return value.replace(backslashRegExp, directorySeparator);
36
+ };
37
+
38
+ const ensureRegister = function() {
39
+ if( registered) {return};
40
+
41
+ const installation = {
42
+ environment: 'node',
43
+ retrieveFile(pathOrUrl) {
44
+ let path = pathOrUrl;
45
+ // If it's a file URL, convert to local path
46
+ // I could not find a way to handle non-URLs except to swallow an error
47
+ if (path.startsWith('file://')) {
48
+ try {
49
+ path = fileURLToPath(path);
50
+ } catch {};
51
+ };
52
+ path = normalizeSlashes(path);
53
+
54
+ return outputCache.get(path);
55
+ },
56
+ };
57
+
58
+ sourceMapSupport.install(installation);
59
+
60
+ registered = true;
61
+ };
62
+
11
63
  export function resolve(specifier, context, next) {
12
64
  const { parentURL = baseURL } = context;
13
65
 
@@ -23,36 +75,49 @@ export function resolve(specifier, context, next) {
23
75
  return next(specifier, context);
24
76
  };
25
77
 
26
- export function load(url, context, next) {
78
+ export async function load(url, context, next) {
27
79
  if (context.format === "civet") {
28
80
  const path = fileURLToPath(url);
29
81
  const source = readFileSync(path, "utf8");
30
- const tsSource = compile(source, {
82
+ const {code: tsSource, sourceMap} = compile(source, {
31
83
  filename: path,
84
+ sourceMap: true,
32
85
  });
33
86
 
87
+ const transpiledPath = url + ".ts";
88
+
34
89
  // 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"), {
90
+ const result = await next(transpiledPath, {
37
91
  // ts-node won't transpile unless this is module
38
92
  // can't use commonjs since we don't rewrite imports
39
93
  format: "module",
40
94
  // NOTE: Setting the source in the context makes it available when ts-node uses defaultLoad
41
95
  source: tsSource,
42
96
  });
97
+
98
+ // NOTE: we must install our source map support after ts-node does to take priority
99
+ ensureRegister();
100
+
101
+ // parse source map from downstream (ts-node) result
102
+ // compose with civet source map
103
+ result.source = SourceMap.remap(result.source, sourceMap, url, "");
104
+ // NOTE: This path needs to match what ts-node uses so we can override the source map
105
+ outputCache.set(normalizeSlashes(path), result.source);
106
+
107
+ return result;
43
108
  };
44
109
 
45
- // Let Node.js handle all other URLs.
110
+ // Other URLs continue unchanged.
46
111
  return next(url, context);
47
112
  };
48
113
 
49
-
50
114
  // commonjs hook
51
115
  const require = createRequire(import.meta.url);
52
116
  require.extensions[".civet"] = function(m, filename) {
53
117
  const source = readFileSync(filename, "utf8");
54
118
  const code = compile(source, {
55
119
  filename,
120
+ inlineMap: true,
56
121
  js: true,
57
122
  });
58
123