vtex / grunt-vtex

A Grunt convention-over-configuration meta-project
MIT License
8 stars 2 forks source link
srv-checkout-ui xp-shopping

Grunt VTEX

A Grunt convention-over-configuration meta-project.

The file index.coffee exposes only one function: generateConfig.
It receives your grunt, pkg (your package.json parsed object) and options.
It returns an object with configurations for all tasks used across projects in VTEX.

Your project should only define very specific customizations outside of this config.
This enforces uniformity and eases advancing configurations across every project simultaneously.

Important - contributors

If you are heavily altering a defined task or adding a new one, please bump the minor version.

Usage

In your Gruntfile:

GruntVTEX = require 'grunt-vtex'

module.exports = (grunt) ->
  pkg = grunt.file.readJSON 'package.json'

  options = {...}
  config = GruntVTEX.generateConfig grunt, pkg, options

  ## customize by altering config
  config.copy.main = {...}

  tasks = {...}

  grunt.initConfig config
  grunt.loadNpmTasks name for name of pkg.devDependencies when name[0..5] is 'grunt-'
  grunt.registerTask taskName, taskArray for taskName, taskArray of tasks

Options

Grunt command line options

Registered tasks

Example excerpt of a tags.json file:

{
    oms-ui: {
        stable: {
            2: "2.9.76"
        },
        beta: {
            2: "2.9.99-beta"
        }
    },
    license-manager-ui: {
        stable: {
            2: "2.1.23"
        },
        beta: {
            2: "2.1.23"
        }
    },
    vtex-id-ui: {
        stable: {
            2: "2.2.6",
            3: "3.2.29"
        },
        next: { },
        beta: {
            2: "2.2.6",
            3: "3.2.29-beta"
        },
        alpha: { }
    }
}

Using link

To develop two projects simultaneously, follow these steps:

This will symlink the build folder from the sibling into the build folder in this project.

You can also separate multiple projects with a comma, e.g.

    $ grunt --link front.shipping-data,front.cart

Using feature toggles

You may turn a feature on using the ft option:

    $ grunt --ft totem

Advanced devReplaceMap usage

devReplaceMap accepts a string or a function as a value for a key. In case of a function, it will receive three parameters:

The result of this function is passed on to the replace function. Therefore, you can return a function that handles the pattern matching!

e.g.:

featureToggleReplace = (features, symlink, tags) -> (match) ->
    if features?['totem'] then match else ''

linkReplace = (features, symlink, tags) -> (match, path, app, major) ->
    env = if grunt.option('stable') then 'stable' else 'beta'
    if symlink[app]
        console.log "link".blue, app, "->".blue, "local"
        return "/#{app}/#{path}"
    else
        version = tags[app][env][major]
        console.log "link".blue, app, "->".blue, version
        return "//io.vtex.com.br/#{app}/#{version}/#{path}"

devReplaceMap = {}
devReplaceMap["{{ 'checkout-custom.css' | legacy_file_url }}"] = '/arquivos/checkout-custom.css'
devReplaceMap["{{ 'checkout-custom.css' | file_url }}"] = '/files/checkout-custom.css'
devReplaceMap["{% if config.kiosk %}(\n|\rn|.)*\{% endif %}"] = featureToggleReplace
devReplaceMap["\\{\\{ \\'(.*)\\' \\| vtex_io: \\'(.*)\\', (\\d) \\}\\}"] = linkReplace

VTEX - 2014