@danielx/civet 0.5.92 → 0.5.94

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
@@ -265,7 +265,6 @@ cli = async function() {
265
265
  if (options.run) {
266
266
  options.js = true;
267
267
  options.inlineMap = true;
268
- require("../register.js");
269
268
  }
270
269
  if (options.repl) {
271
270
  return repl(options);
@@ -353,6 +352,7 @@ cli = async function() {
353
352
  }
354
353
  results.push(process.exit(child.status));
355
354
  } else {
355
+ require("../register.js");
356
356
  try {
357
357
  module.filename = await fs.realpath(filename);
358
358
  } catch (error1) {
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  @file esbuild plugin for Civet language
3
3
 
4
+ Simple zero config example
5
+
4
6
  @example
5
7
  ```javascript
6
8
  import esbuild from 'esbuild'
@@ -13,40 +15,131 @@ esbuild.build({
13
15
  ]
14
16
  }).catch(() => process.exit(1))
15
17
  ```
18
+
19
+ Chain civet output into another esbuild plugin, solid for example
20
+
21
+ @example
22
+ ```javascript
23
+ import esbuild from 'esbuild'
24
+ const { solidPlugin } = require('esbuild-plugin-solid');
25
+ import civetPlugin from '@danielx/civet/esbuild-plugin'
26
+
27
+ esbuild.build({
28
+ ...,
29
+ plugins: [
30
+ civetPlugin(
31
+ next: solidPlugin()
32
+ )
33
+ ]
34
+ }).catch(() => process.exit(1))
35
+ ```
16
36
  */
17
37
 
18
- const { readFile } = require('fs/promises')
38
+
39
+
40
+ ;
41
+
42
+ const { readFile, writeFile, mkdtemp, rmdir } = require('fs/promises')
19
43
  const path = require('path');
20
44
 
21
45
  // NOTE: this references the built version of the module, not the source
22
46
  const { compile } = require("../dist/main.js")
23
47
 
24
- module.exports = {
25
- name: 'civet',
26
- setup(build) {
27
- return build.onLoad({
28
- filter: /\.civet$/
29
- }, async function(args) {
30
- try {
31
- const source = await readFile(args.path, 'utf8')
32
- const filename = path.relative(process.cwd(), args.path)
33
- const compiled = compile(source, {
34
- filename,
35
- inlineMap: true,
36
- js: true
37
- })
38
-
39
- return {
40
- contents: compiled,
41
- }
42
- }
43
- catch (e) {
44
- return {
45
- errors: [{
46
- text: e.message
47
- }]
48
- }
48
+ // NOTE: this function is named civet so esbuild gets "civet" as the name of the plugin
49
+ function civet (options = {}) {
50
+ const {
51
+ filter=/\.civet$/,
52
+ inlineMap=true,
53
+ js=true,
54
+ next
55
+ } = options
56
+
57
+ let nextTransform
58
+ let tmpPath
59
+
60
+ if (next) {
61
+ next.setup({
62
+ onEnd() {; },
63
+ onStart() {; },
64
+ resolve() {; },
65
+ onResolve() {; },
66
+ initialOptions() {; },
67
+ esbuild() {; },
68
+ onLoad(_, handler) {
69
+ return nextTransform = handler
49
70
  }
50
71
  })
51
72
  }
73
+
74
+ return {
75
+ name: "civet",
76
+
77
+ setup(build) {
78
+
79
+ build.onStart(async function() {
80
+ if (next) {
81
+ const { tmpdir } = require('os')
82
+ tmpPath = await mkdtemp(path.join(tmpdir(), "civet-"))
83
+ }
84
+ return
85
+ })
86
+
87
+ build.onEnd(async function() {
88
+ if (tmpPath) {
89
+ await rmdir(tmpPath, { recursive: true })
90
+ }
91
+ return
92
+ })
93
+
94
+ return build.onLoad({ filter }, async function(args) {
95
+ try {
96
+ const source = await readFile(args.path, 'utf8')
97
+ const filename = path.relative(process.cwd(), args.path)
98
+ const compiled = compile(source, {
99
+ filename,
100
+ inlineMap,
101
+ js
102
+ })
103
+
104
+ if (next && tmpPath) {
105
+ const outputFileName = filename + js ? '.jsx' : '.tsx'
106
+ const outputFilePath = path.join(tmpPath, outputFileName)
107
+
108
+ // I'd prefer not to use temp files but I can't find a way to pass a stream to fs.readFile which is what
109
+ // most esbuild plugins use
110
+ await writeFile(outputFilePath, compiled)
111
+ return await nextTransform({
112
+ ...args,
113
+ path: outputFilePath
114
+ })
115
+ }
116
+
117
+ return {
118
+ contents: compiled,
119
+ }
120
+ }
121
+ catch (e) {
122
+ return {
123
+ errors: [{
124
+ text: e.message,
125
+ location: {
126
+ file: args.path,
127
+ namespace: args.namespace,
128
+ line: e.line,
129
+ column: e.column,
130
+ },
131
+ detail: e
132
+ }]
133
+ }
134
+ }
135
+ })
136
+ }
137
+ }
52
138
  }
139
+
140
+ const defaultPlugin = civet()
141
+
142
+ // Default zero-config plugin
143
+ civet.setup = defaultPlugin.setup
144
+
145
+ module.exports = civet
package/dist/esm.mjs CHANGED
@@ -44,7 +44,7 @@ function ensureRegister () {
44
44
  let path = pathOrUrl
45
45
  // If it's a file URL, convert to local path
46
46
  // I could not find a way to handle non-URLs except to swallow an error
47
- if (path.startsWith('file://')) {
47
+ if (path.startsWith('file:')) {
48
48
  try {
49
49
  path = fileURLToPath(path)
50
50
  } catch(e) {}
@@ -85,10 +85,11 @@ export async function load(url, context, next) {
85
85
  js: true,
86
86
  })
87
87
 
88
- const transpiledPath = url + ".tsx"
88
+ // NOTE: Append .tsx to URL so ts-node treats as TypeScript
89
+ const transpiledUrl = url + ".tsx"
89
90
 
90
91
  // NOTE: Assuming ts-node hook follows load hook
91
- const result = await next(transpiledPath, {
92
+ const result = await next(transpiledUrl, {
92
93
  // ts-node won't transpile unless this is module
93
94
  // can't use commonjs since we don't rewrite imports
94
95
  format: "module",
@@ -99,9 +100,13 @@ export async function load(url, context, next) {
99
100
  // NOTE: we must install our source map support after ts-node does to take priority
100
101
  ensureRegister()
101
102
 
103
+ // Remove .tsx extension for final URL
104
+ result.responseURL = (result.responseURL ?? transpiledUrl)
105
+ .replace(/.tsx$/, '')
106
+
102
107
  // parse source map from downstream (ts-node) result
103
108
  // compose with civet source map
104
- result.source = SourceMap.remap(result.source, sourceMap, url, "")
109
+ result.source = SourceMap.remap(result.source, sourceMap, url, result.responseURL)
105
110
  // NOTE: This path needs to match what ts-node uses so we can override the source map
106
111
  outputCache.set(normalizeSlashes(path), result.source)
107
112