Home

Awesome

srcbuild

Source file tree builder.

Usage

setup a lsp ( livescript + stylus + pug ) watcher:

require! <[@plotdb/srcbuild]>
srcbuild.lsp {base: 'web', i18n: ..., logger: ...}

where

These fields will be passed to all customized builders. Additionally, configurations in builder-specific fields will lso be passed to corresponding customized builders. For example, bundle field will be passed to bundle builder:

srcbuild.lsp {bundle: { ... /* this will be passed to bundle builder */ }, ...}

For lsp, there are 4 different builders:

See following sections for additional options in custom builders.

Custom Adapter

Extend base builder for a customized builder:

base = require("@plotdb/srcbuild").base

mybuild = (opt = {}) -> @init({srcdir: 'src', desdir: 'des'} <<< opt)
mybuild.prototype = Object.create(base.prototype) <<< {
  is-supported: (file) -> return true
  get-dependencies: (file) -> return []
  build: (files) -> # build will be called if is supported.
}

with following user-defined functions:

and the common options for init are as following:

check src/ext/lsc.ls or src/ext/pug.ls for example.

Options for custom builder

Except common options, each builder may support different options:

These options are constructor options for corresponding builder, e.g., for pug builder:

new pugbuild({ i18n: ... })

When using shorthands like srcbuild.lsp(...), you can also specify corresponding option in scope, such as:

srcbuild.lsp({
  base: '...', i18n: '...',
  pug: {intlbase: '...'}
});

common options will be overwritten by scoped options.

Using custom builders

Send adapters to watcher from getAdapter() of each custom builders:

require! <[@plotdb/srcbuild/dist/watch @plotdb/srcbuild/dist/ext/pug]>
pugbuilder = new pug(...)
watcher = new watch({adapters: [pugbuilder.getAdapter]})

By default, watcher watches the current working directory. Change watcher behavior with following constructor options:

ODB / On demand build

use watch.demand(target-file) to force rebuild by request. e.g.,

require! <[srcbuild]>
watch = srcbuild.lsp!

# this triggers rebuilding of `web/src/pug/index.pug` file.
watch.demand('web/static/index.html').then -> console.log "built."

target to source file mapping is done by resolve function in custom builder, so to use on demand build, resolve must be implemented.

i18n

use srcbuild.i18n to quickly setup an i18next object:

require! <[srcbuild]>
srcbuild.i18n(options)
  .then (i18n) -> srcbuild.lsp {i18n}

options is passed to i18next init function. Additional fields in options used by srcbuild.i18n:

When i18n object is provided, i18n data can be used in pug files via i18n function. e.g.,

div= i18n("my-key")

will show my-key content defined in locale corresponding default.yaml:

my-key: 這是我的鍵

To use a namespaced key, add : before key. For example:

div= i18n("new-ns:another-key")

will access to another-key in new-ns.yaml. Be sure to add your namespace name in ns field of i18n option:

"i18n": { ...  "ns": ["default", "new-ns"] }

additionally, use intlbase to wrap path with a i18n based relative path:

a(href=intlbase('/faq'))

Pug Extension

When building, we extend Pug via plugins and filters to support more features.

Pug include path

Use @ to include files in modules:

include @/ldview/dist/ldview.pug

Use @static to include files under static folder:

include @static/assets/sample.pug

Other paths starting with @ are reserved and will cause error when used.

Mixins

use script and css builtin mixins to load external script and css files:

+script({name: "module-name", version: "main", path: "somefile.js"})
+css({name: "module-name", version: "main", path: "somefile.js"})

where the fields of the parameters:

By default the above script mixin generates a script tag pointing to files under /assets/lib/<name>/<version>/<path>. You can customize the /assets/lib/ by calling libLoader.root(desiredPath).

Additionally, you can also use a list of modules:

+script([
  {name: "module-a", version: "0.0.1", path: "somefile.js"},
  {name: "module-b", version: "0.2.1", path: "another.js"},
  {name: "module-c", path: "with-default-version.js"},
  {name: "module-d", version: "with.default.path" },
  {name: "with-defer-async", defer: false, async: true}
  {name: "omit-everything"},
])

Use the second option object to specify additional parameters, including:

Filters

Following formats and filters are supported:

JS functions

Following functions are added:

Additional filters / functions

There are some additional i18n filters available if properly configured. See above for more information.

License

MIT