forked from malinajs/malinajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalina-rollup.js
44 lines (39 loc) · 1.42 KB
/
malina-rollup.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
const malina = require('malinajs');
module.exports = malinaRollup;
function malinaRollup(option = {}) {
if(option.displayVersion !== false) console.log('! Malina.js', malina.version);
if(!option.extension) option.extension = ['html', 'ma', 'xht'];
let content_cache = {};
return {
name: 'malina',
async transform(code, id) {
if(!option.extension.some(ext => id.endsWith('.' + ext))) return null;
let result;
let opts = Object.assign({
path: id,
name: id.match(/([^/\\]+)\.\w+$/)[1]
}, option);
try {
let ctx = await malina.compile(code, opts);
result = ctx.result;
if(ctx.css.result) {
let name = id.replace(/[^\w.\-]/g, '') + '.css';
content_cache[name] = ctx.css.result;
result += `\nimport "${name}";\n`;
}
} catch (e) {
if(e.details) console.log(e.details);
throw e;
}
return {code: result};
},
async resolveId(name, importer) {
if(content_cache[name]) return name;
if(name == 'malinajs') return await this.resolve('malinajs/runtime.js', importer, {skipSelf: true});
return null;
},
load(id) {
return content_cache[id] || null;
}
};
}