-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,556 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
{ | ||
"env": { | ||
"commonjs": true, | ||
"es6": true, | ||
"node": true | ||
}, | ||
"parserOptions": { | ||
"sourceType": "module", | ||
"ecmaVersion": 2018 | ||
}, | ||
"extends": "eslint:recommended", | ||
"rules": { | ||
"array-bracket-spacing": [ | ||
"error" | ||
], | ||
"arrow-body-style": [ | ||
"warn" | ||
], | ||
"arrow-spacing": [ | ||
"error" | ||
], | ||
"block-spacing": [ | ||
"error", | ||
"never" | ||
], | ||
"brace-style": [ | ||
"error", | ||
"1tbs" | ||
], | ||
"comma-spacing": [ | ||
"error" | ||
], | ||
"comma-style": [ | ||
"error" | ||
], | ||
"computed-property-spacing": [ | ||
"error" | ||
], | ||
"curly": [ | ||
"error" | ||
], | ||
"dot-location": [ | ||
"error", | ||
"property" | ||
], | ||
"dot-notation": [ | ||
"error" | ||
], | ||
"eqeqeq": [ | ||
"error" | ||
], | ||
"func-call-spacing": [ | ||
"error" | ||
], | ||
"indent": [ | ||
"error", | ||
4, | ||
{"MemberExpression": "off"} | ||
], | ||
"key-spacing": [ | ||
"error" | ||
], | ||
"keyword-spacing": [ | ||
"error" | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"new-cap": [ | ||
"warn" | ||
], | ||
"new-parens": [ | ||
"error" | ||
], | ||
"no-multi-spaces": [ | ||
"error" | ||
], | ||
"no-trailing-spaces": [ | ||
"warn" | ||
], | ||
"no-unneeded-ternary": [ | ||
"warn" | ||
], | ||
"no-useless-call": [ | ||
"error" | ||
], | ||
"no-useless-computed-key": [ | ||
"error" | ||
], | ||
"no-var": [ | ||
"error" | ||
], | ||
"no-whitespace-before-property": [ | ||
"warn" | ||
], | ||
"object-curly-spacing": [ | ||
"error" | ||
], | ||
"object-shorthand": [ | ||
"warn" | ||
], | ||
"operator-assignment": [ | ||
"warn" | ||
], | ||
"operator-linebreak": [ | ||
"error", | ||
"after" | ||
], | ||
"prefer-arrow-callback": [ | ||
"error", | ||
{"allowNamedFunctions": true} | ||
], | ||
"prefer-rest-params": [ | ||
"error" | ||
], | ||
"quotes": [ | ||
"error", | ||
"double" | ||
], | ||
"rest-spread-spacing": [ | ||
"error" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"semi-spacing": [ | ||
"error" | ||
], | ||
"semi-style": [ | ||
"error" | ||
], | ||
"space-before-blocks": [ | ||
"error" | ||
], | ||
"space-before-function-paren": [ | ||
"error", | ||
{"anonymous": "always", "named": "never", "asyncArrow": "always"} | ||
], | ||
"space-in-parens": [ | ||
"error" | ||
], | ||
"space-infix-ops": [ | ||
"error" | ||
], | ||
"space-unary-ops": [ | ||
"error" | ||
], | ||
"spaced-comment": [ | ||
"warn" | ||
], | ||
"switch-colon-spacing": [ | ||
"error" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
.eslint* | ||
rollup.config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,63 @@ | ||
# less-compile-roots | ||
Tool for compiling only root Less files | ||
|
||
It is a common practice to modularize Less files (on a component or similar base) and then combine (`@import`) them in several resulting bundles. In most cases you only want to compile those resulting bundles to get a few combined CSS files, while compiling every single component file is pointless and time-wasting. | ||
|
||
Unfortunately, the official Less compiler does not currently provide a way to perform such selective compilation automatically. The `less-compile-roots` module was created to meet this lack. It is a simple tool for extracting and compiling root Less files (i.e. the files that are not being imported by other Less files). | ||
|
||
## Installation | ||
|
||
``` | ||
npm install less-compile-roots | ||
``` | ||
|
||
:warning: Note that the `less-compile-roots` module uses [`less`](https://www.npmjs.com/package/less) as a peer dependency, and you need to have the `less` package installed explicitly in your project as well. | ||
|
||
## Usage | ||
|
||
Here is a basic usage example: | ||
|
||
```javascript | ||
let lessCompileRoots = require("less-compile-roots"); | ||
|
||
lessCompileRoots.compileRoots({ | ||
// Glob pattern matching existing less files | ||
pattern: "src/**/*.less", | ||
// Pass any Less.js options you need | ||
lessOptions: { | ||
sourceMap: {sourceMapFileInline: true} | ||
} | ||
}) | ||
.then(() => { | ||
console.log("All done"); | ||
}) | ||
.catch(reason => { | ||
console.error(reason); | ||
}); | ||
``` | ||
|
||
## API | ||
|
||
The following methods are exported by the `less-compile-roots` module: | ||
|
||
### `compileRoots(options)` | ||
|
||
The method picks out the root Less files from all files matching the provided glob pattern (`options.pattern`), and compiles them with the Less pre-processor. It returns a Promise that resolves once all these root files have been successfully compiled. | ||
|
||
The supported options are: | ||
|
||
* `pattern` _(required)_: a glob pattern (or a list of patterns) matching your source Less files. Please refer the [fast-glob docs](https://github.com/mrmlnc/fast-glob#patterns) for details. | ||
* `lessOptions` _(optional)_: the options object to pass to the [`less.render` method](http://lesscss.org/usage/#programmatic-usage). The [available options](http://lesscss.org/usage/#less-options) are listed in the official Less documentation. | ||
* `globOptions` _(optional)_: the options object to pass to the fast-glob function. See their [docs](https://github.com/mrmlnc/fast-glob#options-1) for details. | ||
|
||
### `getRoots(options)` | ||
|
||
This methods just returns a Promise that resolves with a list of the root file paths. It accepts the same options as the [`compileRoots`](#compileroots-options) method except the `lessOptions` parameter. This method may be useful if you just need to get the list of root Less files without compiling them. | ||
|
||
## Requirements | ||
|
||
* NodeJS engine v9.11.2+ | ||
* Less pre-processor v2.0.0+ | ||
|
||
## Caveats | ||
|
||
`less-compile-roots` is a *very simple* and tiny tool built to be fast. It uses a single plain regular expression to extract all imports from a Less file, which proves to be sufficient in the majority of cases. It is worth noticing however that there is no way to take account of all possible syntactic edge cases with a simple regular expression. So if your Less files contain some really “peculiar” or unusual code involving the `@import` statements, or kind of uncommonly commented out code mixing the imports with other stuff on one line, then please pay greater attention to the results you get, and in case of need just simplify those confusing parts in the problem files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
let fs = require("fs"); | ||
|
||
function readFile(path) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(path, "utf8", (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function writeFile(path, data) { | ||
return new Promise((resolve, reject) => { | ||
fs.writeFile(path, data, err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function setExt(path, ext, oldExtRE = /\.less$/) { | ||
return path.replace(oldExtRE, "") + ext; | ||
} | ||
|
||
let path = require("path"); | ||
let fastGlob = require("fast-glob"); | ||
|
||
let flat = Array.prototype.flat ? list => list.flat() : list => [].concat(...list); | ||
|
||
async function getImports(entries) { | ||
let commentRE = /\/\*[\s\S]*?\*\/|\/\/\s*@import[^;]+;/g; | ||
let importRE = /(?<=@import[^"']+["']).+?(?=['"]\s*;)/g; | ||
let promises = entries.map(entry => | ||
readFile(entry) | ||
.then(data => { | ||
data = data.replace(commentRE, ""); | ||
let dir = path.dirname(entry); | ||
return (data.match(importRE) || []).map(importPath => { | ||
importPath = path.join(dir, importPath); | ||
if (!path.extname(importPath)) { | ||
importPath += ".less"; | ||
} | ||
return importPath; | ||
}); | ||
}) | ||
); | ||
let importLists = await Promise.all(promises); | ||
return new Set(flat(importLists)); | ||
} | ||
|
||
function compile(entries, lessOptions) { | ||
let less = require("less"); | ||
let promises = entries.map(entry => | ||
readFile(entry) | ||
.then(data => less.render(data, { | ||
...lessOptions, | ||
paths: path.dirname(entry), | ||
filename: entry | ||
})) | ||
.then(({css, map}) => { | ||
let writeCSS = writeFile(setExt(entry, ".css"), css); | ||
if (!map) { | ||
return writeCSS; | ||
} | ||
let writeMap = writeFile(setExt(entry, ".map"), map); | ||
return Promise.all([writeCSS, writeMap]); | ||
}) | ||
); | ||
return Promise.all(promises); | ||
} | ||
|
||
async function getRoots({pattern, globOptions = {}}) { | ||
let entries = await fastGlob(pattern, globOptions); | ||
let importSet = await getImports(entries); | ||
return entries.filter(entry => !importSet.has(path.normalize(entry))); | ||
} | ||
|
||
async function compileRoots({pattern, lessOptions = {}, globOptions = {}}) { | ||
let rootEntries = await getRoots({pattern, globOptions}); | ||
return compile(rootEntries, lessOptions); | ||
} | ||
|
||
exports.compileRoots = compileRoots; | ||
exports.getRoots = getRoots; |
Oops, something went wrong.