Skip to content

Commit

Permalink
feat(typescript): add typescript support, must be enabled via build o…
Browse files Browse the repository at this point in the history
…ption typescript[client|server].enable
  • Loading branch information
jyounce committed Aug 8, 2016
1 parent 3cbd8ee commit 1a94a60
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ rapid-build.json (build options - can be cson, json or js file)
# extra.compile.server[less|sass] = (array of strings) = file paths: additional files to compile to dist/server that the build didn't compile # extra.compile.server[less|sass] = (array of strings) = file paths: additional files to compile to dist/server that the build didn't compile
# extra.minify.client[css|js] = (array of strings) = file paths: additional files to minify in dist/client that the build didn't minify (by default, the build does not minify files in libs or bower_components) # extra.minify.client[css|js] = (array of strings) = file paths: additional files to minify in dist/client that the build didn't minify (by default, the build does not minify files in libs or bower_components)
# security.client.clickjacking = (boolean) defaults to true = includes a clickjacking defense script in the default spa.html (set to false to disable) # security.client.clickjacking = (boolean) defaults to true = includes a clickjacking defense script in the default spa.html (set to false to disable)
# compile.typescript[client|server].enable = (boolean) defaults to false = Set to true to enable client and or server typescript compiler.
# compile.typescript.client.entries = (array of strings) defaults to ['main.ts'] = Entry point(s) of your app. This file(s) is required. Browserify is used for module loading. Must use commonjs for typescript's module code generation.
# ====================================================================================================================================================================================================================================================================================================== # ======================================================================================================================================================================================================================================================================================================
``` ```


Expand Down
5 changes: 3 additions & 2 deletions docs/src/client/scripts/constants/about-constant.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ angular.module('rapid-build').constant 'ABOUT', [
label: 'css, less and sass' label: 'css, less and sass'
info: 'client' info: 'client'
, ,
label: 'js, <a target="_blank" href="http://babeljs.io/">es6</a> label: 'js, <a target="_blank" href="http://babeljs.io/">es6</a>,
and coffeescript' coffeescript and
<a href="/build-options#compile.typescript[client|server].enable">typescript</a>'
info: 'client and server' info: 'client and server'
] ]
, ,
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ angular.module('rapid-build').constant 'BUILD_COMMON_TASKS', [
, ,
label: '<a target="_blank" href="http://babeljs.io/">es6</a> &#8594; js' label: '<a target="_blank" href="http://babeljs.io/">es6</a> &#8594; js'
info: '@dir client and server' info: '@dir client and server'
,
label: '<a href="/build-options#compile.typescript[client|server].enable">typescript</a> &#8594; js'
info: '@dir client and server'
, ,
label: 'less &#8594; css' label: 'less &#8594; css'
info: '@dir client' info: '@dir client'
Expand Down
56 changes: 56 additions & 0 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 @@ -191,6 +191,62 @@ angular.module('rapid-build').constant 'BUILD_OPTS', [
} }
} }
""" """
, # =================================================================
label: 'compile'
icon: 'fa-cog'
html:
class: items2: list: 'small'
items: [
label: 'typescript[client|server].enable'
info: '@type boolean, @default false'
items: [
label: 'Set to true to enable client and or server typescript compiler.'
,
label: 'Place client typescript files in src/client/scripts/ directory.'
icon: 'fa-exclamation-circle'
,
label: 'Place server typescript files in src/server/ directory.'
icon: 'fa-exclamation-circle'
]
,
label: 'typescript.client.entries'
info: '@type array of strings, @default [\'main.ts\']'
items: [
label: 'Entry point(s) of your app.'
icon: 'fa-exclamation-circle'
items: [
label: 'This file(s) is required.'
,
label: 'File path(s) must be relative to scripts directory.'
]
,
label: '<a target="_blank" href="http://browserify.org/">Browserify</a>
is used for module loading.'
,
label: 'Must use commonjs for typescript\'s
<a target="_blank" href="https://www.typescriptlang.org/docs/handbook/compiler-options.html">module code generation</a>.'
]
]
example:
label: 'Example'
info: '<a href="/getting-started#rapid-build.json">for rapid-build.json</a>'
lang: 'js'
size: 'small'
code: """
{
compile: {
typescript: {
client: {
enable: true,
entries: ['init.ts']
},
server: {
enable: true
}
}
}
}
"""
, # ================================================================= , # =================================================================
label: 'dist' label: 'dist'
icon: 'fa-cog' icon: 'fa-cog'
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ angular.module('rapid-build').constant 'GETTING_STARTED', [
│ ├── libs/ │ ├── libs/
│ │ └── # (3rd party libraries that aren't bower components) │ │ └── # (3rd party libraries that aren't bower components)
│ ├── scripts/ │ ├── scripts/
│ │ └── coffee, es6 or js │ │ └── coffee, es6, js or typescript
│ ├── styles/ │ ├── styles/
│ │ └── css, less, sass or scss │ │ └── css, less, sass or scss
│ ├── test/ │ ├── test/
Expand All @@ -72,7 +72,7 @@ angular.module('rapid-build').constant 'GETTING_STARTED', [
└── server/ └── server/
├── test/ ├── test/
│ └── coffee, es6 or js │ └── coffee, es6 or js
└── routes.{coffee,es6,js} # (optional, see build options dist.server.fileName) └── routes.{coffee,es6,js,ts} # (optional, see build options dist.server.fileName)
nodes_modules/ # (generated folder via package.json) nodes_modules/ # (generated folder via package.json)
bower.json bower.json
package.json # (required) package.json # (required)
Expand Down
1 change: 1 addition & 0 deletions docs/src/client/views/mains/build-options.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ <h1>
{ caption: 'angular', url: '#angular' }, { caption: 'angular', url: '#angular' },
{ caption: 'browser', url: '#browser' }, { caption: 'browser', url: '#browser' },
{ caption: 'build', url: '#build' }, { caption: 'build', url: '#build' },
{ caption: 'compile', url: '#compile' },
{ caption: 'dist', url: '#dist' }, { caption: 'dist', url: '#dist' },
{ caption: 'exclude', url: '#exclude' }, { caption: 'exclude', url: '#exclude' },
{ caption: 'extra', url: '#extra' }, { caption: 'extra', url: '#extra' },
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"body-parser": "1.15.2", "body-parser": "1.15.2",
"bower": "1.7.9", "bower": "1.7.9",
"browser-sync": "2.13.0", "browser-sync": "2.13.0",
"browserify": "13.1.0",
"coffee-script": "1.10.0", "coffee-script": "1.10.0",
"colors": "1.1.2", "colors": "1.1.2",
"commander": "2.9.0", "commander": "2.9.0",
Expand Down Expand Up @@ -81,7 +82,8 @@
"gulp-sass": "2.3.2", "gulp-sass": "2.3.2",
"gulp-sequence": "0.4.5", "gulp-sequence": "0.4.5",
"gulp-template": "4.0.0", "gulp-template": "4.0.0",
"gulp-uglify": "1.5.4", "gulp-typescript": "2.13.6",
"gulp-uglify": "2.0.0",
"gulp-util": "3.0.7", "gulp-util": "3.0.7",
"gulp-watch": "4.3.8", "gulp-watch": "4.3.8",
"http-proxy-middleware": "0.17.0", "http-proxy-middleware": "0.17.0",
Expand All @@ -103,7 +105,10 @@
"postcss-import": "7.1.3", "postcss-import": "7.1.3",
"q": "1.4.1", "q": "1.4.1",
"sha1": "1.1.1", "sha1": "1.1.1",
"through2": "2.0.1" "through2": "2.0.1",
"tsify": "1.0.3",
"vinyl-source-stream": "1.1.0",
"watchify": "3.7.0"
}, },
"keywords": [ "keywords": [
"build system", "build system",
Expand Down
1 change: 1 addition & 0 deletions src/config/config-options.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = (config, options) ->
# format options # format options
# ============== # ==============
options = require("#{config.req.config.options}/option-build") config, options options = require("#{config.req.config.options}/option-build") config, options
options = require("#{config.req.config.options}/option-compile") config, options
options = require("#{config.req.config.options}/option-dist-and-src") config, options options = require("#{config.req.config.options}/option-dist-and-src") config, options
options = require("#{config.req.config.options}/option-ports") config, options options = require("#{config.req.config.options}/option-ports") config, options
options = require("#{config.req.config.options}/option-order") config, options options = require("#{config.req.config.options}/option-order") config, options
Expand Down
1 change: 1 addition & 0 deletions src/config/config.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = (rbDir, options) ->
config = require("#{config.req.config.configs}/config-angular") config, options config = require("#{config.req.config.configs}/config-angular") config, options
config = require("#{config.req.config.configs}/config-spa") config, options config = require("#{config.req.config.configs}/config-spa") config, options
config = require("#{config.req.config.configs}/config-exclude") config, options config = require("#{config.req.config.configs}/config-exclude") config, options
config = require("#{config.req.config.configs}/config-compile") config, options
config = require("#{config.req.config.configs}/config-extra") config config = require("#{config.req.config.configs}/config-extra") config
config = require("#{config.req.config.configs}/config-extra-copy") config, options config = require("#{config.req.config.configs}/config-extra-copy") config, options
config = require("#{config.req.config.configs}/config-extra-compile") config, options config = require("#{config.req.config.configs}/config-extra-compile") config, options
Expand Down
34 changes: 34 additions & 0 deletions src/config/configs/config-compile.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = (config, options) ->
log = require "#{config.req.helpers}/log"
isType = require "#{config.req.helpers}/isType"
test = require("#{config.req.helpers}/test")()

# init compile
# ============
compile =
typescript:
client:
enable: options.compile.typescript.client.enable
entries: options.compile.typescript.client.entries
server:
enable: options.compile.typescript.server.enable

compile.typescript.client.entries = ['main.ts'] if isType.null compile.typescript.client.entries

# add compile to config
# =====================
config.compile = compile

# logs
# ====
# log.json compile, 'compile ='

# tests
# =====
test.log 'true', config.compile, 'add compile to config'

# return
# ======
config


7 changes: 7 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 @@ -15,6 +15,7 @@ module.exports = (config, options) ->
scripts: 'scripts' scripts: 'scripts'
styles: 'styles' styles: 'styles'
test: 'test' test: 'test'
typings: 'typings'
views: 'views' views: 'views'


file = file =
Expand Down Expand Up @@ -67,6 +68,8 @@ module.exports = (config, options) ->
dir: o.clientStyles or dir.styles dir: o.clientStyles or dir.styles
test: test:
dir: o.clientTest or dir.test dir: o.clientTest or dir.test
typings:
dir: dir.typings
views: views:
dir: o.clientViews or dir.views dir: o.clientViews or dir.views
server: server:
Expand All @@ -76,7 +79,11 @@ module.exports = (config, options) ->
dir: o.serverDir or dir.scripts dir: o.serverDir or dir.scripts
test: test:
dir: o.serverTest or dir.test dir: o.serverTest or dir.test
typings:
dir: dir.typings
if loc is 'dist' if loc is 'dist'
delete info.client.typings
delete info.server.typings
unless isApp unless isApp
info.client.dirName = config.rb.prefix.distDir info.client.dirName = config.rb.prefix.distDir
info.server.dirName = config.rb.prefix.distDir info.server.dirName = config.rb.prefix.distDir
Expand Down
9 changes: 8 additions & 1 deletion src/config/configs/config-globs.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = (config) ->
js: 'js' js: 'js'
less: 'less' less: 'less'
sass: 'sass,scss' sass: 'sass,scss'
ts: 'ts' # typescript src files
defs: 'd.ts' # typescript definition files


getExts = (_exts) -> # _exts = string array getExts = (_exts) -> # _exts = string array
_exts = _exts.split ',' _exts = _exts.split ','
Expand All @@ -40,6 +42,8 @@ module.exports = (config) ->
js: "/**/*.#{exts.js}" js: "/**/*.#{exts.js}"
less: "/**/*.#{exts.less}" less: "/**/*.#{exts.less}"
sass: "/**/*.{#{exts.sass}}" sass: "/**/*.{#{exts.sass}}"
ts: "/**/*.#{exts.ts}"
defs: "/**/*.#{exts.defs}"
bustFiles: "/**/*.{#{getExts 'css,js,images'}}" bustFiles: "/**/*.{#{getExts 'css,js,images'}}"
bustRefs: "/**/*.{#{getExts 'html,css,js'}}" bustRefs: "/**/*.{#{getExts 'html,css,js'}}"


Expand All @@ -60,7 +64,7 @@ module.exports = (config) ->
addGlob = (loc, type, langs, includeBower, includeLibs) -> addGlob = (loc, type, langs, includeBower, includeLibs) ->
for own k1, v1 of glob[loc] for own k1, v1 of glob[loc]
for own k2, v2 of v1 for own k2, v2 of v1
continue if k2 is 'server' and ['scripts','test'].indexOf(type) is -1 continue if k2 is 'server' and ['scripts','test','typings'].indexOf(type) is -1
continue if k2 is 'server' and (includeBower or includeLibs) continue if k2 is 'server' and (includeBower or includeLibs)
v2[type] = {} unless isType.object v2[type] v2[type] = {} unless isType.object v2[type]
for v3 in langs for v3 in langs
Expand Down Expand Up @@ -88,11 +92,14 @@ module.exports = (config) ->
addGlob 'src', 'scripts', ['js'] addGlob 'src', 'scripts', ['js']
addGlob 'src', 'scripts', ['coffee'] addGlob 'src', 'scripts', ['coffee']
addGlob 'src', 'scripts', ['es6'] addGlob 'src', 'scripts', ['es6']
addGlob 'src', 'scripts', ['ts']
addGlob 'src', 'typings', ['defs']
addGlob 'src', 'styles', ['css'] addGlob 'src', 'styles', ['css']
addGlob 'src', 'styles', ['less'] addGlob 'src', 'styles', ['less']
addGlob 'src', 'styles', ['sass'] addGlob 'src', 'styles', ['sass']
addGlob 'src', 'test', ['css', 'js'] addGlob 'src', 'test', ['css', 'js']
addGlob 'src', 'test', ['coffee'] addGlob 'src', 'test', ['coffee']
addGlob 'src', 'test', ['ts']
addGlob 'src', 'test', ['es6'] addGlob 'src', 'test', ['es6']
addGlob 'src', 'views', ['html'] addGlob 'src', 'views', ['html']


Expand Down
23 changes: 23 additions & 0 deletions src/config/options/option-compile.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = (config, options) ->
isType = require "#{config.req.helpers}/isType"

# init compile options
# ====================
compile = options.compile
compile = {} unless isType.object compile
compile.typescript = {} unless isType.object compile.typescript
compile.typescript.client = {} unless isType.object compile.typescript.client
compile.typescript.server = {} unless isType.object compile.typescript.server
compile.typescript.client.enable = false unless isType.boolean compile.typescript.client.enable
compile.typescript.server.enable = false unless isType.boolean compile.typescript.server.enable
compile.typescript.client.entries = null unless isType.array compile.typescript.client.entries

# add compile options
# ===================
options.compile = compile

# return
# ======
options


50 changes: 50 additions & 0 deletions src/helpers/TsProject.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,50 @@
# TYPESCRIPT PROJECT FACTORY
# needed for watch to use same ts project
# =======================================
fs = require 'fs'

class TsProject
# typescript instance locations
# =============================
instances = {}

# private
# =======
help =
fileExists: (_path) ->
try fs.lstatSync(_path).isFile()
catch e then false

hasTsConfig: (tsconfigPath) ->
hasTsConfig = @fileExists tsconfigPath
# console.log "Has tsconfig.json: #{hasTsConfig}".alert
hasTsConfig

# @id = location (ex: client or server)
# @ts = gulp-typescript
# @tsconfig = tsconfig.json path
# @opts = ts project options
# ===========================================
class Typescript
constructor: (@id, @ts, @tsconfig, @opts={}) ->
@setProject()

setProject: ->
if help.hasTsConfig @tsconfig
@project = @ts.createProject @tsconfig, @opts
else
@project = @ts.createProject @opts
@

# static method to retrieve an
# instance or create a new one
# ============================
@get: (id, ts, tsconfig, opts) ->
if not instances[id] # create new instance
# console.log "TS INSTANCE ID: #{id}".alert
instances[id] = new Typescript id, ts, tsconfig, opts
instances[id]

# Export Class!
# =============
module.exports = TsProject
5 changes: 4 additions & 1 deletion src/helpers/log.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ module.exports =


watch: (taskName, file, opts={}) -> watch: (taskName, file, opts={}) ->
taskName = opts.logTaskName or taskName taskName = opts.logTaskName or taskName
@task "#{taskName} #{file.event}: #{file.path}", 'minor' @task "#{taskName} #{file.event}: #{file.path}", 'minor'

watchTS: (paths) ->
@task "typescript changed: #{_path}", 'minor' for _path in paths
14 changes: 8 additions & 6 deletions src/init/tasks.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ module.exports = (gulp, config) ->


# compile # compile
# ======= # =======
taskHelp.addTask 'coffee:client', '/compile/coffee', loc: 'client' taskHelp.addTask 'coffee:client', '/compile/coffee', loc: 'client'
taskHelp.addTask 'coffee:server', '/compile/coffee', loc: 'server' taskHelp.addTask 'coffee:server', '/compile/coffee', loc: 'server'
taskHelp.addTask 'es6:client', '/compile/es6', loc: 'client' taskHelp.addTask 'es6:client', '/compile/es6', loc: 'client'
taskHelp.addTask 'es6:server', '/compile/es6', loc: 'server' taskHelp.addTask 'es6:server', '/compile/es6', loc: 'server'
taskHelp.addTask 'less', '/compile/less' taskHelp.addTask 'typescript:client', '/compile/typescript-client'
taskHelp.addTask 'sass', '/compile/sass' taskHelp.addTask 'typescript:server', '/compile/typescript-server'
taskHelp.addTask 'less', '/compile/less'
taskHelp.addTask 'sass', '/compile/sass'


# config # config
# ====== # ======
Expand Down
19 changes: 19 additions & 0 deletions src/plugins/gulp-update-file-base.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
PLUGIN_NAME = 'gulp-update-file-base'
through = require 'through2'
gutil = require 'gulp-util'
PluginError = gutil.PluginErrors

# Plugin level function(dealing with files)
# Effects gulp.dest(dest).
# Ensure file copies to correct dist location.
# ============================================
updateFileBase = (fileBase) ->
through.obj (file, enc, cb) ->
return cb null, file unless fileBase
return cb null, file unless file
return cb new PluginError PLUGIN_NAME, 'streaming not supported' if file.isStream()
return cb null, file unless file.isBuffer()
file.base = fileBase
cb null, file

module.exports = updateFileBase
Loading

0 comments on commit 1a94a60

Please sign in to comment.