Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@plotdb/srcbuild

plotdb379MIT0.0.69

Source file tree builder.

readme

srcbuild

Source file tree builder.

Usage

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

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

where

  • base: root dir for src and static folders. default .
  • i18n: i18n object.
  • ignored: files to be ignored. in anymatch-compatible definition.
    • by default ['.git']
  • logger: optional. for logging output. use console.log by default.

    • sample logger with pino:

      require! <[@plotdb/srcbuild pino]> srcbuild.lsp({logger: pino({level: 'debug'})})

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:

  • lsc: build *.ls from src/ls to static/js.
  • stylus: build *.styl from src/styl to static/css.
  • pug: build *.pug from src/pug to static.
  • bundle: bundle css and js files

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:

  • is-supported(file): return true if file is supported by this builder, otherwise return false.
    • file: file name for file to be verified. Relative to cwd.
  • get-dependencies(file): return a list of files that this file depends on.
    • file: same as is-supported
  • build(files): should compile / generate target files of given file list files.
    • files: a list of objects corresponding to files to be compiled, with following fields:
      • file: path of the file to be built, relative to cwd.
      • mtime: timestamp of the modified time of this file. may be modified time of its dependencies.
  • purge(files): should remove generated files corresponding to files listed in files.
    • files: same as build.
  • resolve(file): return source file path for given target file file.
    • return null if the given target file can't be derived from any supported source files.

and the common options for init are as following:

  • base: root directory for srcbuild to run.
  • srcdir: directory for source files. should be relative to base. default src if omitted.
  • desdir: directory for built files. should be relative to base. default static if omitted.
  • logger: logger for log output. use console if omitted.
  • initScan: default true. if true, run a directory scanning for files to build when adapter is initing.

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:

  • pug:

    • intlbase: base dir to place i18n files. for example, intl part of /intl/zh-TW/index.html. default intl.
    • i18n: an optional i18n object having the same interface with i18next

      • when provided, enable i18n building with following additional features:

        • if buildIntl is set to true, build source files to locations by i18n config like /intl/zh-TW/index.html.
        • an additional function i18n will be available during pug compilation.

          • i18n(text): translate text based on the i18n object provided.
          • language(): return current language. ( e.g., zh-TW )
          • intlbase(p, lng): return a path to given p, based on current i18n ( or specified lng arg ) setup.
            • for example, intlbase('link', 'kr') may generate /intl/kr/link, based on he base dir config.
          • additionally, a pug filter i18n is also available, which can be used like:

            span:i18n translate this text

    • noView: default false. when true, js view files ( generated to viewdir ) won't be built.

    • buildIntl: default true. when true build locale-based files under static/intl/ and .view/intl
      • requires i18n; omitted if i18n is not available
    • viewdir: default .view. a directory for storing prebuilt pug files ( in .js format )
    • bundler: default null. Auto packing will be possible only if this is provided.
    • locals: additional local variables for pug context when compiling.
  • lsc:
    • useGlslify: default false. set to true if you need glslify of lsc files.
      • NOTE this is an experiment feature and may be removed ( move to standalone builder ) in the future.
  • bundle: bundle options. includes:
    • configFile: json file storing bundle configuration. optional.
    • relativePath: use relative path for paths in config file. default false. possible values:
      • false: all files in configFile are relative to current working directory.
      • true: all files in configFile are relative to the directory containing bundle.json
      • or, specific a path as the relative root.
    • manager: block manager, optional. required for @plotdb/block bundling.
      • can be either an block.manager object, or ...
      • a function returning such object which accepts an object with following fields as parameter:
        • base: the base dir of this bundle.
    • config: bundle configuration in following format: { "css": {
      "name": [ ... list of files to bundle together ]
      }, "js": {
      ...
      } }
  • asset: for copying asset files.
    • ext: array of file extensions to copy. default ["png", "gif", "jpg", "svg", "json"]

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:

  • adapters: array of adapters to use to handle file change events.
  • ignored: array of glob strings to ignore when watching for changes. by default [".git"].
  • root: directory, or array of directories to watch. by default ["."].
  • logger: optional. logger object with logging functions such as info, warn and error.

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:

  • enabled: true if i18n is enabled. default true

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:

  • name: module name
  • version: module version. default main, if omitted.
  • path: path of file to load. default index.min.js, if omitted.
  • defer: defer execution or not. default true if omitted.
  • async: async loading or not. default false if omitted.

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:

  • pack: experimental deprecated default false. Enable auto packing or not.
    • if true, enable auto packing which trigger bundling automatically to a filename from md5 of all script urls.
    • require bundler option in pugbuild constructor.
    • doesn't work with external urls.
    • there are still issues about rebuilding and build from view.
    • replaced by bundle filter, which runs in compile time.
  • min: default true. When true, use minimized packed file with pack option.

Filters

Following formats and filters are supported:

  • lsc: transpile content from livescript to JavaScript.
  • stylus: transpile content from stylus to CSS.
  • md: transpile content from markdown to HTML.
  • bundle: bundle files including js, css or block. usage sample:

    :bundle(options = {type: "block", files: [ { bid }, ... ]})

JS functions

Following functions are added:

  • md(code): convert markdown to HTML.
  • yaml(path): read yaml file and return object. (tentative)
  • yamls(path): read content of yaml files under path directory. (tentative)

Additional filters / functions

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

License

MIT

changelog

Change Log

v0.0.69

  • use buildIntl option to toggle intl folder generation.

v0.0.68

  • upgrade dependencies for vulnerability fixing

v0.0.67

  • support customized locals in pug builder

v0.0.66

  • fix bug: cached source file is not updated - it's mtime is not retrieved because getting desv mtime failure.

v0.0.65

  • fix bug: precompiled js isn't updated even if src file updated.

v0.0.64

  • fix bug: srcbuild-pug triggers a directory traverse which shouldn't happen.
  • add option initScan for toggling initial directory traverse.

v0.0.63

  • bundle should add both codesrc and src so the dependencies will be accurate

v0.0.62

  • use bundler class api for bundled file path if bundler is not provided in pugbuild

v0.0.61

  • auto set type when loading config if type is not defined
  • upgrade @plotdb/block, @plotdb/css and @plotdb/rescope dependencies for bug fixing

v0.0.60

  • upgrade dependencies @plotdb/block, @plotdb/rescope and @plotdb/csscope

v0.0.59

  • fix bug: bunlder filter should only work if bundler is provided and available

v0.0.58

  • separate ext/base init into initAdapter and initVars so we can update vars first
  • bundler ext:
    • accept function as manager parameter in bundler.
    • relativePath is now by default true
    • tweak error logging for bundling issue

v0.0.57

  • breaking change: in bundler, path of configFile should now be relative to base.
  • support block bundling with bundle filter
  • upgrade dependencies

v0.0.56

  • audit fix vulnerability about dependency minimatch
  • enable asset build directly in lsp

v0.0.55

  • remove logging when view rendering fails since it should be handled by express server
  • make error of view rendering fails a lderror with id 1033

v0.0.54

  • fix bug: bunlder is not added into adapter list in lsp, thus sometimes rebuild wont be triggered.
  • fix bug: pack option in css/script should by default use minimized file.
  • support min option to explicitly use unminimized file with pack option

v0.0.53

  • fix bug: ext/bundle fails if relative-path is false

v0.0.52

  • fix bug: precompiled pug js should still be run with custom option and api

v0.0.51

  • support auto packing with limitation.

v0.0.50

  • fix bug: multiple subtree features doesn't work, because we didn't add all adapters into watcher

v0.0.49

  • support building of multiple subtree

v0.0.48

  • support pug generation from outside of src dir

v0.0.47

  • disable pug compileDebug option by default
  • rebuild demo dir

v0.0.46

  • in watcher log, show correct watched directory instead of src
  • set _root with default value ["."] if option root is omitted.
  • force intlbase to return absolute path

v0.0.45

  • support customizable watching directory.

v0.0.44

  • support asset build ( static file copying )
  • support noView mode in pugbuild

v0.0.43

  • support language modifier in intlbase API

v0.0.42

  • trap bundle file parsing error and log, instead of crash directly

v0.0.41

  • fix bug: pug view engine doesn't support filters option

v0.0.40

  • support custom filter in ext/pug

v0.0.39

  • bug fix: pug build fails when pug file is empty

v0.0.38

  • upgrade @loadingio/debounce.js
  • tweak dependency range syntax
  • audit and fix cached-path-relative vulnerability

v0.0.37

  • bug fix: crash when iterating to a symlink pointing to a non-existed file.

v0.0.36

  • use @plotdb/colors to replace colors

v0.0.35

  • upgrade marked for vulnerability fixing

v0.0.34

  • support legacy syntax in libLoader for script loading
  • fix bug: libLoader.root doesn't work properly

v0.0.33

  • support relative path in bundle.json
  • add test case for bundler
  • use local script for testing

v0.0.32

  • bug fix: defer in libLoader should be by default true.
  • bug fix: stylus extension doesn't provide correct path for dependencies.

v0.0.31

  • bug fix: the script and css mixin supports should be done by postParse plugin instead of code modification.

v0.0.30

  • directly support script and css mixin in pug compiling.

v0.0.29

  • bug fix: bundle not rebuild when bundle config file updated
  • bug fix: bundler exception not caught

v0.0.28

  • totally remove compress option to prevent unwanted side effect of code removal

v0.0.27

  • dont compress unused in pug lsc filter for our potential use of custom script block design in @plotdb/block

v0.0.26

  • remove log

v0.0.25

  • pass ignored to adapter to bypass unnecessary files to save time
  • by default minimize javascript and css in pug filter.

v0.0.24

  • set doctype to html also in pug-cli to prevent t="t" generation.

v0.0.23

  • set doctype to html to prevent t="t" generation.

v0.0.22

  • add srcbuild-pug command for building pug with extapi
  • fix yaml loading issue by using load instead of safe-load.

v0.0.21

  • fix module resolving path bug

v0.0.20

  • resolve module path from basedir in ext/pug.

v0.0.19

  • bump stylus version to 0.55.0 for removing deprecated dependencies

v0.0.18

  • fix bug: lsc builder doesn't build unless glslify is enabled

v0.0.17

  • support json API for reading json file in pug

v0.0.16

  • fix bug: glslify transformed by browserify should have basedir from src file dir.

v0.0.15

  • upgrade path-parse to fix vulnerability
  • support glslify transfomration by option use-glslify

v0.0.14

  • add i18n.intlbase and i18n.langauge pug api and deprecate intlbase pug api.

v0.0.13

  • fix bug: incorrect parameter passing in view/pug to ext/pug

v0.0.12

  • add bundling sample code
  • fix bundling file path shown in log

v0.0.11

  • fix bug: desdir and base not passed to pug in pug view constructor

v0.0.10

  • fix bug: basedir should be opt.basedir

v0.0.9

  • support pug view engine for express

v0.0.8

  • fix bug: trying to get mtime of a non-existed file.
  • support options for files to ignore. by default, ignore .git folders.

v0.0.7

  • simplify log

v0.0.6

  • trap exception during log-dependencies and prevent from further building.
  • keep old dependency if log-dependencies fails.

v0.0.5

  • support bundling

v0.0.4

  • fix bug in ext/pug: basedir doesn't exist when initing, causing problem when getting dependencies. use path.resolve(srcdir) instead.

v0.0.3

  • fix typo

v0.0.2

  • fix bug: let pug use correct filename and basedir parameter. resolve pug files correctly