-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.js
52 lines (47 loc) · 1.87 KB
/
rollup.config.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
46
47
48
49
50
51
52
import typescript from 'rollup-plugin-ts';
import { resolve, dirname } from 'path';
import { promises as fs } from 'fs';
import { buildParserFile } from '@lezer/generator';
const built = new Map();
export default {
input: 'src/index.ts',
output: [
{ file: 'dist/index.cjs', format: 'cjs' },
{ file: 'dist/index.js', format: 'es' }
],
external: (id) => id != 'tslib' && !/^(\.?\/|\w:)/.test(id),
plugins: [
{
name: 'rollup-plugin-grammar-parser',
resolveId(source, importer) {
if (!source.endsWith('.grammar') && !source.endsWith('.grammar.terms.js')) return null;
return resolve(importer ? dirname(importer) : process.cwd(), source);
},
load(id) {
const isGrammarFile = id.endsWith('.grammar');
if (!isGrammarFile && !id.endsWith('.grammar.terms.js')) return null;
if (isGrammarFile) this.addWatchFile(id);
const grammarFile = id.replace(/\.terms\.js$/, '');
const build =
built.get(grammarFile) ||
built
.set(
grammarFile,
fs.readFile(grammarFile, 'utf8').then((code) => {
return buildParserFile(code, {
fileName: grammarFile,
moduleStyle: 'es',
warn: (message) => this.warn(message)
});
})
)
.get(grammarFile);
return build.then((result) => (isGrammarFile ? result.parser : result.terms));
},
watchChange(id) {
built.delete(id);
}
},
typescript()
]
};