@danielx/civet 0.3.16 → 0.4.1-8.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.
@@ -1,28 +1,35 @@
1
- const { readFile } = require('fs/promises');
2
- const path = require('path');
3
- // This is the path to the file when both are in /dist
4
- const { compile } = require("./main.js");
1
+ const { readFile } = require('fs/promises')
2
+ const path = require('path')
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
- build.onLoad({
10
- filter: /\.civet$/,
11
- }, function(args) {
12
- return readFile(args.path, 'utf8')
13
- .then(function(source) {
14
- const filename = path.relative(process.cwd(), args.path);
10
+ return build.onLoad({
11
+ filter: /\.civet$/
12
+ }, async function(args) {
13
+ try {
14
+ const source = await readFile(args.path, 'utf8')
15
+ const filename = path.relative(process.cwd(), args.path)
16
+ const compiled = compile(source, {
17
+ filename,
18
+ inlineMap: true,
19
+ js: true
20
+ })
21
+
15
22
  return {
16
- contents: compile(source, { filename, js: true }),
17
- };
18
- })
19
- .catch(function(e) {
23
+ contents: compiled,
24
+ }
25
+ }
26
+ catch (e) {
20
27
  return {
21
28
  errors: [{
22
- text: e.message,
29
+ text: e.message
23
30
  }],
24
- };
25
- });
26
- });
31
+ }
32
+ }
33
+ })
27
34
  },
28
- };
35
+ }
package/dist/esm.mjs CHANGED
@@ -1,61 +1,126 @@
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
7
5
 
8
- const baseURL = pathToFileURL(process.cwd() + '/').href;
9
- const extensionsRegex = /\.civet$/;
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
22
+
23
+ const baseURL = pathToFileURL(process.cwd() + '/').href
24
+ const extensionsRegex = /\.civet$/
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
+ function ensureRegister () {
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(e) {}
51
+ }
52
+ path = normalizeSlashes(path)
53
+
54
+ return outputCache.get(path)
55
+ },
56
+ }
57
+
58
+ sourceMapSupport.install(installation)
59
+
60
+ return registered = true
61
+ }
10
62
 
11
63
  export function resolve(specifier, context, next) {
12
- const { parentURL = baseURL } = context;
64
+ const { parentURL = baseURL } = context
13
65
 
14
66
  if (extensionsRegex.test(specifier)) {
15
67
  return {
16
68
  shortCircuit: true,
17
69
  format: "civet",
18
70
  url: new URL(specifier, parentURL).href,
19
- };
20
- };
71
+ }
72
+ }
21
73
 
22
74
  // Let Node.js handle all other specifiers.
23
- return next(specifier, context);
24
- };
75
+ return next(specifier, context)
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
- const path = fileURLToPath(url);
29
- const source = readFileSync(path, "utf8");
30
- const tsSource = compile(source, {
80
+ const path = fileURLToPath(url)
81
+ const source = readFileSync(path, "utf8")
82
+ const {code: tsSource, sourceMap} = compile(source, {
31
83
  filename: path,
32
- });
84
+ sourceMap: true,
85
+ })
86
+
87
+ const transpiledPath = url + ".ts"
33
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
- });
43
- };
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)
44
106
 
45
- // Let Node.js handle all other URLs.
46
- return next(url, context);
47
- };
107
+ return result
108
+ }
48
109
 
110
+ // Other URLs continue unchanged.
111
+ return next(url, context)
112
+ }
49
113
 
50
114
  // commonjs hook
51
- const require = createRequire(import.meta.url);
115
+ const require = createRequire(import.meta.url)
52
116
  require.extensions[".civet"] = function(m, filename) {
53
- const source = readFileSync(filename, "utf8");
117
+ const source = readFileSync(filename, "utf8")
54
118
  const code = compile(source, {
55
119
  filename,
56
- js: true,
57
- });
120
+ inlineMap: true,
121
+ js: true
122
+ })
58
123
 
59
124
  //@ts-ignore TODO: Figure out how to load types from inculde folders in Civet LSP
60
- m._compile(code, filename);
61
- };
125
+ return m._compile(code, filename)
126
+ }