Skip to content
Browse files

Add a file type, File

  • Loading branch information...
1 parent 893749b commit 5804d7382251fb35ef905536c78515326c0f0ca6 Kenneth Kufluk committed Jun 13, 2012
Showing with 50 additions and 18 deletions.
  1. +4 −6 lib/loadbuilder/analyzer.js
  2. +6 −12 lib/loadbuilder/asset.js
  3. +40 −0 lib/loadbuilder/file.js
View
10 lib/loadbuilder/analyzer.js
@@ -1,4 +1,5 @@
-var parser = require('esprima');
+var parser = require('esprima'),
+ File = require('./file');
function match(fragment, tree) {
// console.log(fragment, tree);
@@ -53,12 +54,9 @@ function walk(matcher, tree, parent, index) {
function analyze(matcher, source) {
// treeish can be source string, file object (from asset.js) or ast
- if (source.mtime && !source.ast) {
- source.ast = parser.parse(source.source);
- }
var tree;
- if (source.ast) {
- tree = source.ast;
+ if (source instanceof File) {
+ tree = source.ast();
} else if (typeof source == 'object') {
tree = source;
} else {
View
18 lib/loadbuilder/asset.js
@@ -3,7 +3,7 @@ var util = require('./util'),
fs = require('fs'),
path = require('path'),
jshint = require('jshint').JSHINT,
- escodegen = require('escodegen');
+ File = require('./file');
var USING = {
type: "CallExpression",
@@ -90,17 +90,11 @@ util.extend(Script.prototype, {
toSource: function() {
return this.deferWrapper(this.fromFile().source);
},
- preProcess: function(data) {
- return this.builder.preProcessor ? this.builder.preProcessor(data) : data;
- },
fromFile: function() {
- var fileInfo = fs.statSync(this.fullPath());
- if (!this.file || this.file.mtime != fileInfo.mtime.getTime()) {
- this.file = {
- mtime: fileInfo.mtime.getTime(),
- source: this.preProcess(fs.readFileSync(this.fullPath(), 'utf8'))
- };
+ if (!this.file) {
+ this.file = new File(this.fullPath(), this.builder.preProcessor);
}
+ this.file.load();
return this.file;
},
fullPath: function() {
@@ -171,7 +165,7 @@ util.extend(Module.prototype, {
},
addId: function() {
var provides = analyzer.analyze(PROVIDE, this.fromFile()),
- tree = this.fromFile().ast,
+ tree = this.fromFile().ast(),
provide;
if (provides.length > 0) {
@@ -185,7 +179,7 @@ util.extend(Module.prototype, {
});
}
}
- return escodegen.generate(tree, { beautify: true });
+ return this.fromFile().generate({ beautify: true });
}
});
View
40 lib/loadbuilder/file.js
@@ -0,0 +1,40 @@
+var util = require('./util'),
+ fs = require('fs'),
+ esprima = require('esprima'),
+ escodegen= require('escodegen');
+
+function File(fullPath, preProcessor) {
+ this.fullPath = fullPath;
+ this.preProcessor = preProcessor;
+ this.load();
+}
+
+util.extend(File.prototype, {
+ load: function() {
+ var fileInfo = fs.statSync(this.fullPath),
+ mtime = fileInfo.mtime.getTime();
+ if (!this.source || this.mtime != mtime) {
+ this.mtime = mtime;
+ this.source = this.preProcess(fs.readFileSync(this.fullPath, 'utf8'));
+ this.esprimaAst = null;
+ }
+ },
+ preProcess: function(data) {
+ return this.preProcessor ? this.preProcessor(data) : data;
+ },
+ ast: function() {
+ return this.esprimaAst = this.esprimaAst || esprima.parse(this.source);
+ },
+ generate: function(options) {
+ return escodegen.generate(this.ast(), options);
+ },
+ minified: function() {
+
+ },
+});
+
+function file(fullPath, preProcessor) {
+ return new File(fullPath, preProcessor);
+}
+
+module.exports = File;

0 comments on commit 5804d73

Please sign in to comment.
Something went wrong with that request. Please try again.