@danielx/civet 0.5.93 → 0.6.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.
Binary file
package/dist/civet CHANGED
File without changes
@@ -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