forked from WebReflection/hyperHTML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundler.js
More file actions
58 lines (57 loc) · 2.01 KB
/
Copy pathbundler.js
File metadata and controls
58 lines (57 loc) · 2.01 KB
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
var fs = require('fs');
var log = require('tressa').log;
if (process.argv.pop() === 'dependencies') {
log('# Augmenting ' + require('./package.json').name);
fs.writeFileSync(
'index.js',
fs.readFileSync('hyperhtml.js')
.toString()
.replace(/\/\*!([a-z-]+)\*\//g, function ($0, $1) {
log(' module ' + $1);
return fs.readFileSync(require.resolve($1))
.toString()
.replace(/^.+?(function\s*\()/, ', $1')
.replace(/(}\(\));[\s\S]*$/, '$1')
// the following majinbuu parts cannot be covered in here
// however, these are covered in majinbuu project itself
.replace(/(\s+)if \(toLength \|\| TOO_MANY\) \{/, '$1/* istanbul ignore next */$1if (toLength || TOO_MANY) {')
.replace(/(\s+)if \(length\) \{/, '$1/* istanbul ignore else */$1if (length) {');
})
);
} else {
log('# Bundling ' + require('./package.json').name);
var defaultExport = function () {
return /try\s*\{\s*module\.exports\s*=\s*([0-9a-zA-Z_$]+);?\s*\}\s*catch\(.+?\)\s*\{\s*\}/;
};
var source = fs.readFileSync('index.js').toString();
var exports = ['export default $;'].concat([
'adopt',
'bind',
'define',
'escape',
'hyper',
'wire',
'Component'
].map(function (name) {
return 'export const ' + name + '=$.' + name + ';';
}));
function writeFile(name, moduleName, content) {
fs.writeFileSync(
name,
content.replace(defaultExport(), '')
.replace(new RegExp('var ' + moduleName + '(\\s*=)'), function ($0, $1) { return 'var $' + $1; }) +
`\n` + exports.join('\n')
);
}
if (defaultExport().test(source)) {
var moduleName = RegExp.$1;
log(' creating index.mjs');
writeFile('index.mjs', moduleName, source);
}
var source = fs.readFileSync('min.js').toString();
if (defaultExport().test(source)) {
var moduleName = RegExp.$1;
log(' creating min.mjs');
writeFile('min.mjs', moduleName, source);
}
}