bimba-cli 0.1.9 → 0.2.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/package.json +1 -1
- package/plugin.js +198 -193
package/package.json
CHANGED
package/plugin.js
CHANGED
|
@@ -1,193 +1,198 @@
|
|
|
1
|
-
import { plugin } from "bun";
|
|
2
|
-
import {theme} from './utils.js';
|
|
3
|
-
import * as compiler from 'imba/compiler'
|
|
4
|
-
import
|
|
5
|
-
import fs from 'fs'
|
|
6
|
-
import { Glob } from "bun";
|
|
7
|
-
import { unlink } from "node:fs/promises";
|
|
8
|
-
|
|
9
|
-
export const cache = process.cwd()
|
|
10
|
-
if (!fs.existsSync(cache)){ fs.mkdirSync(cache);}
|
|
11
|
-
|
|
12
|
-
// this should be reset from outside to get results of entrypoint building
|
|
13
|
-
export let stats = {
|
|
14
|
-
failed: 0,
|
|
15
|
-
compiled: 0,
|
|
16
|
-
cached: 0,
|
|
17
|
-
bundled: 0,
|
|
18
|
-
errors: 0
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const imbaPlugin = {
|
|
22
|
-
name: "imba",
|
|
23
|
-
async setup(build) {
|
|
24
|
-
|
|
25
|
-
// when there is import without file extension
|
|
26
|
-
build.onResolve({filter: /^.*[^.]{5}$/ }, ({ path, importer }) => {
|
|
27
|
-
|
|
28
|
-
let filename = path;
|
|
29
|
-
// resolve relative path
|
|
30
|
-
if (path.startsWith('.')) { filename =
|
|
31
|
-
|
|
32
|
-
// assume that the file is .js
|
|
33
|
-
try { return {path: Bun.resolveSync(filename + '.js', '.')}}
|
|
34
|
-
catch (error) {
|
|
35
|
-
// assume that the file is .mjs
|
|
36
|
-
try { return {path: Bun.resolveSync(filename + '.mjs', '.')}}
|
|
37
|
-
catch (error) {
|
|
38
|
-
// assume that the file is .cs
|
|
39
|
-
try { return {path: Bun.resolveSync(filename + '.cjs', '.')}}
|
|
40
|
-
catch (error) {
|
|
41
|
-
// assume that the file is .imba
|
|
42
|
-
try { return {path: Bun.resolveSync(filename + '.imba', '.')}}
|
|
43
|
-
catch (error) {
|
|
44
|
-
// if direct resolution failed
|
|
45
|
-
filename += '.imba';
|
|
46
|
-
|
|
47
|
-
// assume that the relative path should be resolved relative to importer
|
|
48
|
-
let fn =
|
|
49
|
-
if (fs.existsSync(fn)) return {path: fn};
|
|
50
|
-
// assume that the relative path should be resolved relative to node_modules
|
|
51
|
-
fn =
|
|
52
|
-
if (fs.existsSync(fn)) return {path: fn};
|
|
53
|
-
// assume that the relative path should be resolved relative to project root
|
|
54
|
-
fn =
|
|
55
|
-
if (fs.existsSync(fn)) return {path: fn};
|
|
56
|
-
|
|
57
|
-
// if the path still is unresolved throw error and leave the further resolution on Bun's resolver
|
|
58
|
-
if (error instanceof Error) {
|
|
59
|
-
throw new Error(error.message);
|
|
60
|
-
}
|
|
61
|
-
else
|
|
62
|
-
throw new Error('Could not resolve file: ' + path);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
// when an .imba file is imported...
|
|
70
|
-
build.onLoad({ filter: /\.imba$/ }, async ({ path }) => {
|
|
71
|
-
|
|
72
|
-
const f =
|
|
73
|
-
let contents = '';
|
|
74
|
-
|
|
75
|
-
// return the cached version if it exists
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
display.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
1
|
+
import { plugin } from "bun";
|
|
2
|
+
import {theme} from './utils.js';
|
|
3
|
+
import * as compiler from 'imba/compiler'
|
|
4
|
+
import {resolve, join, dirname, parse} from 'path'
|
|
5
|
+
import fs from 'fs'
|
|
6
|
+
import { Glob } from "bun";
|
|
7
|
+
import { unlink } from "node:fs/promises";
|
|
8
|
+
|
|
9
|
+
export const cache = join(process.cwd(),'.cache');
|
|
10
|
+
if (!fs.existsSync(cache)){ fs.mkdirSync(cache);}
|
|
11
|
+
|
|
12
|
+
// this should be reset from outside to get results of entrypoint building
|
|
13
|
+
export let stats = {
|
|
14
|
+
failed: 0,
|
|
15
|
+
compiled: 0,
|
|
16
|
+
cached: 0,
|
|
17
|
+
bundled: 0,
|
|
18
|
+
errors: 0
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const imbaPlugin = {
|
|
22
|
+
name: "imba",
|
|
23
|
+
async setup(build) {
|
|
24
|
+
|
|
25
|
+
// when there is import without file extension
|
|
26
|
+
build.onResolve({filter: /^.*[^.]{5}$/ }, ({ path, importer }) => {
|
|
27
|
+
|
|
28
|
+
let filename = path;
|
|
29
|
+
// resolve relative path
|
|
30
|
+
if (path.startsWith('.')) { filename = resolve(dirname(importer), filename) };
|
|
31
|
+
|
|
32
|
+
// assume that the file is .js
|
|
33
|
+
try { return {path: Bun.resolveSync(filename + '.js', '.')}}
|
|
34
|
+
catch (error) {
|
|
35
|
+
// assume that the file is .mjs
|
|
36
|
+
try { return {path: Bun.resolveSync(filename + '.mjs', '.')}}
|
|
37
|
+
catch (error) {
|
|
38
|
+
// assume that the file is .cs
|
|
39
|
+
try { return {path: Bun.resolveSync(filename + '.cjs', '.')}}
|
|
40
|
+
catch (error) {
|
|
41
|
+
// assume that the file is .imba
|
|
42
|
+
try { return {path: Bun.resolveSync(filename + '.imba', '.')}}
|
|
43
|
+
catch (error) {
|
|
44
|
+
// if direct resolution failed
|
|
45
|
+
filename += '.imba';
|
|
46
|
+
|
|
47
|
+
// assume that the relative path should be resolved relative to importer
|
|
48
|
+
let fn = resolve(dirname(importer), filename);
|
|
49
|
+
if (fs.existsSync(fn)) return {path: fn};
|
|
50
|
+
// assume that the relative path should be resolved relative to node_modules
|
|
51
|
+
fn = resolve('./node_modules', filename);
|
|
52
|
+
if (fs.existsSync(fn)) return {path: fn};
|
|
53
|
+
// assume that the relative path should be resolved relative to project root
|
|
54
|
+
fn = resolve(process.cwd(), filename);
|
|
55
|
+
if (fs.existsSync(fn)) return {path: fn};
|
|
56
|
+
|
|
57
|
+
// if the path still is unresolved throw error and leave the further resolution on Bun's resolver
|
|
58
|
+
if (error instanceof Error) {
|
|
59
|
+
throw new Error(error.message);
|
|
60
|
+
}
|
|
61
|
+
else
|
|
62
|
+
throw new Error('Could not resolve file: ' + path);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// when an .imba file is imported...
|
|
70
|
+
build.onLoad({ filter: /\.imba$/ }, async ({ path }) => {
|
|
71
|
+
|
|
72
|
+
const f = parse(path)
|
|
73
|
+
let contents = '';
|
|
74
|
+
|
|
75
|
+
// return the cached version if it exists
|
|
76
|
+
const path_hash = Bun.hash(path)
|
|
77
|
+
const time_hash = Bun.hash(fs.statSync(path).mtimeMs)
|
|
78
|
+
const cached = join(cache, path_hash + '_' + time_hash + '.js');
|
|
79
|
+
if (fs.existsSync(cached)) {
|
|
80
|
+
console.log('from cache')
|
|
81
|
+
stats.bundled++;
|
|
82
|
+
stats.cached++;
|
|
83
|
+
return {
|
|
84
|
+
contents: await Bun.file(cached).text(),
|
|
85
|
+
loader: "js",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// clear previous cached version
|
|
90
|
+
const glob = new Glob(path_hash + '_' + "*.js");
|
|
91
|
+
for await (const file of glob.scan(cache)) {
|
|
92
|
+
const prev_cached = join(cache, file);
|
|
93
|
+
if (fs.existsSync(prev_cached)) unlink(prev_cached);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// if no cached version read and compile it with the imba compiler
|
|
97
|
+
const file = await Bun.file(path).text();
|
|
98
|
+
const out = compiler.compile(file, {
|
|
99
|
+
sourcePath: path,
|
|
100
|
+
platform: 'browser'
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// print about file complitaion
|
|
104
|
+
console.write(theme.action("compiling: ") + theme.folder(f.dir + '/') + theme.filename(f.base) + " - ");
|
|
105
|
+
|
|
106
|
+
// the file has been successfully compiled
|
|
107
|
+
if (!out.errors || !out.errors.length) {
|
|
108
|
+
stats.bundled++;
|
|
109
|
+
stats.compiled++;
|
|
110
|
+
contents = out.js;
|
|
111
|
+
await Bun.write(cached, contents);
|
|
112
|
+
console.write(theme.success("cached" + "\n"));
|
|
113
|
+
}
|
|
114
|
+
// there were errors during compilation
|
|
115
|
+
else {
|
|
116
|
+
console.write(theme.failure(" fail ") + "\n");
|
|
117
|
+
stats.failed++;
|
|
118
|
+
for (let i = 0; i < out.errors.length; i++) {
|
|
119
|
+
if(out.errors[i]) printerr(out.errors[i]);
|
|
120
|
+
}
|
|
121
|
+
stats.errors++;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// and return the compiled source code as "js"
|
|
125
|
+
return {
|
|
126
|
+
contents,
|
|
127
|
+
loader: "js",
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
plugin(imbaPlugin);
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
// -------------------------------------------------------------------------------
|
|
137
|
+
// print pretty messages produced by the imba compiler
|
|
138
|
+
// -------------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
// print an error generated by the imba compiler
|
|
141
|
+
function printerr(err) {
|
|
142
|
+
|
|
143
|
+
// halper function to produce empty strings
|
|
144
|
+
const fill = (len = 0) => {return new Array(len + 1).join(' ')}
|
|
145
|
+
|
|
146
|
+
// gather the needed information from the compiler error
|
|
147
|
+
const snippet = err.toSnippet().split("\n");
|
|
148
|
+
const display = {
|
|
149
|
+
error: " " + err.message + " ",
|
|
150
|
+
outdent: fill(10),
|
|
151
|
+
source: snippet[1] + " ",
|
|
152
|
+
margin: " line " + err.range.start.line + " ",
|
|
153
|
+
errs: snippet[2].indexOf('^'),
|
|
154
|
+
erre: snippet[2].lastIndexOf('^') + 1,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// calculate parameters for priniting a message
|
|
158
|
+
const center = display.margin.length + display.errs + Math.floor((display.erre - display.errs) / 2);
|
|
159
|
+
const half = Math.ceil((display.error.length - 1) / 2);
|
|
160
|
+
const start = Math.max(0, center - half);
|
|
161
|
+
const end = start + display.error.length;
|
|
162
|
+
const total = Math.max(display.margin.length + display.source.length, end);
|
|
163
|
+
|
|
164
|
+
// print emtpy line
|
|
165
|
+
console.log('');
|
|
166
|
+
|
|
167
|
+
// print line with the error message
|
|
168
|
+
console.log(
|
|
169
|
+
display.outdent +
|
|
170
|
+
theme.margin(fill(Math.min(start, display.margin.length))) +
|
|
171
|
+
theme.code(fill(Math.max(0, start - display.margin.length))) +
|
|
172
|
+
theme.error(display.error) +
|
|
173
|
+
theme.margin(fill(Math.max(0, display.margin.length - end))) +
|
|
174
|
+
theme.code(fill(Math.min(total - display.margin.length, total - end)))
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// print line with the source code
|
|
178
|
+
console.log(
|
|
179
|
+
display.outdent +
|
|
180
|
+
theme.margin(display.margin) +
|
|
181
|
+
theme.code(display.source.slice(0,display.errs)) +
|
|
182
|
+
theme.error(display.source.slice(display.errs,display.erre)) +
|
|
183
|
+
theme.code(display.source.slice(display.erre)) +
|
|
184
|
+
theme.code(fill(total - display.source.length - display.margin.length))
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
// print empty line to balance the view
|
|
188
|
+
// later we can put something usefull here
|
|
189
|
+
// for example a link to online docs about the error
|
|
190
|
+
console.log(
|
|
191
|
+
display.outdent +
|
|
192
|
+
theme.margin(fill(display.margin.length)) +
|
|
193
|
+
theme.code(fill(total - display.margin.length))
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
// print emtpy line
|
|
197
|
+
console.log('');
|
|
198
|
+
}
|