less

package module
v0.0.0-...-b193abc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2020 License: Apache-2.0 Imports: 9 Imported by: 1

README

less-go

Less compiler for Golang

Builds CSS using original Less compiler and goja for a true cross-platform Less solution.

The original project less-go used Duktape instead of goja.

Status

This project is "stable" but also very much a work in progress. Expect changes as required, and feel free to submit pull requests to improve it.

Installation

go get github.com/tystuyfzand/less-go

Command Line usage

cd $GOPATH/src/github.com/tystuyfzand/less-go/lessc
go get
go build
./lessc --input="inputFile" --output="outputFile"
./lessc -i inputFile -o outputFile

Examples:

./lessc --input="./styles.less" --output="./styles.css"
./lessc -i styles.less -o styles.css

More about usage you can see in cli help:

./lessc -h

Programmatic usage

import "github.com/tystuyfzand/less-go"

func main() {
	output, err := less.RenderFile("./styles.less", map[string]interface{}{"compress": true})
}
Function reference
Render(input string, mods ...map[string]interface{}) (string, error)

Renders Less as raw input and generates output CSS.

RenderFile(input string, mods ...map[string]interface{}) (string, error)

Renders Less files and generates output CSS.

SetReader(customReader Reader)
type Reader interface {
    ReadFile(string) ([]byte, error)
}

Sets a custom reader for .less files. You can use it to replace standard input from file system to another. Example:

type LessReader struct{}

var lessFiles = map[string][]byte{"styles": []byte{".class { width: (1 + 1) }"}}

func (LessReader) ReadFile(path string) ([]byte, error) {
    lessFile, ok := lessFiles[path]
    if !ok {
    	return "", errors.New("path not found")
    }
    return lessFile, nil
}

func main() {
    less.SetReader(LessReader)
    ...
}

Updating Less

Get the latest version of less.js, navigate to packages/less/src.

With npm installed, run:

npm install @babel/core @babel/cli @babel/preset-env

Then, run babel to get a copy that goja can run:

npx babel less/ --out-dir /path/to/less-go/assets/less/ --presets=@babel/preset-env

And finally, re-build the packr files:

packr2

Current limitations

Unlike the original version, this doesn't use any C Javascript engine. It will run on any platform, and cross compile just fine.

CLI interface doesn't support options

Documentation

Overview

You can use the "packr clean" command to clean up this, and any other packr generated files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(input string, mods ...map[string]interface{}) (string, error)

Render renders the input as raw less using a shared compiler.

func RenderFile

func RenderFile(input string, mods ...map[string]interface{}) (string, error)

RenderFile renders the input as a file using a shared compiler.

func SetReader

func SetReader(r Reader)

SetReader sets the default compiler's reader

Types

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

func NewCompiler

func NewCompiler() (*Compiler, error)

NewCompiler creates a new compiler with a set reader/writer. Compilers are thread-safe, only because they use a shared mutex. If using from multiple goroutines regularly, it's probably better to use a pool.

func (*Compiler) Render

func (c *Compiler) Render(input string, mods ...map[string]interface{}) (string, error)

Render renders input as raw less

func (*Compiler) RenderFile

func (c *Compiler) RenderFile(input string, mods ...map[string]interface{}) (string, error)

RenderFile renders the input as a file path, using the Reader interface.

func (*Compiler) SetReader

func (c *Compiler) SetReader(customReader Reader)

SetReader sets the reader interface to provide files

type Reader

type Reader interface {
	ReadFile(string) ([]byte, error)
}

type Writer

type Writer interface {
	WriteFile(string, []byte, os.FileMode) error
}

Directories

Path Synopsis
You can use the "packr2 clean" command to clean up this, and any other packr generated files.
You can use the "packr2 clean" command to clean up this, and any other packr generated files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL