Skip to content
Permalink
Browse files

fix(less): npm update required, do not send less imports to the dist …

…folder
  • Loading branch information
jyounce committed Jun 24, 2015
1 parent d65de12 commit d3742e6fbaa7d446730d3435ffa795d8fe258c39
Showing with 135 additions and 12 deletions.
  1. +71 −0 helpers/Less.coffee
  2. +1 −0 package.json
  3. +63 −12 tasks/compile/less.coffee
@@ -0,0 +1,71 @@
module.exports = (config, gulp) ->
q = require 'q'
lessImports = require 'less-imports'
log = require "#{config.req.helpers}/log"

class Less
# private methods
# ===============
getImports = (files) ->
imports = {}
for own k1, v1 of files
continue if not v1.length
v1.forEach (v2, i) ->
imports[v2] = i
Object.keys imports

addNotToImports = (imports) ->
imports.forEach (v, i) ->
imports[i] = "!#{v}"
imports

isImport = (imports, _path) ->
imports.indexOf(_path) isnt -1

# constructor
# ===========
constructor: (@src) ->
@imports = {}
@files = {}

# public methods
# ==============
addImport: (_path) ->
paths = lessImports.findImports _path
return @ if not paths.length
@files[_path] = paths
@

setImports: ->
defer = q.defer()
gulp.src @src, read:false
.on 'data', (file) =>
@addImport file.path
.on 'end', =>
defer.resolve @
defer.promise

getFiles: ->
@files

getImports: ->
files = @getFiles()
imports = getImports files
@imports = addNotToImports imports
@imports

getWatchSrc: (_path) ->
files = @getFiles()
imports = getImports files
return [ _path ] if not isImport imports, _path
# is import, what non import(s) has it?
src = []
for own k1, v1 of files
continue if isImport imports, k1
continue if v1.indexOf(_path) is -1 # file has import
src.push k1
src




@@ -50,6 +50,7 @@
"gulp-template": "~3.0.0",
"gulp-uglify": "~1.2.0",
"gulp-watch": "~4.2.4",
"less-imports": "~1.1.0",
"mkdirp": "~0.5.1",
"open": "~0.0.5",
"q": "~1.4.1"
@@ -5,44 +5,95 @@ module.exports = (gulp, config, watchFile={}) ->
gulpif = require 'gulp-if'
less = require 'gulp-less'
plumber = require 'gulp-plumber'
tasks = require("#{config.req.helpers}/tasks")()
lessHelper = require("#{config.req.helpers}/Less") config, gulp
forWatchFile = !!watchFile.path

# streams
# =======
addToDistPath = -> # add 'styles/' for app and 'rapid-build/styles/' for rb
addToDistPath = (appOrRb) ->
transform = (file, cb) ->
fileName = path.basename file.path
basePath = file.base.replace config.src.app.client.styles.dir, ''
basePath = file.base.replace config.src[appOrRb].client.styles.dir, ''
basePathDup = path.join basePath, basePath
relPath = path.join basePathDup, file.relative
_path = path.join config.src.app.client.styles.dir, relPath
_path = path.join config.src[appOrRb].client.styles.dir, relPath
file.path = _path
cb null, file
es.map transform

runTask = (src, dest) ->
# main task
# =========
runTask = (src, dest, forWatch, appOrRb) ->
defer = q.defer()
gulp.src src
.pipe plumber()
.pipe less()
.pipe gulpif forWatch, addToDistPath appOrRb
.pipe gulp.dest dest
.on 'end', ->
# console.log dest
defer.resolve()
defer.promise

# less helpers
# ============
getImports = (appOrRb) ->
defer = q.defer()
new lessHelper config.glob.src[appOrRb].client.styles.less
.setImports()
.then (me) ->
imports = me.getImports()
defer.resolve imports
defer.promise

getWatchSrc = (appOrRb) ->
defer = q.defer()
new lessHelper config.glob.src[appOrRb].client.styles.less
.setImports()
.then (me) ->
src = me.getWatchSrc watchFile.path
defer.resolve src
defer.promise

# less runs
# =========
runLessWatch = (appOrRb) ->
defer = q.defer()
getWatchSrc(appOrRb).then (src) ->
dest = config.dist[appOrRb].client.styles.dir
runTask(src, dest, true, appOrRb).done -> defer.resolve()
defer.promise

runLess = (appOrRb) ->
defer = q.defer()
getImports(appOrRb).then (imports) ->
dest = config.dist[appOrRb].client.styles.dir
src = config.glob.src[appOrRb].client.styles.less
src = [].concat src, imports
runTask(src, dest).done -> defer.resolve()
defer.promise

# entry points
# ============
runSingle = ->
runTask watchFile.path, watchFile.rbDistDir
runLessWatch watchFile.rbAppOrRb

runMulti = ->
tasks.run.all(
config, runTask,
'styles', 'less',
['client']
)
defer = q.defer()
q.all([
runLess 'app'
runLess 'rb'
]).done -> defer.resolve()
defer.promise

# register task
# =============
return runSingle() if forWatchFile
gulp.task "#{config.rb.prefix.task}less", -> runMulti()







0 comments on commit d3742e6

Please sign in to comment.
You can’t perform that action at this time.