-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.coffee
50 lines (39 loc) · 1.73 KB
/
plugin.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
uglify = require 'uglify-js'
CleanCSS = require 'clean-css'
fs = require 'fs'
path = require 'path'
module.exports = (env, callback) ->
class MinifyFile extends env.ContentPlugin
constructor: (@filepath) ->
getFilename: ->
@filepath.relative.replace /\.minify\.json$/, ''
getView: -> (env, locals, contents, templates, callback) ->
filepath = @filepath
fileType = @getFilename().split('.').pop()
env.logger.verbose "Minify: Loading Minify config from #{@filepath.relative}"
fs.readFile @filepath.full, (error, buffer) ->
if error
callback error
else
options = JSON.parse buffer.toString()
basePath = path.normalize path.dirname filepath.full
env.logger.verbose "Minify: Loading scripts from #{basePath}"
pathList = (path.join(basePath, item) for item in options.input)
try
if fileType is 'js'
result = uglify.minify pathList, {compress: false}
callback null, Buffer result.code
else if fileType is 'css'
# Use provided settings, if any, and define the root path:
options.settings = options.settings or env.config.minify?.css or {}
# Normalize paths:
inputPaths = for inputPath in options.input
path.resolve basePath, inputPath
result = new CleanCSS(options.settings).minify inputPaths
callback null, Buffer result.styles
catch error
callback error
MinifyFile.fromFile = (filepath, callback) ->
callback null, new MinifyFile filepath
env.registerContentPlugin 'minify', '**/*.*.minify.json', MinifyFile
callback()