-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
45 lines (39 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const { readFile } = require('fs/promises');
const path = require('path');
const coffeescript = require('coffeescript');
const omit = (obj, keys) =>
Object.keys(obj)
.filter((key) => !keys.includes(key))
.reduce((res, key) => Object.assign(res, { [key]: obj[key] }), {});
const compileCoffee = (code, options) => coffeescript.compile(code, options);
const convertMessage = ({ message, location, code, filename }) => {
location = {
file: filename,
line: location.first_line,
column: location.first_column,
length: location.first_line - location.last_column,
lineText: code,
};
return { text: message, location };
};
const coffeeScriptPlugin = (options = {}) => ({
name: 'coffeescript',
setup(build) {
build.onLoad({ filter: /.\.(coffee|litcoffee)$/ }, async (args) => {
const source = await readFile(args.path, 'utf8');
const filename = path.relative(process.cwd(), args.path);
const opt = omit(options, ['sourceMap']);
try {
const contents = compileCoffee(source, { filename, ...opt });
return {
contents,
};
} catch (e) {
return {
errors: [convertMessage(e)],
};
}
});
},
});
module.exports = coffeeScriptPlugin;