-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (82 loc) · 2.6 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// regularify plugin
var through = require("through");
try{
var parse = require("regularjs").parse;
}catch(e){
try{
var parse = require("regularjs/src/node").parse;
}catch(e){
console.error("you need install regularjs local")
}
}
var DEFAULT_RGL_EXTENSION = ['rgl'];
var DEFAULT_RGLC_EXTENSION = ['rglc'];
function wrap(str, options, callback){
options =options || {};
try{
var code = parse(str, {BEGIN: options.BEGIN, END: options.END, stringify:true}) ;
}catch(e){
callback(e)
}
code = 'module.exports=' + code + '';
callback(null, code)
}
var rScript = /\<script(?:\>|\s[^>]*\>)([\s\S]+)\<\/script>/;
var rTemplate = /\<template(?:\>|\s[^>]*\>)([\s\S]+)\<\/template>/;
function wrapComponent(str, options, callback){
var scriptRaw;
str = str.replace(rScript, function(all, script){
scriptRaw = script.trim();
//https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
return "";
}).trim();
// TODO:
try{
var code = parse(str, {BEGIN: options.BEGIN, END: options.END, stringify:true}) ;
}catch(e){
return callback(e)
}
code = "var template=" + code + ",Regular=require('regularjs');" + scriptRaw;
code += ";if(typeof module.exports === 'object') module.exports = Regular.extend(module.exports);var proto =module.exports.prototype; if(!proto.hasOwnProperty('template')){proto.template=template}"
callback(null ,code);
}
module.exports = function(option){
option = option || {};
if(Array.isArray(option)) option = {extensions: option}
var rgl = ( option.rgl || [] ).concat( DEFAULT_RGL_EXTENSION );
var rglc = ( option.rglc || [] ).concat( DEFAULT_RGLC_EXTENSION );
var BEGIN = option.BEGIN, END = option.END;
var rglMatch = new RegExp ("\\." + rgl.join("|") + "$")
var rglcMatch = new RegExp ("\\." + rglc.join("|") + "$")
return function(file){
var input = "";
function write(buffer){
input += buffer;
}
// rgl
function end(){
var self = this;
wrap(input, {BEGIN: BEGIN, END: END}, function(error, code){
if(error) return console.error(file +":\n" + error)
self.queue(code);
self.queue(null);
} )
}
// rglc
function endc(){
var self = this;
wrapComponent(input, {BEGIN: BEGIN, END: END}, function(error, code){
if(error) return console.error(file + ":\n" + error)
self.queue(code);
self.queue(null);
} )
}
var test = rglMatch.test(file);
if(test) return through(write, end)
var test = rglcMatch.test(file);
if(test){
return through(write, endc)
}
return through();
}
};