Skip to content

Commit

Permalink
adds compile time and file sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
sintaxi committed Sep 9, 2022
1 parent 18bfd5c commit 15e5f43
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 50 deletions.
28 changes: 24 additions & 4 deletions bin/harp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var help = function(){
var box = (...args) => console.log(boxt(...args).split("\n").join("\n "));

log()
log("Harp".underline.blue, "〜".grey, "Static Web Server".brightYellow, "v".grey + pkg.version.grey)
log("Harp".underline.blue, "〜".grey, pkg.description.brightYellow, "v".grey + pkg.version.grey)
log()
// log("Process | Bundle | Generate - Just In Time".grey)
// log()
Expand All @@ -32,8 +32,8 @@ var help = function(){
log(" -h, --host " +"0.0.0.0".blue +" server host to answer to")
// log(" -c, --cache " +"false".blue +" server memory cache")
// log(" -t, --pretty " +"false".blue +" server/compile keep whitespace")
log(" -h, --help server/compile keep whitespace")
log(" -v, --version server/compile keep whitespace")
log(" -h, --help ")
log(" -v, --version ")
box(
`${'PROCESSING'.grey } ${'DATA'.grey }
${'.ejs -> .html' } ${'_data.json - directory data' }
Expand Down Expand Up @@ -92,6 +92,18 @@ ${ ('http://localhost:' + port + "/").underline.cyan }`, { align: "left", color:
}




var duration = function(d){
if (d[0] < 1){
var ms = d[1].toString().substring(0,3)
return + ms + "ms"
} else {
var ms = d[1].toString().substring(0,1)
return [d[0], ms].join(".") + "s"
}
}

/**
* Compile
*/
Expand All @@ -100,12 +112,20 @@ if (argv["_"].length === 2){
var projectPath = path.resolve(process.cwd(), argv["_"][0])
var buildPath = path.resolve(process.cwd(), argv["_"][1])

console.log()
console.log(" ", `Harp v${ pkg.version }`.yellow)
console.log(" ", "Generating Site & Bundling Assets...".blue)
console.log()

harp.compile(projectPath, buildPath, function(errors, output){
if(errors) {
console.log(JSON.stringify(errors, null, 2))
process.exit(1)
}
//console.log("Done!".green)
console.log()
console.log(" ", "Done!".green, `(${ duration(output.stats.duration) })`.grey)
console.log()
})

}

74 changes: 37 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var helpers = require('./helpers')
var middleware = require('./middleware')
var pkg = require('../package.json')
var url = require("url")
var hrn = require("human-readable-numbers")



Expand Down Expand Up @@ -154,6 +155,11 @@ exports.middleware = middleware;
*/

exports.compile = function(projectPath, outputPath, callback){
const compileStart = process.hrtime()

var stats = {
types: {}
}

/**
* Both projectPath and outputPath are optional
Expand All @@ -176,7 +182,7 @@ exports.compile = function(projectPath, outputPath, callback){
try{
outputPath = path.resolve(projectPath, outputPath)
var setup = helpers.setup(projectPath, "production")
var terra = terraform.root(setup.publicPath, setup.config.globals)
var terra = terraform.root(setup.publicPath, setup.config.globals)
}catch(err){
return callback(err)
}
Expand All @@ -196,6 +202,13 @@ exports.compile = function(projectPath, outputPath, callback){
})
}

var toHuman = function(bytes){
var human = hrn.toHumanString(bytes)
var pattern = /\d+\D$/
if (pattern.test(human)) return human
return human + "B"
}


/**
* Compile and save file
Expand All @@ -204,40 +217,17 @@ exports.compile = function(projectPath, outputPath, callback){
var compileFile = function(file, done){
process.nextTick(function () {
terra.render(file, function(error, body){
if(error){
done(error)
}else{
if(body){
var dest = path.resolve(outputPath, terraform.helpers.outputPath(file))
fs.mkdirp(path.dirname(dest), function(err){
fs.writeFile(dest, body, done)
})
}else{
if (file === "app.jsx"){
var results = esbuild.buildSync({
absWorkingDir: setup.publicPath,
entryPoints: [setup.publicPath + '/app.jsx'],
outfile: 'app.js',
bundle: true,
write: false,
plugins: [],
})
fs.writeFile([outputPath, "app.js"].join(path.sep), results.outputFiles[0]["text"], done)
}else if (file === "bundle.cjs"){
var results = esbuild.buildSync({
absWorkingDir: setup.publicPath,
entryPoints: [setup.publicPath + '/bundle.cjs'],
outfile: 'bundle.js',
bundle: true,
write: false,
plugins: [],
})
fs.writeFile([outputPath, "bundle.js"].join(path.sep), results.outputFiles[0]["text"], done)
} else {
done()
}
}
}
if(error) return done(error)
if(!body) return done()
var dest = path.resolve(outputPath, terraform.helpers.outputPath(file))
fs.mkdirp(path.dirname(dest), function(err){
var sizeInBytes = body.length
var sizeHuman = toHuman(sizeInBytes)
var sizePadded = sizeHuman.padStart(8, " ")
var filePath = `/${ terraform.helpers.outputPath(file) }`
console.log(sizePadded.green, filePath)
fs.writeFile(dest, body, done)
})
})
})
}
Expand All @@ -253,7 +243,14 @@ exports.compile = function(projectPath, outputPath, callback){
if(!terraform.helpers.shouldIgnore(file) && [".jsx", ".jade", ".ejs", ".md", ".styl", ".less", ".scss", ".sass", ".coffee", ".cjs"].indexOf(ext) === -1){
var localPath = path.resolve(outputPath, file)
fs.mkdirp(path.dirname(localPath), function(err){
fs.copy(path.resolve(setup.publicPath, file), localPath, done)
fs.stat(path.resolve(setup.publicPath, file), function(err, stats){
var sizeInBytes = stats.size
var sizeHuman = toHuman(sizeInBytes)
var sizePadded = sizeHuman.padStart(8, " ")
var filePath = `/${ file }`
console.log(sizePadded.grey, filePath)
fs.copy(path.resolve(setup.publicPath, file), localPath, done)
})
})
}else{
done()
Expand All @@ -273,9 +270,12 @@ exports.compile = function(projectPath, outputPath, callback){
callback(err)
}else{
async.each(results, copyFile, function(err){
stats.duration = process.hrtime(compileStart)

setup.config['harp_version'] = pkg.version
delete setup.config.globals
callback(null, setup.config)
setup.stats = stats
callback(null, setup)
})
}
})
Expand Down
25 changes: 18 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "harp",
"version": "0.43.0",
"description": "Static web server with built in preprocessing",
"description": "Static Web Server/Generator/Bundler",
"author": "Brock Whitten <[email protected]>",
"contributors": [
"Brock Whitten <[email protected]>",
Expand Down Expand Up @@ -50,12 +50,13 @@
"connect": "3.7.0",
"envy-json": "0.2.1",
"fs-extra": "10.1.0",
"human-readable-numbers": "0.9.5",
"mime-types": "2.1.35",
"minimist": "1.2.6",
"parseurl": "1.3.3",
"pause": "0.1.0",
"send": "0.18.0",
"terraform": "1.22.0"
"terraform": "1.22.1"
},
"devDependencies": {
"axios": "0.27.2",
Expand Down

0 comments on commit 15e5f43

Please sign in to comment.