forked from jnordberg/wintersmith-browserify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.coffee
More file actions
55 lines (45 loc) · 1.89 KB
/
plugin.coffee
File metadata and controls
55 lines (45 loc) · 1.89 KB
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
51
52
53
54
55
browserify = require 'browserify'
module.exports = (env, callback) ->
options = env.config.browserify or {}
options.transforms ?= ['caching-coffeeify']
options.debug ?= (env.mode is 'preview')
options.externals ?= {}
options.ignore ?= []
options.extensions ?= ['.coffee']
for transform, i in options.transforms
options.transforms[i] = require transform
class BrowserifyPlugin extends env.ContentPlugin
constructor: (@filepath) ->
@bundler = browserify options
@bundler.add @filepath.full
externals = options.externals[@filepath.relative] or []
@bundler.external file for file in externals
@bundler.ignore file for file in options.ignore
@bundler.transform transform for transform in options.transforms
@property 'source', ->
require('fs').readFileSync(@filepath.full).toString()
getFilename: ->
env.utils.stripExtension(@filepath.relative) + '.js'
getView: -> (env, locals, contents, templates, callback) ->
stream = @bundler.bundle()
stream.on 'error', (error) =>
# add better debuginfo to parse errors
msg = ''
if error.file?
msg += env.relativeContentsPath error.file
else
msg += @filepath.relative
msg += ':'
if error.location?.first_line?
msg += "#{ error.location.first_line }"
msg += " #{ error.message }"
if error.body? and error.location?.first_line? and error.location?.first_column?
line = error.body.split('\n')[error.location.first_line]
pad = (' ' for i in [0...error.location.first_column]).join('')
msg += "\n\n #{ line }\n #{ pad }^\n"
error.message = msg
callback null, stream
BrowserifyPlugin.fromFile = (filepath, callback) ->
callback null, new BrowserifyPlugin filepath
env.registerContentPlugin 'scripts', '**/*.*(js|coffee)', BrowserifyPlugin
callback()