From 45e76609b07f1b4b476ad4993c86b42d5ca0f00b Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sat, 3 Aug 2019 20:09:09 +1000 Subject: [PATCH] vweb.assets: adds AssetManager for combining/minifying/caching css & js assets (#1452) * start * more work * more * more work * update comments * add back mut * make minify faster * add mut * revert option size * ret bool from public methods not ?bool for now * update comments etc * update comment * dont return option for now * clean up --- vlib/vweb/assets/assets.v | 212 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 vlib/vweb/assets/assets.v diff --git a/vlib/vweb/assets/assets.v b/vlib/vweb/assets/assets.v new file mode 100644 index 0000000000..c019a2af24 --- /dev/null +++ b/vlib/vweb/assets/assets.v @@ -0,0 +1,212 @@ +module assets + +// this module provides an AssetManager for combining +// and caching javascript & css. + +import ( + os + time + crypto.md5 +) + +const ( + UnknownAssetTypeError = 'vweb.assets: unknown asset type' +) + +struct AssetManager { +mut: + css []Asset + js []Asset +pub: + // when true assets will be minified + minify bool + // the directory to store the cached/combined files + cache_dir string +} + +struct Asset { + file_path string + last_modified time.Time +} + +// new_manager returns a new AssetManager +pub fn new_manager() *AssetManager { + return &AssetManager{} +} + +// add_js adds a css asset +pub fn (am mut AssetManager) add_css(file string) bool { + return am.add('css', file) +} + +// add_js adds a js asset +pub fn (am mut AssetManager) add_js(file string) bool { + return am.add('js', file) +} + +// combine_css returns the combined css as a string when to_file is false +// when to_file is true it combines the css to disk and returns the path of the file +pub fn (am mut AssetManager) combine_css(to_file bool) string { + return am.combine('css', to_file) +} + +// combine_js returns the combined js as a string when to_file is false +// when to_file is true it combines the css to disk and returns the path of the file +pub fn (am mut AssetManager) combine_js(to_file bool) string { + return am.combine('js', to_file) +} + +// include_css returns the html tag(s) for including the css files in a page. +// when combine is true the files are combined. +pub fn (am mut AssetManager) include_css(combine bool) string { + return am.include('css', combine) +} + +// include_js returns the html \n' + } + for asset in assets { + out += '\n' + } + } + return out +} + +// dont return option until size limit is removed +// fn (am mut AssetManager) add(asset_type, file string) ?bool { +fn (am mut AssetManager) add(asset_type, file string) bool { + if !os.file_exists(file) { + // return error('vweb.assets: cannot add asset $file, it does not exist.') + return false + } + asset := Asset{ + file_path: file + last_modified: time.Time{uni: os.file_last_mod_unix(file)} + } + if asset_type == 'css' { + am.css << asset + } else if asset_type == 'js' { + am.js << asset + } else { + panic('$UnknownAssetTypeError ($asset_type).') + } + return true +} + +fn (am mut AssetManager) exists(asset_type, file string) bool { + assets := am.get_assets(asset_type) + for asset in assets { + if asset.file_path == file { + return true + } + } + return false +} + +fn (am mut AssetManager) get_assets(asset_type string) []Asset { + if asset_type != 'css' && asset_type != 'js' { + panic('$UnknownAssetTypeError ($asset_type).') + } + assets := if asset_type == 'css' { + am.css + } else { + am.js + } + return assets +} + +// todo: implement proper minification +pub fn minify_css(css string) string { + mut lines := css.split('\n') + for i, _ in lines { + lines[i] = lines[i].trim_space() + } + return lines.join(' ') +} + +// todo: implement proper minification +pub fn minify_js(js string) string { + mut lines := js.split('\n') + for i, _ in lines { + lines[i] = lines[i].trim_space() + } + return lines.join(' ') +}