Skip to content

Commit

Permalink
Support abstract components (detected by special dirname prefix)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeni Dmitriev committed Oct 21, 2016
1 parent ee4fe9a commit c608ce9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions .stylcorc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"ensure_trailing_newline": true,
"file_write_options": "utf8",
"virtual_component_prefix": null,
"abstract_dir_prefix": null,
"files": {
".": {
"filename": "{{NAME}}",
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ For virtual components, [you can disable](#no_virtual) creating some of componen
If option value is `null`, no virtual component recognition is made, and every component's files are generated by the same rules.


#### abstract_dir_prefix ####
`string` or `null`, default `null`

Consider component abstract if it is created inside directory with specified prefix in the name begin.

Abstract component is _not imported in any buildfile_, and is only used for as extend base for other components.

For example, if you set this option to `"_"`, component `foo` created with directory `_common` (i.e. `_oommon/foo`,
as well as `bar/_common/foo`) is recognized as virtual.

If option value is `null`, no abstract component recognition is made, and every component is appended to buildfile
(if [correspondent option](#append_to_buildfile) is enabled).


#### files ####
`object` containing `object`s

Expand Down Expand Up @@ -294,6 +308,7 @@ Here are default configuration values for Stylco. You can override any of them w
"ensure_trailing_newline": true,
"file_write_options": "utf8",
"virtual_component_prefix": null,
"abstract_dir_prefix": null,
"files": {
".": {
"filename": "{{NAME}}",
Expand Down
24 changes: 23 additions & 1 deletion lib/stylco.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ module.exports = function (debug) {

// Append component entry to build file
// @todo recursively create buildfiles for each level?
if (_config.append_to_buildfile) {
if (_config.append_to_buildfile && !this._isAbstractComponent(destination)) {
buildfile_path = path.join(destination_dir, '..') + this._config.file_ext;
if (_config.allow_buildfile_outside_basedir || !_isPathOutsideDir(_config.basedir, buildfile_path)) {
// Check whether buildfile exists, and if it has any content
Expand Down Expand Up @@ -278,6 +278,27 @@ module.exports = function (debug) {
}
};

/**
* Determine whether component should be treated as abstract
* Looks for correspondent dirname prefix (if specified in config) on each path level, excluding last one
* @param {string} destination Component destination as specified by user (e.g. 'somedir/mycomponent')
* @returns {boolean}
* @private
*/
Stylco.prototype._isAbstractComponent = function (destination) {
var _this = this,
parts;
if (this._config.abstract_dir_prefix) {
parts = _splitPath(destination);
parts.pop(); // remove component name
return parts.some(function (item) {
return _beginsWith(_this._config.abstract_dir_prefix, item);
});
} else {
return false;
}
};

/**
* Filter files list depending on file existence and availability for current component
* @param {string|string[]} imports Input list of file IDs (or single item as string)
Expand Down Expand Up @@ -453,6 +474,7 @@ module.exports = function (debug) {
* @property {boolean} ensure_trailing_newline
* @property {FileWriteOptions} file_write_options
* @property {string|null} virtual_component_prefix
* @property {string|null} abstract_dir_prefix
* @property {object<string, StylcoConfigFileItem>} files
*/

Expand Down

0 comments on commit c608ce9

Please sign in to comment.