Permalink
Browse files

Asset manager refactoring

  • Loading branch information...
1 parent 2f48e51 commit 3c108eb4b3b3c9cac91f7245c1294736c5e277d6 Owen Barnes committed Jun 22, 2012
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -18,37 +18,39 @@ pro = uglifyjs.uglify
module.exports = (ss, options) ->
+ loadFile = (dir, fileName, type, options, cb) ->
+ dir = pathlib.join(ss.root, dir)
+ path = pathlib.join(dir, fileName)
+ extension = pathlib.extname(path)
+ extension = extension && extension.substring(1) # argh!
+ formatter = ss.client.formatters[extension]
+ throw new Error("Invalid path. Request for #{path} must not live outside #{dir}") if path.substr(0, dir.length) != dir
+ throw new Error("Unsupported file extension '.#{extension}' when we were expecting some type of #{type.toUpperCase()} file. Please provide a formatter for #{path.substring(root.length)} or move it to /client/static") unless formatter
+ throw new Error("Unable to render '#{fileName}' as this appears to be a #{formatter.assetType.toUpperCase()} file. Expecting some type of #{type.toUpperCase()} file in #{dir.substr(root.length)} instead") unless formatter.assetType == type
+ formatter.compile(path.replace(/\\/g, '/'), options, cb) # replace '\' with '/' to support Windows
+
+ # Public
+
js: (path, opts, cb) ->
- loadFile ss.root, options.dirs.code, path, 'js', opts, (output) ->
+ loadFile options.dirs.code, path, 'js', opts, (output) ->
output = wrapCode(output, path, opts.pathPrefix)
output = minifyJSFile(output, path) if opts.compress && !path.indexOf('.min') >= 0
cb(output)
worker: (path, opts, cb) ->
- loadFile ss.root, options.dirs.workers, path, 'js', opts, (output) ->
+ loadFile options.dirs.workers, path, 'js', opts, (output) ->
output = minifyJSFile(output, path) if opts.compress
cb(output)
css: (path, opts, cb) ->
- loadFile(ss.root, options.dirs.css, path, 'css', opts, cb)
+ loadFile(options.dirs.css, path, 'css', opts, cb)
html: (path, opts, cb) ->
- loadFile(ss.root, options.dirs.views, path, 'html', opts, cb)
+ loadFile(options.dirs.views, path, 'html', opts, cb)
# PRIVATE
-loadFile = (root, dir, fileName, type, options, cb) ->
- dir = pathlib.join(root, dir)
- path = pathlib.join(dir, fileName)
- extension = pathlib.extname(path)
- extension = extension && extension.substring(1) # argh!
- formatter = formatters.byExtension[extension]
- throw new Error("Invalid path. Request for #{path} must not live outside #{dir}") if path.substr(0, dir.length) != dir
- throw new Error("Unsupported file extension '.#{extension}' when we were expecting some type of #{type.toUpperCase()} file. Please provide a formatter for #{path.substring(root.length)} or move it to /client/static") unless formatter
- throw new Error("Unable to render '#{fileName}' as this appears to be a #{formatter.assetType.toUpperCase()} file. Expecting some type of #{type.toUpperCase()} file in #{dir.substr(root.length)} instead") unless formatter.assetType == type
- formatter.compile(path.replace(/\\/g, '/'), options, cb) # replace '\' with '/' to support Windows
-
formatKb = (size) ->
"#{Math.round((size / 1024) * 1000) / 1000} KB"
@@ -4,9 +4,6 @@
mods = []
-# Keep a list of all loaded code formatters by file extension
-exports.byExtension = {}
-
exports.init = (root) ->
add = (nameOrModule, config = {}) ->
@@ -32,6 +29,8 @@ exports.init = (root) ->
exports.load = ->
+ byExtension = {}
mods.forEach (mod) ->
mod.extensions.forEach (extension) ->
- exports.byExtension[extension] = mod
+ byExtension[extension] = mod
+ byExtension
View
@@ -52,7 +52,7 @@ module.exports = (ss, router) ->
# Return API
formatters: formatters.init(ss.root)
- templateEngine: templateEngine.init(ss, options)
+ templateEngine: templateEngine(ss)
assets: systemAssets
options: options
Oops, something went wrong.

0 comments on commit 3c108eb

Please sign in to comment.