Skip to content

Commit

Permalink
[fix] allow external sources to be part of the collection, change fil…
Browse files Browse the repository at this point in the history
…e constructor around a bit
  • Loading branch information
Swaagie committed May 26, 2015
1 parent ed68cf7 commit b614850
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 33 deletions.
54 changes: 41 additions & 13 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ Compiler.prototype.put = function put(filepath) {
if (error) return compiler.emit('error', error);

compiler.emit('preprocessed', filepath);
compiler.register(new File(filepath, compiler.type(filepath), false, code));
compiler.register(new File(filepath, {
extname: compiler.type(filepath),
code: code
}));
});
};

Expand Down Expand Up @@ -333,17 +336,16 @@ Compiler.prototype.catalog = function catalog(pages, done) {
* @api private
*/
function prefab(assemble, filepath, next) {
if (/^(http:|https:)?\/\//.test(filepath)) return next(null, assemble);
if (compiler.http(filepath)) return next(null, assemble);

compiler.process(filepath, function store(error, code) {
if (error) return next(error);

var file = new File(
filepath,
compiler.type(filepath),
list[filepath].dependency,
code
);
var file = new File(filepath, {
extname: compiler.type(filepath),
dependency: list[filepath].dependency,
code: code
});

file = file.hash in assemble ? assemble[file.hash] : file;
file.pagelets = (file.pagelets || []).concat(list[filepath].pagelets);
Expand Down Expand Up @@ -433,10 +435,13 @@ Compiler.prototype.catalog = function catalog(pages, done) {
client: crypto.createHash('md5').update(data.client).digest('hex')
};

compiler.register(new File(path, '.js', false, framework.get('template', {
name: data.hash.client,
client: data.client
})));
compiler.register(new File(path, {
extname: '.js',
code: framework.get('template', {
name: data.hash.client,
client: data.client
})
}));
}

//
Expand Down Expand Up @@ -486,7 +491,11 @@ Compiler.prototype.catalog = function catalog(pages, done) {
if (err) return done(err);
debug('Finished creating browserify build');

var file = new File(compiler.client, '.js', true, buffer);
var file = new File(compiler.client, {
extname: '.js',
dependency: true,
code: buffer
});

compiler.register(file);

Expand Down Expand Up @@ -532,6 +541,14 @@ Compiler.prototype.page = function find(page) {
if (!(type in page._dependencies)) return;

page._dependencies[type].forEach(function each(dependency) {
if (compiler.http(dependency)) {
return assets.push(new File(dependency, {
type: compiler.type(dependency),
dependency: true,
external: true
}));
}

dependency = compiler.resolve(dependency);
if (!dependency) return;

Expand All @@ -542,6 +559,17 @@ Compiler.prototype.page = function find(page) {
return assets;
};

/**
* Check if the path is a http(s) url.
*
* @param {String} filepath Url to file.
* @return {Boolean}
* @api private
*/
Compiler.prototype.http = function http(filepath) {
return /^(http:|https:)?\/\//.test(filepath);
}

/**
* Generate HTML.
*
Expand Down
43 changes: 30 additions & 13 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ var crypto = require('crypto')
* Simple representation of a single file.
*
* @constructor
* @param {String|Array} aliases absolute path(s) to the source.
* @param {String} extname extension of the file.
* @param {Boolean} dependency File is page level dependency.
* @param {Buffer} code The contents of the file.
* @param {String} filepath Absolute path to file.
* @param {Object} options Set of options
*
* Available options:
* - aliases {String|Array} absolute (alternative) path(s) to the source.
* - extname {String} extension of the file.
* - dependency {Boolean} File is page level dependency.
* - code {Buffer} The contents of the file.
* - external {Boolean} File is hosted externally.
*
* @api public
*/
function File(aliases, extname, dependency, code) {
function File(filepath, options) {
this.fuse();

options = options || {};
options.code = options.code || '';
options.extname = options.extname || '.js';
options.external = options.external || false;
options.dependency = options.dependency || false;

//
// Normalize aliasses. File can have multiple aliases, for example due to
// symlinked content. Aliases will be used by compiler.register
//
aliases = Array.isArray(aliases) ? aliases : [ aliases ];
options.aliases = options.aliases || filepath;
options.aliases = Array.isArray(options.aliases)
? options.aliases
: [ options.aliases ];

this.readable('enumerable', File.predefine(this, {
enumerable: true,
Expand All @@ -31,22 +46,23 @@ function File(aliases, extname, dependency, code) {
this.writable('code'); // Actual code.
this.writable('buffer'); // Buffer of code.
this.writable('_events'); // EventEmitter 3.
this.writable('aliases', aliases); // Absolute paths to source.
this.writable('aliases', options.aliases); // Absolute paths to source.

this.enumerable('length'); // Buffer length.
this.enumerable('hash', null); // Hashed code representation.
this.enumerable('pagelets', []); // List of pagelets.
this.enumerable('extname', extname); // File extension.
this.enumerable('filepath', filepath); // Absolute path to file.
this.enumerable('extname', options.extname); // File extension.
this.enumerable('external', options.external); // File is hosted externally.

this.readable('dependency', dependency); // File is page dependency.
this.readable('dependency', options.dependency); // File is page dependency.
this.readable('type', this.mime[this.extname]); // The content-type.

//
// Process the content of the file if provided.
//
code = code || '';
this.hash = this.encrypt(code);
this.set(code);
this.hash = this.encrypt(options.code);
this.set(options.code);
}

fuse(File, require('eventemitter3'));
Expand All @@ -60,6 +76,7 @@ fuse(File, require('eventemitter3'));
File.readable('location', {
enumerable: false,
get: function get() {
if (this.external) return this.filepath;
return '/' + this.hash + this.extname;
}
}, true);
Expand Down Expand Up @@ -147,7 +164,7 @@ File.readable('is', function is(type) {
* @return {File} fluent interface
* @api public
*/
File.readable('alias', function alias(filepath) {
File.readable('alias', function alias(filepath) {
if (!~this.aliases.indexOf(filepath)) this.aliases.push(filepath);
return this;
});
Expand Down
4 changes: 2 additions & 2 deletions test/lib/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ describe('Collection', function () {
});

it('returns array of location of files in the collection', function () {
collection.push(new File('empty1', '.css'));
collection.push(new File('empty2', '.js'));
collection.push(new File('empty1', { extname: '.css' }));
collection.push(new File('empty2', { extname:'.js' }));

var json = JSON.stringify(collection);
assume(json).to.be.a('string');
Expand Down
6 changes: 5 additions & 1 deletion test/lib/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ describe('Compiler', function () {
, compiler, bigpipe, file;

beforeEach(function () {
file = new File('./path/to/file.js', '.js', [], 'tiny piece of code');
compiler = new Compiler('/tmp');
bigpipe = new BigPipe;
file = new File('./path/to/file.js', {
extname: '.js',
aliases: [],
code: 'tiny piece of code'
});
});

afterEach(function () {
Expand Down
20 changes: 16 additions & 4 deletions test/lib/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ describe('File', function () {
, file;

beforeEach(function () {
file = new File(location, ext, false, code);
file = new File(location, {
extname: ext,
code: code
});
});

afterEach(function () {
Expand Down Expand Up @@ -40,14 +43,20 @@ describe('File', function () {
});

it('allows code to be null or undefined', function () {
file = new File(location, ext, false, void 0);
file = new File(location, {
extname: ext,
code: void 0
});

assume(file.code).to.equal('');
assume(file.buffer.length).to.equal(0);
assume(file.hash).to.equal('da39a3ee5e6b4b0d3255bfef95601890afd80709');
assume(file.length).to.equal(0);

file = new File(location, ext, false, null);
file = new File(location, {
extname: ext,
code: null
});

assume(file.code).to.equal('');
assume(file.buffer.length).to.equal(0);
Expand All @@ -64,7 +73,10 @@ describe('File', function () {
});

it('will be called on construction if content is provided', function () {
var result = new File(location, ext, false, 'custom');
var result = new File(location, {
extname: ext,
code: 'custom'
});

assume(result.code).to.equal('custom');
assume(result.buffer).to.be.instanceof(Buffer);
Expand Down

0 comments on commit b614850

Please sign in to comment.