-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserify.js
48 lines (34 loc) · 1.2 KB
/
browserify.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
const fs = require('fs');
const path = require('path');
const browserify = require('browserify');
const stream = require('node-stream');
const escapeStringRegex = require('escape-string-regexp');
// base dir
const BASEDIR = 'src/';
const ABSOLUTE_PATH = __dirname;
const ENTRY = './main.js';
// setup entry point
const b = browserify({
basedir: BASEDIR,
entries: ENTRY,
fullPaths: true
});
// add all other files
b.add('./ui/component.js');
// modify absolute paths so conditioner can find the modules by name
const relativePathRegExp = new RegExp('"(' + escapeStringRegex(ABSOLUTE_PATH) + '.+?)"', 'ig');
const baseDirRegExp = new RegExp('^' + BASEDIR);
const toRelativePaths = function(buffer) {
// get the string value from the buffer so we can replace absolute paths
let value = buffer.toString('utf-8');
// make paths relative
value = value.replace(relativePathRegExp, function(match, p1) {
return '"./' + path.relative(ABSOLUTE_PATH, p1).replace(baseDirRegExp, '') + '"';
});
// return a new buffer
return Buffer.from(value, 'utf-8');;
}
// save as bundle
b.bundle()
.pipe( stream.map( toRelativePaths ) )
.pipe( fs.createWriteStream('./dist/bundle.js') );