Skip to content
Permalink
Browse files

feat(options): add option to use a custom spa file and not the build …

…system's
  • Loading branch information
jyounce committed Jun 13, 2015
1 parent fd95cae commit 763322cad411a937358becc85f7c3c73ca577a12
@@ -48,7 +48,6 @@ gulp rapid-build:prod
# dist.client[images|scripts|styles|views].dir = (string) defaults to property name
# dist.client.bower.dir = (string) defaults to 'bower_components'
# dist.client.libs.dir = (string) defaults to 'libs' = 3rd party libraries that aren't bower components
# dist.client.spa.file = (string) defaults to 'spa.html'
# dist.server.dir = (string) defaults to 'server'
# dist.server.file = (string) defaults to 'routes.js'
# src.dir = (string) defaults to 'src'
@@ -67,7 +66,12 @@ gulp rapid-build:prod
# angular.templateCache.useAbsolutePaths = (boolean) defaults to false = prefix template urls with a '/'
# spa.title = (string) defaults to package.json name or 'Application' = html title tag value
# spa.description = (string) defaults to package.json description = html meta description tag value
# ===================================================================================================================================================================
# spa.src.file = (string) defaults to 'spa.html' = set if you want to use your own spa file and not the build system's (file must be located in your client src directory)
# spa.src.dir = (string) defaults to null = set if you are using your own spa file and that file is located in a directory in your client src directory
# spa.dist.file = (string) defaults to spa.src.file or 'spa.html' = provide if you want the dist spa file to be named differently, example: 'index.html'
# spa.exclude.styles = (boolean) defaults to false = set to true to exclude styles from the spa file
# spa.exclude.scripts = (boolean) defaults to false = set to true to exclude scripts from the spa file
# ==============================================================================================================================================================================================================
```

##### CHANGELOG
@@ -13,10 +13,10 @@ module.exports = (rbDir, options) ->
config = require("#{config.req.config}/config-env") config
config = require("#{config.req.config}/config-rb") config, rbDir
config = require("#{config.req.config}/config-app") config, options
config = require("#{config.req.config}/config-spa") config, options
config = require("#{config.req.config}/config-file-names") config
config = require("#{config.req.config}/config-angular") config, options
config = require("#{config.req.config}/config-dist-and-src") config, options
config = require("#{config.req.config}/config-spa") config, options
config = require("#{config.req.config}/config-templates") config
config = require("#{config.req.config}/config-temp") config
config = require("#{config.req.config}/config-node_modules") config
@@ -30,8 +30,8 @@ module.exports = (config, options) ->
angular.templateCache.dev.enable = options.angular.templateCache.dev.enable or false
angular.templateCache.useAbsolutePaths = options.angular.templateCache.useAbsolutePaths or false

# bower dependencies
# ==================
# rb bower dependencies
# =====================
angular.bowerDeps =
'angular': angular.version
'angular-resource': angular.version
@@ -17,7 +17,6 @@ module.exports = (config, options) ->
views: 'views'

file =
spa: 'spa.html'
appServer: 'routes' # app server dist entry file
rbServer: 'server' # rb server dist bootstrap file

@@ -114,23 +113,6 @@ module.exports = (config, options) ->
v3.dirName = dir[k3]
addDirName 'dist'

# add spa
# =======
getDistSpa = ->
spaFile = options.dist.client.spa.file or file.spa
path.join config.dist.app.client.dir, spaFile

getSrcSpa = ->
path.join config.src.rb.client.dir, file.spa

config.dist.app.client.spa =
file: options.dist.client.spa.file or file.spa
path: getDistSpa()

config.src.rb.client.spa =
file: file.spa
path: getSrcSpa()

# server
# ======
removeServerDir = ->
@@ -4,7 +4,6 @@
# dist.client[images|scripts|styles|views].dir = (string) defaults to property name
# dist.client.bower.dir = (string) defaults to 'bower_components'
# dist.client.libs.dir = (string) defaults to 'libs' = 3rd party libraries that aren't bower components
# dist.client.spa.file = (string) defaults to 'spa.html'
# dist.server.dir = (string) defaults to 'server'
# dist.server.file = (string) defaults to 'routes.js'
# src.dir = (string) defaults to 'src'
@@ -23,14 +22,19 @@
# angular.templateCache.useAbsolutePaths = (boolean) defaults to false
# spa.title = (string) defaults to package.json name
# spa.description = (string) defaults to package.json description
# spa.src.file = (string) defaults to 'spa.html'
# spa.src.dir = (string) defaults to null
# spa.dist.file = (string) defaults to spa.src.file or 'spa.html'
# spa.exclude.styles = (boolean) defaults to false
# spa.exclude.scripts = (boolean) defaults to false
# ===============================================================================================================================
module.exports = (config, options) ->
log = require "#{config.req.helpers}/log"
isType = require "#{config.req.helpers}/isType"

# options
# =======
formatOptions = ->
# format options
# ==============
distAndSrcOptions = ->
['dist', 'src'].forEach (v1) ->
options[v1] = {} if not isType.object options[v1]
options[v1].dir = null if not isType.string options[v1].dir
@@ -43,20 +47,16 @@ module.exports = (config, options) ->
['bower', 'images', 'libs', 'scripts', 'styles', 'views'].forEach (v3) ->
options[v1][v2][v3] = {} if not isType.object options[v1][v2][v3]
options[v1][v2][v3].dir = null if not isType.string options[v1][v2][v3].dir
# spa dist file
if v1 is 'dist'
options[v1][v2].spa = {} if not isType.object options[v1][v2].spa
options[v1][v2].spa.file = null if not isType.string options[v1][v2].spa.file

formatServerOptions = -> # app server dist entry file
serverOptions = -> # app server dist entry file
options.dist.server.file = null if not isType.string options.dist.server.file

formatPortOptions = -> # server ports
portOptions = -> # server ports
options.ports = {} if not isType.object options.ports
options.ports.server = null if not isType.number options.ports.server
options.ports.reload = null if not isType.number options.ports.reload

formatOrderOptions = ->
orderOptions = ->
options.order = {} if not isType.object options.order
options.order.scripts = {} if not isType.object options.order.scripts
options.order.styles = {} if not isType.object options.order.styles
@@ -65,7 +65,7 @@ module.exports = (config, options) ->
options.order.styles.first = null if not isType.array options.order.styles.first
options.order.styles.last = null if not isType.array options.order.styles.last

formatAngularOptions = ->
angularOptions = ->
options.angular = {} if not isType.object options.angular
options.angular.modules = null if not isType.array options.angular.modules
options.angular.version = null if not isType.string options.angular.version
@@ -75,17 +75,25 @@ module.exports = (config, options) ->
options.angular.templateCache.dev = {} if not isType.object options.angular.templateCache.dev
options.angular.templateCache.dev.enable = null if not isType.boolean options.angular.templateCache.dev.enable

formatSpaOptions = ->
spaOptions = ->
options.spa = {} if not isType.object options.spa
options.spa.title = null if not isType.string options.spa.title
options.spa.description = null if not isType.string options.spa.description
options.spa.src = {} if not isType.object options.spa.src
options.spa.dist = {} if not isType.object options.spa.dist
options.spa.exclude = {} if not isType.object options.spa.exclude
options.spa.src.dir = null if not isType.string options.spa.src.dir
options.spa.src.file = null if not isType.string options.spa.src.file
options.spa.dist.file = null if not isType.string options.spa.dist.file
options.spa.exclude.styles = null if not isType.boolean options.spa.exclude.styles
options.spa.exclude.scripts = null if not isType.boolean options.spa.exclude.scripts

formatOptions()
formatServerOptions()
formatPortOptions()
formatOrderOptions()
formatAngularOptions()
formatSpaOptions()
distAndSrcOptions()
serverOptions()
portOptions()
orderOptions()
angularOptions()
spaOptions()

# logs
# ====
@@ -1,16 +1,57 @@
module.exports = (config, options) ->
path = require 'path'
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()
pkg = require "#{config.req.app}/package.json"

# helpers
# =======
getSrcDir = (_custom, dir) ->
rbDir = config.src.rb.client.dir
appDir = config.src.app.client.dir
return rbDir if not _custom
return appDir if not dir
# to keep windows happy
dir = dir.split path.sep
for own k, v of dir
continue if not v
appDir = path.join appDir, v
appDir

# defaults
# ========
custom = !!options.spa.src.file
srcFile = options.spa.src.file or 'spa.html'
distFile = options.spa.dist.file or srcFile
srcDir = getSrcDir custom, options.spa.src.dir

# init spa
# ========
spa = {}
spa.custom = custom
spa.title = options.spa.title or pkg.name or 'Application'
spa.description = options.spa.description or pkg.description or null

# dist
# ====
spa.dist =
file: distFile
path: path.join config.dist.app.client.dir, distFile

# src
# ===
spa.src =
file: srcFile
path: path.join srcDir, srcFile

# exclude
# =======
spa.exclude =
styles: options.spa.exclude.styles or false
scripts: options.spa.exclude.scripts or false

# add spa to config
# =====================
# =================
config.spa = spa

# logs
@@ -45,6 +45,7 @@
"gulp-nodemon": "~2.0.3",
"gulp-plumber": "~1.0.1",
"gulp-rename": "~1.2.2",
"gulp-replace": "~0.5.3",
"gulp-sequence": "~0.3.2",
"gulp-template": "~3.0.0",
"gulp-uglify": "~1.2.0",
@@ -1,24 +1,24 @@
<!doctype html>
<html xmlns:ng="http://angularjs.org" xmlns:app="ignored" ng-app="<%= moduleName %>">
<html xmlns:ng="http://angularjs.org" xmlns:app="ignored" ng-app="<!--#include moduleName-->">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title><%= title %></title>
<meta name="description" content="<%= description %>" />
<title><!--#include title--></title>
<meta name="description" content="<!--#include description-->" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- ClickJacking Defense begin (https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet) -->
<style id="antiClickjack">body{display:none !important;}</style>
<script>(function(w){if(w.top===w.self){var a=w.document.getElementById('antiClickjack');a.parentNode.removeChild(a);}else{w.top.location=w.self.location;}}(window));</script>
<!-- ClickJacking Defense end -->

<%= styles %>
<!--#include styles-->
<!-- When you load angular at the bottom, you need to create the "ng-cloak" rules yourself -->
<style>[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak{display:none;}ng\:form{display:block;}</style>
</head>
<body ng-cloak class="ng-cloak">
<div ng-view></div>

<%= scripts %>
<!--#include scripts-->
</body>
</html>
@@ -3,7 +3,7 @@ express = require 'express'
config = require './config.json'
app = express()
port = config.app.ports.server
spa = config.dist.app.client.spa.file
spa = config.spa.dist.file
client = config.dist.app.client.dir
appPath = config.dist.app.server.scripts.path
appFile = config.dist.app.server.scripts.file
@@ -1,19 +1,36 @@
module.exports = (gulp, config) ->
q = require 'q'
gulpif = require 'gulp-if'
rename = require 'gulp-rename'
replace = require 'gulp-replace'
template = require 'gulp-template'
pathHelp = require "#{config.req.helpers}/path"
moduleHelp = require "#{config.req.helpers}/module"
format = require("#{config.req.helpers}/format")()

# helpers
# =======
runReplace = (type) ->
newKey = "<%= #{type} %>"
key = "<!--#include #{type}-->"
exclude = config.spa.exclude[type]
gulpif not exclude, replace key, newKey

# task
# ====
runTask = (src, dest, data={}) ->
runTask = (src, dest, file, data={}) ->
defer = q.defer()
gulp.src src
.pipe rename file
.pipe runReplace 'styles'
.pipe runReplace 'scripts'
.pipe runReplace 'moduleName'
.pipe runReplace 'title'
.pipe runReplace 'description'
.pipe template data
.pipe gulp.dest dest
.on 'end', ->
# console.log 'spa.html built'.yellow
# console.log "#{file} built".yellow
defer.resolve()
defer.promise

@@ -41,8 +58,9 @@ module.exports = (gulp, config) ->
gulp.task "#{config.rb.prefix.task}build-spa", ->
data = getData()
runTask(
config.src.rb.client.spa.path
config.spa.src.path
config.dist.app.client.dir
config.spa.dist.file
data
)

@@ -7,7 +7,7 @@ module.exports = (gulp, config) ->
runTask = (src, dest, file) ->
defer = q.defer()
gulp.src src
.pipe minifyHtml()
.pipe minifyHtml empty:true, conditionals:true, ssi:true
.pipe gulp.dest dest
.on 'end', ->
console.log "minified #{file}".yellow
@@ -18,9 +18,9 @@ module.exports = (gulp, config) ->
# =============
gulp.task "#{config.rb.prefix.task}minify-spa", ->
runTask(
config.dist.app.client.spa.path
config.spa.dist.path
config.dist.app.client.dir
config.dist.app.client.spa.file
config.spa.dist.file
)


@@ -45,7 +45,7 @@ module.exports = (gulp, config, watchFile={}) ->
opts.module = config.angular.moduleName
gulp.src src
.pipe addToDistPath()
.pipe gulpif isProd, minifyHtml()
.pipe gulpif isProd, minifyHtml empty:true, conditionals:true, ssi:true
.pipe templateCache file, opts
.pipe gulp.dest dest
.on 'end', ->
@@ -95,7 +95,7 @@ module.exports = (gulp, config, browserSync) ->

events = (file, taskName, opts={}) -> # add, change, unlink
log.watch taskName, file, opts
return tasks.buildSpa() if taskName is 'spa.html'
return tasks.buildSpa() if taskName is 'build spa'
return if not file
return if not file.event
return if not file.path
@@ -129,6 +129,15 @@ module.exports = (gulp, config, browserSync) ->
else
createWatch glob.src.html, 'html', lang:'html', srcType:'views', bsReload:true

# spa watch (if custom spa file then watch it)
# ============================================
spaWatch = ->
if config.spa.custom
return createWatch config.spa.src.path, 'build spa', lang:config.spa.dist.file
defer = q.defer()
defer.resolve()
defer.promise

# register task
# =============
gulp.task "#{config.rb.prefix.task}watch", ->
@@ -149,7 +158,7 @@ module.exports = (gulp, config, browserSync) ->
# views
htmlWatch()
# spa
# createWatch config.src.rb.client.spa.path, 'spa.html', lang:'spa.html'
spaWatch()
]).done -> defer.resolve()
defer.promise

0 comments on commit 763322c

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