Skip to content

Commit

Permalink
Allow to use a custom config file
Browse files Browse the repository at this point in the history
  • Loading branch information
11joselu committed Apr 24, 2019
1 parent f68f530 commit d7a4206
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 30 deletions.
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ npm install less-incremental-build --save-dev
<h2>Usage</h2>

```bash
node less-incremental-build --src <main_file>.less --output <output>.css
```

### With npx

```bash
npx less-incremental-build --src <main_file>.less --output <output>.css
npm less-incremental-build --src <main_file>.less --output <output>.css
```

## API
Expand All @@ -41,6 +35,29 @@ Type: `string`

Output file to compile less styles. Should be a css file (If not exists, it will be created)

### config

Type: `string`

Path of your custom config file. Do not pass `src` or `output` as argument, should be added inside your custom config file.

<h2>Config File</h2>

`watcher.config.js`

```javascript
module.exports = {
src: './static/less/main.less',
output: './build/styles.css',
// Add less Options
lessOptions: { sourceMap: { sourceMapFileInline: true } },
};
```

```bash
npm less-incremental-build --config watcher.config.js
```

<h2>Maintainers</h2>

<table>
Expand Down
44 changes: 42 additions & 2 deletions __tests__/compiler/Renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('Test Renderer', () => {
.to.be.an('object')
.to.have.own.property('paths')
.that.is.a('array');
expect(renderer.getLessPlugin()).to.be.an('array').that.is.not.empty;
expect(renderer.getLessPlugin()[0])
expect(renderer.getHasherPlugin()).to.be.an('object');
expect(renderer.getHasherPlugin())
.to.have.own.property('install')
.that.is.a('function');
});
Expand All @@ -41,3 +41,43 @@ describe('Test Renderer', () => {
expect(renderer.getPaths()).to.include(newPath);
});
});

describe('Test Renderer with config file', () => {
it('Should create a correct file object', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
const renderer = new Renderer(manager, [], {});
const lessOptions = renderer.getLessOptions();
expect(lessOptions).to.be.an('object');
expect(lessOptions).to.have.own.property('paths');
expect(lessOptions).to.have.own.property('plugins');
});

it('Should create a correct file object with paths attributes', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
const renderer = new Renderer(manager, [], { paths: ['tested'] });
const lessOptions = renderer.getLessOptions();
expect(lessOptions.paths)
.to.be.an('array')
.that.includes('tested');
});

it('Should create a correct file object with plugins attributes', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
const renderer = new Renderer(manager, [], { plugins: ['tested'] });
const lessOptions = renderer.getLessOptions();
expect(lessOptions.plugins)
.to.be.an('array')
.that.includes('tested');
});

it('Should throw erros with not correct file object', () => {
const manager = new FileManager('test.less', 'test.css', process.cwd());
expect(() => {
new Renderer(manager, [], { paths: 'tested' });
}).to.throw();

expect(() => {
new Renderer(manager, [], { plugins: 'tested' });
}).to.throw();
});
});
31 changes: 23 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,35 @@ const utils = require('./lib/utils-functions');
const validateArguments = require('./lib/validator/paramsValidator');

const cwd = process.cwd();
let config = {};

validateArguments(argv.src, argv.output);
if (argv.config) {
if (argv.input || argv.output) {
logger.warnInfo(
'`input` or `output` argument will be override by config file options'
);
}

config = require(argv.config);
argv.src = config.src;
argv.output = config.output;
}

validateArguments(argv.src, argv.output);
const manager = new FileManager(argv.src, argv.output, cwd);

if (!utils.existsDirectory(manager.getOutputDir())) {
utils.mkdirp(manager.getOutputDir());
}

const graph = new GraphStructure(manager);
const renderer = new Renderer(manager, graph.getFoundedPaths());
const wQueue = new WatcherQueue(graph.getFiles());
const renderer = new Renderer(
manager,
graph.getFoundedPaths(),
config.lessOptions
);

console.reset();
const wQueue = new WatcherQueue(graph.getFiles());

let isFirstBuildValid = false;
let isCompiling = false;
Expand All @@ -36,15 +51,15 @@ const watcher = chokidar.watch(wQueue.getQueue());
const compileLess = (newFile, isMainFile = false) => {
console.reset();

if (isCompiling) {
return;
}

const stream = vfs.src(newFile);

stream
.pipe(
through(function(file, enc, done) {
if (isCompiling) {
return;
}

let str = file.contents.toString();

const { removedImports, newImports } = graph.getImportStateFromFile(
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FileManager {
}

isMainFile(filePath) {
const input = this.getInputFile();
const input = path.relative(this.getCwd(), this.getInputFile());

return (
input === filePath || input === path.relative(this.getCwd(), filePath)
Expand Down
62 changes: 52 additions & 10 deletions lib/compiler/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,57 @@ const less = require('less');
const hashPlugin = require('../lessPlugin/hashPlugin');

class Renderer {
constructor(fileManager, paths) {
constructor(fileManager, paths, lessOptions = {}) {
this.paths = paths;
this.manager = fileManager;
this.lessPlugins = [
hashPlugin(this.manager.getMap(), this.manager.getCwd()),
];
this.hasherPlugin = hashPlugin(
this.manager.getMap(),
this.manager.getCwd()
);
this.lessOptions = this._parseLessOptions(lessOptions);
}

render(styles) {
return less.render(styles, {
_parseLessOptions(lessOptions) {
const defaultOptions = {
paths: this.paths,
plugins: this.lessPlugins,
});
plugins: [this.hasherPlugin],
};
const options = Object.assign(defaultOptions, lessOptions);

if (lessOptions.paths) {
if (!Array.isArray(lessOptions.paths)) {
this._throwErrorOptionType(lessOptions.paths, 'paths');
}

options.paths = this.paths.concat(options.paths);
}

if (lessOptions.plugins) {
if (!Array.isArray(lessOptions.plugins)) {
this._throwErrorOptionType(lessOptions.plugins, 'paths');
}

options.plugins = [].concat(this.hasherPlugin, options.plugins);
}

// Do not compress outputfile
delete options.compress;

return options;
}

_throwErrorOptionType(option, optionName) {
throw new Error(
'Less ' +
optionName +
' option should be an Array but a ' +
typeof option +
' given'
);
}

render(styles) {
return less.render(styles, Object.assign({}, this.lessOptions));
}

pushNewPath(path) {
Expand All @@ -27,13 +65,17 @@ class Renderer {
return this.paths;
}

getLessPlugin() {
return this.lessPlugins;
getHasherPlugin() {
return this.hasherPlugin;
}

getManager() {
return this.manager;
}

getLessOptions() {
return this.lessOptions;
}
}

module.exports = Renderer;
2 changes: 1 addition & 1 deletion lib/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ exports.logErrorBuild = error => {
};

exports.warnInfo = message => {
const _message = this.createWarn('WARN') + ' ' + chalk.grey(message);
const _message = this.createWarnTitle('WARN') + ' ' + chalk.grey(message);
log(_message);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "less-incremental-builder",
"version": "1.1.6",
"version": "1.2.0",
"description": "Build less file with a incremental builder",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit d7a4206

Please sign in to comment.