-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (56 loc) · 1.7 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
var fs = require('fs');
var path = require('path');
var escompile = require('esnext').compile;
var Transpiler = require('es6-module-transpiler').Compiler;
var formats = {
'amd': 'toAMD',
'yui': 'toYUI',
'cjs': 'toCJS',
'globals': 'toGlobals'
};
module.exports = function pluging(env, callback) {
function EsCompiler(path, content) {
this.path = path;
this.content = content;
}
EsCompiler.prototype = Object.create(env.ContentPlugin.prototype);
EsCompiler.prototype.constructor = EsCompiler;
EsCompiler.prototype.getFilename = function() {
return this.path.relative;
};
EsCompiler.prototype.getModulename = function(anonymous) {
return anonymous ?
null :
env.utils.stripExtension(this.getFilename());
};
EsCompiler.prototype.getPluginColor = function() {
return 'yellow';
};
EsCompiler.prototype.getView = function() {
return function view(env, locals, contents, templates, callback) {
var config = env.config.esnext || {},
format = formats[config.format || 'globals'],
transpileConf = {
compatFix: true
};
env.utils.extend(transpileConf, config.transpilerOptions);
var intermed = escompile(this.content, config.compilerOptions),
localCompiler = new Transpiler(intermed.code,
this.getModulename(config.anonymous),
transpileConf);
callback(null, new Buffer(localCompiler[format]()));
};
};
EsCompiler.fromFile = function(path, callback) {
fs.readFile(path.full, function(err, data) {
var escomp;
if (!err) {
escomp = new EsCompiler(path, data.toString());
}
callback(err, escomp);
});
};
var config = env.config.esnext || {};
env.registerContentPlugin('scripts', config.pattern || '**/*.*(js|es)', EsCompiler);
callback();
};