Skip to content

Commit

Permalink
Respect .tesselignore in --slim builds
Browse files Browse the repository at this point in the history
1. If opts.slim is true,
  i. glob for all ./**/*.tesselignore files;
    a. let exclusions be the result of combining the contents of each .tesselignore into a single {pattern};
  ii. let b be a new browserify bundler object with the existing options that @reconbot discovered
  iii. let files be glob.sync(exclusions)
  iv. for each file of files
    a. call b.exclude(file)
      a. call b.bundle(...)
  v. on completion of bundle, minify and write bundled program code.
  • Loading branch information
rwaldron committed Oct 23, 2015
1 parent df5ec9e commit 98b24ac
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 38 deletions.
112 changes: 74 additions & 38 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var path = require('path');
var tar = require('tar');
var Ignore = require('fstream-ignore');
var browserify = require('browserify');
var uglify = require('uglifyify');
var uglify = require('uglify-js');
var glob = require('glob');

var PUSH_START_SCRIPT_NAME = 'start';
var NODE_PUSH_SCRIPT = __dirname + '/../../resources/start_node_script.sh';
Expand Down Expand Up @@ -217,49 +218,84 @@ actions.tarBundle = function(opts) {
if (opts.slim) {
logs.info('Generating slim build.');
return new Promise(function(resolve, reject) {
var b = browserify(opts.resolvedEntryPoint, {
builtins: false,
commondir: false,
browserField: false,
detectGlobals: false,
ignoreMissing: true,
});
b.transform(uglify);
b.bundle(function(err, results) {
if (err) {
reject(err);
} else {
fs.writeFileSync(opts.slimPath, results.toString());

var fstream = new Ignore({
path: target,
ignoreFiles: ['.tesselignore']
glob(process.cwd() + '/**/*.tesselignore', {
dot: true
}, function(error, ignoreFiles) {
if (error) {
return reject(error);
}
var rules = ignoreFiles.reduce(function(rules, ignoreFile) {
var dirname = path.dirname(ignoreFile);
var patterns = fs.readFileSync(ignoreFile, 'utf8').trim().split('\n').map(function(pattern) {
return path.join(dirname, pattern).replace(process.cwd() + path.sep, '');
});

fstream.addIgnoreRules(['*', '!' + opts.slimPath]);
return rules.concat(patterns);
}, []).map(function(rule) {

fstream.basename = '';
pack._noProprietary = true;
if (rule[rule.length - 1] === '/') {
rule += '**/*.*';
}

fstream.on('entry', function(entry) {
entry.root = {
path: entry.path
};
});
if (rule[rule.length - 1] !== '*' && rule.indexOf('.') === -1) {
rule += '/**/*.*';
}

return rule;
});

var exclusions = '{' + rules.join(',') + '}';

var b = browserify(opts.resolvedEntryPoint, {
builtins: false,
commondir: false,
browserField: false,
detectGlobals: false,
ignoreMissing: true
});

glob.sync(exclusions).forEach(function(file) {
b.exclude(file);
});

fstream.pipe(pack)
.on('data', function(chunk) {
buffers.push(chunk);
})
.on('error', function(data) {
reject(data);
})
.on('end', function() {
fs.unlinkSync(opts.slimPath);

resolve(Buffer.concat(buffers));
b.bundle(function(error, results) {
if (error) {
return reject(error);
} else {
var bundled = uglify.minify(results.toString(), {
fromString: true
});
}

fs.writeFileSync(opts.slimPath, bundled.code);

var fstream = new Ignore({
path: target,
});

fstream.addIgnoreRules(['*', '!' + opts.slimPath]);

fstream.basename = '';
pack._noProprietary = true;

fstream.on('entry', function(entry) {
entry.root = {
path: entry.path
};
});

fstream.pipe(pack)
.on('data', function(chunk) {
buffers.push(chunk);
})
.on('error', function(data) {
reject(data);
})
.on('end', function() {
fs.unlinkSync(opts.slimPath);
resolve(Buffer.concat(buffers));
});
}
});
});
});
} else {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"envfile": "^1.0.0",
"fs-extra": "^0.18.0",
"fstream-ignore": "^1.0.2",
"glob": "^5.0.15",
"inquirer": "^0.8.5",
"keypress": "^0.2.1",
"lodash": "^3.5.0",
Expand All @@ -67,6 +68,7 @@
"stream-to-buffer": "^0.1.0",
"tar": "^2.1.1",
"tar-stream": "^1.2.1",
"uglify-js": "^2.5.0",
"uglifyify": "^3.0.1",
"url-join": "0.0.1",
"usb": "^1.0.5",
Expand Down

0 comments on commit 98b24ac

Please sign in to comment.