-
Notifications
You must be signed in to change notification settings - Fork 1
/
ify.js
43 lines (31 loc) · 1.1 KB
/
ify.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
"use strict";
//cnyksify, a browserify plugin
//see README.md for details
const path = require('path');
const Transform = require('stream').Transform;
module.exports = function(b) {
var entries = b._options.entries;
var sources = typeof entries == "string" ? [entries] : entries;
var source = path.resolve(sources[0]);
if(sources.length != 1)
throw "Cnyksify does not support for multiples entries";
console.error("Remap %s as app entry", source);
//re-require source (already in entry, but now exposed)
b.require(source, { entry : true, expose : 'app'});
// b.require('cnyks', {expose : 'cnyks'});
b.require('cnyks/lib/bundle', {expose : 'cnyks/lib/bundle'});
//register bundle suffix & prefix
var suffix = "require('cnyks/lib/bundle')(require('app'));";
var prefix = "#!/usr/bin/env node\n";
var transform = function(buf, enc, next) {
if(prefix)
prefix = this.push(prefix) && false;
this.push(buf);
next();
};
var flush = function() {
this.push(suffix);
this.push(null);
};
b.pipeline.get("wrap").unshift(new Transform({transform, flush}));
};