forked from json-editor/json-editor
-
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.
Merge pull request json-editor#675 from json-editor/feature/merges-20…
…200227 Feature/merges 20200227
- Loading branch information
Showing
20 changed files
with
3,960 additions
and
1,620 deletions.
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/tests/codeceptjs/output | ||
.vscode | ||
.env | ||
/src/themes/*.json |
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,55 @@ | ||
const glob = require('glob') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const css2json = require('css2json') | ||
|
||
class CssToJson { | ||
constructor (params) { | ||
this.pattern = params.pattern | ||
this.jsonPattern = params.jsonPattern | ||
} | ||
|
||
apply (compiler) { | ||
compiler.hooks.entryOption.tap('CssToJson', (context, entry) => { | ||
glob(this.pattern, (err, files) => { | ||
if (err) throw err | ||
files.forEach(this.convert) | ||
}) | ||
}) | ||
} | ||
|
||
convert (file) { | ||
const target = | ||
path.join(path.dirname(path.resolve(file)), path.basename(file, '.css')) + '.json' | ||
|
||
if (fs.existsSync(target) && (fs.statSync(file).mtime < fs.statSync(target).mtime)) { | ||
return | ||
} | ||
|
||
const css = fs.readFileSync(file, { encoding: 'utf-8' }) | ||
const rules = Object.entries(css2json(css)) | ||
.map( | ||
([selector, block]) => | ||
`"${formatSelector(selector)}":"${concatBlock(block)}"` | ||
) | ||
.join(',') | ||
fs.writeFileSync(target, `{${rules}}`) | ||
} | ||
} | ||
|
||
function formatSelector (selector) { | ||
return _fixQuote(selector).replace(/\r?\n/g, ' ') | ||
} | ||
|
||
function concatBlock (value) { | ||
const block = Object.entries(value) | ||
.map(([property, value]) => `${property}:${value}`) | ||
.join(';') | ||
return _fixQuote(block) | ||
} | ||
|
||
function _fixQuote (str) { | ||
return str.replace(/"/g, "'") | ||
} | ||
|
||
module.exports = CssToJson |
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
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
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
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
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
Oops, something went wrong.