This repository has been archived by the owner on Mar 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
prepublish.js
50 lines (42 loc) · 1.65 KB
/
prepublish.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
'use strict';
var fs = require('fs');
var uglify = require('uglify-js');
var runtime = require('./');
try {
fs.mkdirSync(__dirname + '/lib');
} catch (ex) {
if (ex.code !== 'EEXIST') throw ex;
}
var source = fs.readFileSync(__dirname + '/index.js', 'utf8');
var ast = uglify.parse(source);
var dependencies = {};
var internals = {'dependencies': true, 'internals': true};
var sources = {};
ast.body.forEach(function (node) {
var name;
switch (node.TYPE) {
case 'Defun': name = node.name.name; break;
case 'Var': name = node.definitions[0].name.name; break;
}
if (!name || !/^pug\_/.test(name)) return;
name = name.replace(/^pug\_/, '');
var src = uglify.minify(source.substring(node.start.pos, node.end.endpos), {fromString: true}).code;
sources[name] = src;
dependencies[name] = [];
if (node.TYPE === 'Defun') {
var ast = uglify.parse(src);
ast.figure_out_scope();
var globals = ast.globals.map(function (val, key) {
return key;
});
dependencies[name] = globals.filter(function (key) { return /^pug\_/.test(key); })
.map(function (key) { return key.replace(/^pug\_/, ''); });
}
if (!runtime[name]) internals[name] = true;
});
Object.keys(dependencies).forEach(function (fn) {
dependencies[fn] = dependencies[fn].sort();
});
fs.writeFileSync(__dirname + '/lib/dependencies.js', 'module.exports = ' + JSON.stringify(dependencies, null, 2) + '\n');
fs.writeFileSync(__dirname + '/lib/internals.js', 'module.exports = ' + JSON.stringify(internals, null, 2) + '\n');
fs.writeFileSync(__dirname + '/lib/sources.js', 'module.exports = ' + JSON.stringify(sources, null, 2) + '\n');