Skip to content

Commit

Permalink
feat(build option): add build option dist.paths.absolute (defaults to…
Browse files Browse the repository at this point in the history
… true) set to false to create relative urls, see build options doc for more info
  • Loading branch information
jyounce committed Jul 19, 2016
1 parent d14024d commit f0961b4
Show file tree
Hide file tree
Showing 18 changed files with 182 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ rapid-build.json (build options - can be cson, json or js file)
# dist.client[images|scripts|styles|test|views].dir = (string) defaults to property name # dist.client[images|scripts|styles|test|views].dir = (string) defaults to property name
# dist.client.bower.dir = (string) defaults to 'bower_components' # 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.libs.dir = (string) defaults to 'libs' = 3rd party libraries that aren't bower components
# dist.paths.absolute = (boolean) defaults to true = set to false to create relative urls instead of absolute for link tag's href, script tag's src and urls in css
# dist.server.dir = (string) defaults to 'server' # dist.server.dir = (string) defaults to 'server'
# dist.server.test.dir = (string) defaults to 'test' # dist.server.test.dir = (string) defaults to 'test'
# dist.server.fileName = (string) defaults to 'routes.js': this is the server's entry script # dist.server.fileName = (string) defaults to 'routes.js': this is the server's entry script
Expand Down
1 change: 1 addition & 0 deletions docs/rapid-build.cson
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ common:
client: client:
css: [ css: [
'libs/bootstrap/bootstrap.css' 'libs/bootstrap/bootstrap.css'
'libs/highlight/**/*.css'
] ]


dev: dev:
Expand Down
27 changes: 20 additions & 7 deletions docs/src/client/scripts/constants/build-opts-constant.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -230,12 +230,24 @@ angular.module('rapid-build').constant 'BUILD_OPTS', [
, ,
label: 'Property name example, \'scripts\'' label: 'Property name example, \'scripts\''
] ]
# , ,
# label: 'client.paths.absolute' label: 'client.paths.absolute'
# info: '@type boolean, @default true' info: '@type boolean, @default true'
# items: [ items: [
# label: 'By default, the build creates urls absolute to the root of the domain for:' label: 'Set to false to create relative urls.'
# ] ,
label: 'By default, the build creates
<a target="_blank" href="https://goo.gl/jK1i0L">urls absolute</a>
to the root of your domain for:'
items: [
label: 'link tag\'s href attribute'
info: 'for stylesheets'
,
label: 'script tag\'s src attribute'
,
label: 'and changes urls in css to absolute'
]
]
, ,
label: 'server.dir' label: 'server.dir'
info: '@type string, @default \'server\'' info: '@type string, @default \'server\''
Expand Down Expand Up @@ -274,7 +286,8 @@ angular.module('rapid-build').constant 'BUILD_OPTS', [
scripts: { dir: 'js' }, scripts: { dir: 'js' },
styles: { dir: 'css' }, styles: { dir: 'css' },
test: { dir: 'tests' }, test: { dir: 'tests' },
views: { dir: 'html' } views: { dir: 'html' },
paths: { absolute: false }
}, },
server: { server: {
dir: 'backend', dir: 'backend',
Expand Down
6 changes: 6 additions & 0 deletions src/config/configs/config-dist-and-src.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ module.exports = (config, options) ->


formatDist() # only one dist.dir formatDist() # only one dist.dir


# absolute client paths
# =====================
config.dist.client = {}
config.dist.client.paths = {}
config.dist.client.paths.absolute = if options.dist.client.paths.absolute is null then true else !!options.dist.client.paths.absolute

# logs # logs
# ==== # ====
# log.json config.dist, 'config.dist =' # log.json config.dist, 'config.dist ='
Expand Down
5 changes: 5 additions & 0 deletions src/config/options/option-dist-and-src.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module.exports = (config, options) ->
# ========================== # ==========================
options.dist.server.fileName = null unless isType.string options.dist.server.fileName options.dist.server.fileName = null unless isType.string options.dist.server.fileName


# absolute client paths
# =====================
options.dist.client.paths = {} unless isType.object options.dist.client.paths
options.dist.client.paths.absolute = null unless isType.boolean options.dist.client.paths.absolute

# return # return
# ====== # ======
options options
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/tasks.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ module.exports = (config, gulp={}) ->
# console.log "was called from task #{taskName} = #{calledFromTask}".yellow # console.log "was called from task #{taskName} = #{calledFromTask}".yellow
calledFromTask calledFromTask


startTask: (taskName) -> startTask: (taskPath, taskOpts={}) ->
# ALERT: gulp.start is supposedly going away in gulp 4 task = require "#{config.req.tasks}#{taskPath}"
gulp.start "#{config.rb.prefix.task}#{taskName}" taskOpts.taskCB = taskOpts.taskCB or ->
task config, gulp, taskOpts


addTask: (taskName, _path, opts={}) -> # very important method addTask: (taskName, _path, opts={}) -> # very important method
deps = getTaskDeps opts.deps deps = getTaskDeps opts.deps
Expand Down
3 changes: 2 additions & 1 deletion src/init/tasks.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ module.exports = (gulp, config) ->


# format # format
# ====== # ======
taskHelp.addTask 'absolute-css-urls', '/format/absolute-css-urls' taskHelp.addTask 'update-css-urls', '/format/update-css-urls'
taskHelp.addTask 'update-css-urls:prod', '/format/update-css-urls', calledFrom: 'minify-task'


# generate # generate
# ======== # ========
Expand Down
17 changes: 9 additions & 8 deletions src/plugins/gulp-absolute-css-urls.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ findAndReplace = (css, config, paths, opts, imports, formatTask, regX) ->
# Main (replace relative urls with absolute urls) # Main (replace relative urls with absolute urls)
# =============================================== # ===============================================
replaceCssUrls = (file, root, config, opts={}) -> replaceCssUrls = (file, root, config, opts={}) ->
css = file.contents.toString() css = file.contents.toString()
_root = format.root root _root = format.root root
abs = format.absPath file, _root abs = format.absPath file, _root
base = pathHelp.format file.base base = pathHelp.format file.base
rel = pathHelp.format file.relative rel = pathHelp.format file.relative
isRbPath = base.indexOf(opts.rbDistDir) isnt -1 rbDistPath = pathHelp.format opts.rbDistPath
paths = { root, abs, base, rel, isRbPath } isRbPath = base.indexOf(rbDistPath) isnt -1
imports = [] paths = { root, abs, base, rel, isRbPath }
imports = []


# urlPath = ../images/superheroes.png | /images/superheroes.png # urlPath = ../images/superheroes.png | /images/superheroes.png
css = findAndReplace css, config, paths, opts, imports, 'getCssUrl', urlRegX # match = url('../images/x.png') css = findAndReplace css, config, paths, opts, imports, 'getCssUrl', urlRegX # match = url('../images/x.png')
Expand Down
4 changes: 3 additions & 1 deletion src/tasks/build/build-spa-file.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ module.exports = (config, gulp, taskOpts={}) ->
getFilesJson = (jsonEnvFile) -> getFilesJson = (jsonEnvFile) ->
jsonEnvFile = path.join config.generated.pkg.files.path, jsonEnvFile jsonEnvFile = path.join config.generated.pkg.files.path, jsonEnvFile
moduleHelp.cache.delete jsonEnvFile moduleHelp.cache.delete jsonEnvFile
removePathSection = config.dist.app.client.dir
removePathSection += '/' unless config.dist.client.paths.absolute
files = require(jsonEnvFile).client files = require(jsonEnvFile).client
files = pathHelp.removeLocPartial files, config.dist.app.client.dir files = pathHelp.removeLocPartial files, removePathSection
files.styles = format.paths.to.html files.styles, 'styles', join: true, lineEnding: '\n\t' 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.scripts = format.paths.to.html files.scripts, 'scripts', join: true, lineEnding: '\n\t'
files files
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/common/common-client.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = (config, gulp, taskOpts={}) ->
"#{config.rb.prefix.task}compile-extra-sass:client" "#{config.rb.prefix.task}compile-extra-sass:client"
"#{config.rb.prefix.task}copy-extra-files:client" "#{config.rb.prefix.task}copy-extra-files:client"
] ]
"#{config.rb.prefix.task}absolute-css-urls" "#{config.rb.prefix.task}update-css-urls"
"#{config.rb.prefix.task}clean-rb-client" # if exclude.default.client.files "#{config.rb.prefix.task}clean-rb-client" # if exclude.default.client.files
"#{config.rb.prefix.task}build-files" "#{config.rb.prefix.task}build-files"
cb cb
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/compile/less.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (config, gulp, taskOpts={}) ->
plumber = require 'gulp-plumber' plumber = require 'gulp-plumber'
log = require "#{config.req.helpers}/log" log = require "#{config.req.helpers}/log"
lessHelper = require("#{config.req.helpers}/Less") config, gulp lessHelper = require("#{config.req.helpers}/Less") config, gulp
taskHelp = require("#{config.req.helpers}/tasks") config, gulp
forWatchFile = !!taskOpts.watchFile forWatchFile = !!taskOpts.watchFile
absCssUrls = require "#{config.req.tasks}/format/absolute-css-urls" if forWatchFile


# streams # streams
# ======= # =======
Expand All @@ -35,7 +35,7 @@ module.exports = (config, gulp, taskOpts={}) ->
.on 'data', (file) -> .on 'data', (file) ->
return unless forWatch return unless forWatch
watchFilePath = path.relative file.cwd, file.path watchFilePath = path.relative file.cwd, file.path
absCssUrls config, gulp, { watchFilePath } taskHelp.startTask '/format/update-css-urls', { watchFilePath }
.on 'end', -> .on 'end', ->
# console.log dest # console.log dest
defer.resolve() defer.resolve()
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/compile/sass.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (config, gulp, taskOpts={}) ->
plumber = require 'gulp-plumber' plumber = require 'gulp-plumber'
log = require "#{config.req.helpers}/log" log = require "#{config.req.helpers}/log"
sassHelper = require("#{config.req.helpers}/Sass") config, gulp sassHelper = require("#{config.req.helpers}/Sass") config, gulp
taskHelp = require("#{config.req.helpers}/tasks") config, gulp
forWatchFile = !!taskOpts.watchFile forWatchFile = !!taskOpts.watchFile
absCssUrls = require "#{config.req.tasks}/format/absolute-css-urls" if forWatchFile
extCss = '.css' extCss = '.css'


# streams # streams
Expand Down Expand Up @@ -39,7 +39,7 @@ module.exports = (config, gulp, taskOpts={}) ->
.on 'data', (file) -> .on 'data', (file) ->
return unless forWatchFile return unless forWatchFile
watchFilePath = path.relative file.cwd, file.path watchFilePath = path.relative file.cwd, file.path
absCssUrls config, gulp, { watchFilePath } taskHelp.startTask '/format/update-css-urls', { watchFilePath }
.on 'end', -> .on 'end', ->
# console.log dest # console.log dest
defer.resolve() defer.resolve()
Expand Down
7 changes: 4 additions & 3 deletions src/tasks/copy/copy-css.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = (config, gulp, taskOpts={}) ->
q = require 'q' q = require 'q'
log = require "#{config.req.helpers}/log" log = require "#{config.req.helpers}/log"
tasks = require("#{config.req.helpers}/tasks") config tasks = require("#{config.req.helpers}/tasks") config
taskHelp = require("#{config.req.helpers}/tasks") config, gulp
forWatchFile = !!taskOpts.watchFile forWatchFile = !!taskOpts.watchFile
absCssUrls = require "#{config.req.tasks}/format/absolute-css-urls" if forWatchFile


runTask = (src, dest) -> runTask = (src, dest) ->
defer = q.defer() defer = q.defer()
Expand All @@ -18,10 +18,11 @@ module.exports = (config, gulp, taskOpts={}) ->
# === # ===
api = api =
runSingle: -> # synchronously runSingle: -> # synchronously
defer = q.defer() defer = q.defer()
watchFilePath = taskOpts.watchFile.rbDistPath
_tasks = [ _tasks = [
-> runTask taskOpts.watchFile.path, taskOpts.watchFile.rbDistDir -> runTask taskOpts.watchFile.path, taskOpts.watchFile.rbDistDir
-> absCssUrls config, gulp, watchFilePath: taskOpts.watchFile.rbDistPath -> taskHelp.startTask '/format/update-css-urls', { watchFilePath }
] ]
_tasks.reduce(q.when, q()).done -> defer.resolve() _tasks.reduce(q.when, q()).done -> defer.resolve()
defer.promise defer.promise
Expand Down
11 changes: 7 additions & 4 deletions src/tasks/format/absolute-css-urls.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = (config, gulp, taskOpts={}) ->
promiseHelp.get() promiseHelp.get()


buildSpa = -> buildSpa = ->
taskHelp.startTask 'watch-build-spa' taskHelp.startTask '/watch/watch-build-spa'


# tasks # tasks
# ===== # =====
Expand All @@ -32,6 +32,7 @@ module.exports = (config, gulp, taskOpts={}) ->
clientDist = config.dist.app.client.dir clientDist = config.dist.app.client.dir
urlOpts = {} urlOpts = {}
urlOpts.rbDistDir = config.rb.prefix.distDir urlOpts.rbDistDir = config.rb.prefix.distDir
urlOpts.rbDistPath = config.dist.rb.client.styles.dir
urlOpts.prependPath = opts.prependPath urlOpts.prependPath = opts.prependPath
gulp.src src, { base } gulp.src src, { base }
.pipe absCssUrls clientDist, config, urlOpts .pipe absCssUrls clientDist, config, urlOpts
Expand All @@ -58,12 +59,14 @@ module.exports = (config, gulp, taskOpts={}) ->
# === # ===
api = api =
runSingle: -> runSingle: ->
clone = config.internal.getImports() clone = config.internal.getImports()
opts = prependPath: false, src: taskOpts.watchFilePath, watchFileBase: true opts = prependPath: false, src: taskOpts.watchFilePath, watchFileBase: true
runTask('app', 'styles', opts).done -> promise = runTask 'app', 'styles', opts
promise.done ->
imports = config.internal.getImports() imports = config.internal.getImports()
areEqual = arrayHelp.areEqual clone, imports, true areEqual = arrayHelp.areEqual clone, imports, true
buildSpa() unless areEqual buildSpa() unless areEqual
promise


runTask: -> # synchronously runTask: -> # synchronously
defer = q.defer() defer = q.defer()
Expand Down
80 changes: 80 additions & 0 deletions src/tasks/format/relative-css-urls.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,80 @@
module.exports = (config, gulp, taskOpts={}) ->
q = require 'q'
path = require 'path'
log = require "#{config.req.helpers}/log"
pathHelp = require "#{config.req.helpers}/path"
promiseHelp = require "#{config.req.helpers}/promise"
urlRegX = /url\s*\(\s*['"]?(.*?)['"]?\s*\)(?![^\*]*?\*\/)/g
importNoUrlRegX = /@import\s*?['"]+?(.*?)['"]+?(?![^\*]*?\*\/)/g
forWatchFile = !!taskOpts.watchFilePath

# constants
# =========
CLIENT_DIST_ROOT = path.join config.app.dir, config.dist.app.client.dir

# helpers
# =======
getRelativePath = (cssPath, absPath) ->
relPath = path.relative cssPath, absPath
# for windows
relPath = path.normalize relPath
relPath = pathHelp.swapBackslashes relPath

findAndReplace = (css, cssPath, regX) ->
css.replace regX, (match, urlPath) ->
isAbsolute = urlPath[0] is '/'
return match unless isAbsolute
absPath = path.join CLIENT_DIST_ROOT, urlPath
relPath = getRelativePath cssPath, absPath
relUrl = match.replace urlPath, relPath
# log.task relUrl, 'minor'
return relUrl

# tasks
# =====
runTask = (appOrRb, type, opts={}) ->
defer = q.defer()
src = opts.single if opts.single
base = if opts.single then config.dist.app.client.styles.dir else null
src = config.glob.dist[appOrRb].client[type][opts.glob] if opts.glob
dest = config.dist[appOrRb].client[type].dir
gulp.src src, { base }
.on 'data', (file) ->
css = file.contents
return unless css
css = css.toString()
cssPath = path.dirname file.path # css file directory path
css = findAndReplace css, cssPath, urlRegX # match = url('/images/x.png')
css = findAndReplace css, cssPath, importNoUrlRegX # match = @import '/imports/x.css'
file.contents = new Buffer css
.pipe gulp.dest dest
.on 'end', ->
defer.resolve()
defer.promise

# API
# ===
api =
runSingle: -> # for watch
runTask 'app', 'styles', single: taskOpts.watchFilePath

runMulti: (env) -> # async
defer = q.defer()
q.all([
runTask 'rb', 'bower', glob: 'css'
runTask 'rb', 'libs', glob: 'css'
runTask 'rb', 'styles', glob: 'all'
runTask 'app', 'bower', glob: 'css'
runTask 'app', 'libs', glob: 'css'
runTask 'app', 'styles', glob: 'all'
]).done ->
log.task 'changed all css urls to relative'
defer.resolve()
defer.promise

# return
# ======
return api.runSingle() if forWatchFile
api.runMulti taskOpts.env


33 changes: 33 additions & 0 deletions src/tasks/format/update-css-urls.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = (config, gulp, taskOpts={}) ->
q = require 'q'
log = require "#{config.req.helpers}/log"
promiseHelp = require "#{config.req.helpers}/promise"
taskHelp = require("#{config.req.helpers}/tasks") config, gulp

# API
# ===
api =
runTask: -> # synchronously
return promiseHelp.get() unless config.build.client
calledFromMinTask = taskOpts.calledFrom is 'minify-task'
skipInCommonTask = config.env.is.prod and not calledFromMinTask
defer = q.defer()
tasks = [
-> # only run in common-client task
return promiseHelp.get() if calledFromMinTask
taskHelp.startTask '/format/absolute-css-urls', taskOpts

-> # skip in common-client task if prod build
return promiseHelp.get() if config.dist.client.paths.absolute
return promiseHelp.get() if skipInCommonTask
taskHelp.startTask '/format/relative-css-urls', taskOpts
]
tasks.reduce(q.when, q()).done ->
# log.task 'updated css urls', 'minor'
defer.resolve()
defer.promise

# return
# ======
api.runTask()

1 change: 1 addition & 0 deletions src/tasks/minify/minify-client.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = (config, gulp, taskOpts={}) ->
"#{config.rb.prefix.task}inline-css-imports" "#{config.rb.prefix.task}inline-css-imports"
"#{config.rb.prefix.task}cleanup-client" "#{config.rb.prefix.task}cleanup-client"
"#{config.rb.prefix.task}css-file-split" "#{config.rb.prefix.task}css-file-split"
"#{config.rb.prefix.task}update-css-urls:prod"
"#{config.rb.prefix.task}build-spa:prod" "#{config.rb.prefix.task}build-spa:prod"
"#{config.rb.prefix.task}minify-spa" "#{config.rb.prefix.task}minify-spa"
"#{config.rb.prefix.task}cache-bust" "#{config.rb.prefix.task}cache-bust"
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/watch/watch.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = (config, gulp) ->


buildSpa: -> buildSpa: ->
return promiseHelp.get() unless config.build.client return promiseHelp.get() unless config.build.client
taskHelp.startTask 'watch-build-spa' taskHelp.startTask '/watch/watch-build-spa'


browserSync: -> browserSync: ->
return unless config.build.server return unless config.build.server
Expand Down

0 comments on commit f0961b4

Please sign in to comment.