Git Product home page Git Product logo

makit's Introduction

makit

npm version downloads Build Status Coveralls dependencies semantic-release GitHub issues David David Dev DUB license Commitizen friendly

Purposes and Principles:

  • Minimal Concepts. It's intended to be a general purpose build automation tool just like GNU Make. Do not introduce unnecessary concept to keep it simple and stupid.
  • Less Restrictions. It should be as open as GNU Make, makit doesn't expect recipes return anything, doesn't even requrie a recipe for its rule definition, and doesn't care about the actual output of recipes.
  • JavaScript Style. Recipes can be written as callback style, Promise style or just synchronous style. Automatic variables are replaced by camelCased equivalents. Wildcard in static patterns are replaced by JavaScript RegExp and globs.

API Spec: https://searchfe.github.io/makit/modules/_index_.html

Get Started

Basically, the syntax is as simple as Makefiles but with a .js extension. A Makefile.js contains a set of rules, each of which consists of 3 parts:

  • target: either a filepath string, a glob string, or a RegExp object.
  • prerequisites: list of filepath strings, functions that return a list of strings, or list of strings and functions. functions here can be either sync or async.
  • recipe: an optional function, can be either sync or async.

Suppose we have a makefile.js containing the following contents:

const { rule } = require('makit')

rule('all', ['a1.min.js'])  // default rule

rule('a1.min.js', ['a.js'], function () {
    const src = readFileSync(this.dependencies[0], 'utf8')
    const dst = UglifyJS.minify(src).code
    writeFileSync(this.target, dst)
})

When we run makit(which is equivelant to make all cause all is the first rule), makit tries to make the target all which requires a1.min.js so the second rule will be applied firstly and its recipe is called to generate the target a1.min.js. The prerequisites for all has been fully resolved now and makit then tries to call its recipe, which is not defined for the above case so makit will just skip call the recipe and assume the target all is made successfully.

See /demo directory for a working demo. For more details see the typedoc for .rule()

Async (Promise & Callbacks)

When the recipe returns a Promise, that promise will be awaited.

rule('a1.min.js', ['a.js'], async function () {
    const src = await readFile(this.dependencies[0], 'utf8')
    const dst = UglifyJS.minify(src).code
    await writeFile(this.target, dst)
})
// equivelent to
rule('a1.min.js', ['a.js'], async ctx => {
    const src = await readFile(ctx.dependencies[0], 'utf8')
    const dst = UglifyJS.minify(src).code
    await writeFile(ctx.target, dst)
})

Callback style functions also work:

rule('clean', [], (ctx, done) => rimraf('{*.md5,*.min.js}', done))

Dynamic Dependencies

// `makit a.js.md5` will make a.js.md5 from a.js
rule('*.js.md5', ctx => ctx.target.replace('.md5', ''), async function () {
    const src = await this.readDependency(0)
    await this.writeTarget(md5(src))
})

Similarly, async prerequisites functions, i.e. functions of return type Promise<string> or Promise<string[]>, are also supported.

Matching Groups and Backward Reference

Makit uses extglob to match target names. Furthermore it's extended to support match groups which can be referenced in prerequisites.

// `makit output/app/app.js` will make app.js.md5 from a.ts
rule('(output/**)/(*).js', '$1/$2.ts', async function () {
    return this.writeTarget(tsc(await this.readDependency()))
})
make('output/app/app.js')

Dynamic Prerequisites

It's sometimes handy to call make() within the recipe, but global make() is not valid in recipes. For example the following rule is NOT valid:

const { rule, make } = require('makit')

rule('bundle.js', 'a.js', async (ctx) => {
    await make('b.js')
    const js = (await ctx.readFile('a.js')) + (await ctx.readFile('b.js'))
    ctx.writeTarget(js)
})

We introduce rude() to facilitate this situation, a ctx.make API is available inside the recipe of rude:

const { rude } = require('makit')

rude(
    'bundle.js', [], async ctx => {
        await ctx.make('a.js')
        await ctx.make('b.js')
        const js = (await ctx.readFile('a.js')) + (await ctx.readFile('b.js'))
        return ctx.writeTarget(js)
    }
)

A pseudo rule with bundle.js.rude.dep as target, the contents of which is the actual dependencies, will be generated for each rude().

makit's People

Contributors

harttle avatar semantic-release-bot avatar qiansc avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.