Skip to content
Permalink
Browse files

feat(build option): add build option spa[scripts|styles].attrs for ab…

…ility to add or modify attrs on link and script tags in spa.html
  • Loading branch information
jyounce committed Apr 19, 2017
1 parent 0dcc909 commit a857801605988a0c81960c30d6c63ad3166a30fe
@@ -65,6 +65,14 @@ module.exports = (config, options) ->
# ============
spa.placeholders = options.spa.placeholders or []

# scripts and styles
# ==================
spa.scripts =
attrs: options.spa.scripts.attrs or null

spa.styles =
attrs: options.spa.styles.attrs or null

# add spa to config
# =================
config.spa = spa
@@ -13,6 +13,10 @@ module.exports = (config, options) ->
spa.dist = {} unless isType.object spa.dist
spa.dist.fileName = null unless isType.string spa.dist.fileName
spa.placeholders = null unless isType.array spa.placeholders
spa.scripts = {} unless isType.object spa.scripts
spa.styles = {} unless isType.object spa.styles
spa.scripts.attrs = null unless isType.object spa.scripts.attrs
spa.styles.attrs = null unless isType.object spa.styles.attrs

# add spa options
# ===============
@@ -5,19 +5,50 @@ module.exports = ->
paths: (paths, opts={}) ->
opts.join = opts.join or false
opts.htmlTag = opts.htmlTag or null
opts.attrs = opts.attrs or null # tag attrs
opts.lineEnding = opts.lineEnding or '\n'
_paths = []
paths.forEach (_path) =>
_paths.push @html[opts.htmlTag] _path if opts.htmlTag
for _path in paths
break unless opts.htmlTag
tag = @html[opts.htmlTag] _path, opts
continue unless tag
_paths.push tag
_paths = _paths.join opts.lineEnding if opts.join
_paths

html:
styles: (_path) ->
"<link rel=\"stylesheet\" href=\"#{_path}\" />"
# null attr produces valueless attribute
# ======================================
getStringAttrs: (attrs) ->
strAttrs = ''
for attr, val of attrs
continue if val is undefined
strAttrs += ' '
strAttrs += if val is null then "#{attr}" else "#{attr}=\"#{val}\""
strAttrs

scripts: (_path) ->
"<script src=\"#{_path}\"></script>"
mergeAttrs: (attrs, opts={}) -> # mutator
return attrs unless opts.attrs
Object.assign attrs, opts.attrs

styles: (_path, opts={}) -> # opts.attrs overide default attrs
attrs = # defaults
rel: 'stylesheet'
href: _path

@mergeAttrs attrs, opts
strAttrs = @getStringAttrs attrs
return unless strAttrs
tag = "<link#{strAttrs}>"

scripts: (_path, opts={}) -> # opts.attrs overide default attrs
attrs = # defaults
src: _path

@mergeAttrs attrs, opts
strAttrs = @getStringAttrs attrs
return unless strAttrs
tag = "<script#{strAttrs}></script>"

# return
# ======
@@ -29,4 +60,6 @@ module.exports = ->
to:
html: (paths, htmlTag, opts={}) ->
opts.htmlTag = htmlTag
get.paths paths, opts
get.paths paths, opts


@@ -51,8 +51,8 @@ module.exports = (config, gulp, taskOpts={}) ->
removePathSection += '/' unless config.dist.client.paths.absolute
files = require(jsonEnvFile).client
files = pathHelp.removeLocPartial files, removePathSection
files.styles = format.paths.to.html files.styles, 'styles', join: true, lineEnding: '\n\t'
files.scripts = format.paths.to.html files.scripts, 'scripts', join: true, lineEnding: '\n\t'
files.styles = format.paths.to.html files.styles, 'styles', join: true, lineEnding: '\n\t', attrs: config.spa.styles.attrs
files.scripts = format.paths.to.html files.scripts, 'scripts', join: true, lineEnding: '\n\t', attrs: config.spa.scripts.attrs
files

getClickjackingTpl = ->

0 comments on commit a857801

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