-
Notifications
You must be signed in to change notification settings - Fork 18
add base theme.json to use alignment size, spacings and colors #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cedric07
wants to merge
11
commits into
master
Choose a base branch
from
feat/theme-json-base
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aa908c2
add base theme.json to use alignment sizes, palette and spacings
cedric07 4b5244f
remove unnecessary function
cedric07 38cacd8
Add colors in php instead of theme json
cedric07 4f8e741
fix list block
cedric07 0b624a1
Add theme json script
cedric07 d2202cd
Call the new theme-json script on build ans ignore generated files
cedric07 f335813
Move colors in theme json and change SCSS variables to the new name b…
cedric07 9629092
feat (theme): add colors as array in theme-json.scss : https://github…
cedric07 7f2c03c
Rename CSS spacing variables to the new SCSS variables based on the n…
cedric07 6b8cfe5
fix theme json to disable custom colors and gradients
cedric07 c8a3f22
feat (theme): improve theme-json.scss file render
n-langle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,191 @@ | ||
| const chalk = require('chalk') | ||
| const path = require('path') | ||
| const fs = require('fs') | ||
|
|
||
| const logId = '[' + chalk.blue('WebpackThemeJsonPlugin') + ']' | ||
|
|
||
| class WebpackThemeJsonPlugin { | ||
| /** | ||
| * constructor | ||
| * @param {Object} options = { | ||
| * context: string - default: '../src/theme-json' | ||
| * output: string - default: '../theme.json' | ||
| * scssOutput: string - default: '../src/scss/00-variables/_theme-json.scss' | ||
| * watch: boolean - default: false | ||
| * } | ||
| */ | ||
| constructor(options) { | ||
| // folders | ||
| this._context = options.context || path.resolve(__dirname, '../src/theme-json') + '/' | ||
| this._output = options.output || path.resolve(__dirname, '../theme.json') | ||
| this._scssOutput = options.scssOutput || path.resolve(__dirname, '../src/scss/01-abstract/_theme-json.scss') | ||
|
|
||
| if (options.watch) { | ||
| fs.watch(this._context, () => { | ||
| this.refresh() | ||
| }) | ||
| } | ||
|
|
||
| this.refresh() | ||
| } | ||
|
|
||
| /** | ||
| * apply | ||
| */ | ||
| apply() {} | ||
|
|
||
| /** | ||
| * Generate theme json file | ||
| */ | ||
| generateThemeJson() { | ||
| const jsonFiles = fs.readdirSync(this._context, { | ||
| withFileTypes: true, | ||
| }) | ||
| const themeJson = {} | ||
|
|
||
| jsonFiles.forEach((file) => { | ||
| if (file.isFile() && file.name.endsWith('.json')) { | ||
| let json = fs.readFileSync(this._context + file.name, 'utf8') | ||
|
|
||
| try { | ||
| json = JSON.parse(json) | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(logId, 'Error parsing JSON file:', file.name) | ||
| } | ||
|
|
||
| if (isPlainObject(json)) { | ||
| extend(true, themeJson, json) | ||
| } else { | ||
| // eslint-disable-next-line no-console | ||
| console.error(logId, 'JSON file is not a plain object:', file.name) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| fs.writeFileSync(this._output, JSON.stringify(themeJson, null, 2)) | ||
| // eslint-disable-next-line no-console | ||
| console.log(logId, 'JSON files successfully generated !') | ||
|
|
||
| return this | ||
| } | ||
|
|
||
| /** | ||
| * Generate scss variables file | ||
| */ | ||
| generateScssVariables() { | ||
| const comment = [ | ||
| '/**', | ||
| ' * Theme JSON', | ||
| ' * scss variables are extracted from theme.json', | ||
| ' *', | ||
| " * !!! DON'T EDIT THIS FILE !!!", | ||
| ' *', | ||
| ' */', | ||
| ] | ||
| const tasks = { | ||
| 'settings-color-palette'(key, value) { | ||
| let result = '' | ||
| const palette = [] | ||
|
|
||
| for (const color of value) { | ||
| const colorVar = getVariableName('settings-color-' + color.slug) | ||
| result += `${colorVar}: ${color.color};\n` | ||
| palette.push(`${color.slug}: ${colorVar}`) | ||
| } | ||
|
|
||
| return result + `$settings-palette: (\n\t${palette.join(',\n\t')}\n);\n` | ||
| }, | ||
| 'settings-custom': 'default', | ||
| 'settings-spacing-spacingSizes'(key, value) { | ||
| let result = '' | ||
|
|
||
| for (const spacing of value) { | ||
| result += `${getVariableName('settings-spacing-' + spacing.slug)}: ${spacing.size};\n` | ||
| } | ||
|
|
||
| return result | ||
| }, | ||
| } | ||
| // eslint-disable-next-line @wordpress/no-unused-vars-before-return | ||
| const taskNames = Object.keys(tasks) | ||
| let jsonFile = fs.readFileSync(this._output, 'utf8') | ||
|
|
||
| // check if the theme.json file is valid | ||
| try { | ||
| jsonFile = JSON.parse(jsonFile) | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(logId, 'Error parsing JSON file:', this._output) | ||
| return this | ||
| } | ||
|
|
||
| // format the scss variable name | ||
| function getVariableName(id) { | ||
| return `$${id.replace(/([A-Z])/g, '-$1').toLowerCase()}` | ||
| } | ||
|
|
||
| // traverse the theme.json file and generate the scss variables | ||
| function traverse(obj, parents = [], result = '') { | ||
| for (const key in obj) { | ||
| const id = (parents.length > 0 ? parents.join('-') + '-' : '') + key | ||
| const taskName = taskNames.filter((t) => (id.startsWith(t) ? t : null))[0] | ||
| const task = taskName ? tasks[taskName] : null | ||
|
|
||
| if (isPlainObject(obj[key])) { | ||
| result += traverse(obj[key], [...parents, key]) | ||
| } else if (task) { | ||
| if (task === 'default' && typeof obj[key] === 'string') { | ||
| result += `${getVariableName(id)}: ${obj[key]};\n` | ||
| } else if (typeof task === 'function') { | ||
| result += task(key, obj[key]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| fs.writeFileSync(this._scssOutput, comment.join('\n') + '\n' + traverse(jsonFile)) | ||
|
|
||
| return this | ||
| } | ||
|
|
||
| /** | ||
| * Refresh the theme json and scss variables files | ||
| */ | ||
| refresh() { | ||
| this.generateThemeJson() | ||
| this.generateScssVariables() | ||
| return this | ||
| } | ||
| } | ||
|
|
||
| // ---- | ||
| // utils | ||
| // ---- | ||
| function isPlainObject(o) { | ||
| return o?.constructor === Object || Object.getPrototypeOf(o ?? 0) === null | ||
| } | ||
|
|
||
| function extend() { | ||
| const args = arguments | ||
| const firstArgIsBool = typeof args[0] === 'boolean' | ||
| const deep = firstArgIsBool ? args[0] : false | ||
| const start = firstArgIsBool ? 1 : 0 | ||
| const rt = isPlainObject(args[start]) ? args[start] : {} | ||
|
|
||
| for (let i = start + 1; i < args.length; i++) { | ||
| for (const prop in args[i]) { | ||
| if (deep && isPlainObject(args[i][prop])) { | ||
| rt[prop] = extend(true, {}, rt[prop], args[i][prop]) | ||
| } else if (typeof args[i][prop] !== 'undefined') { | ||
| rt[prop] = args[i][prop] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return rt | ||
| } | ||
|
|
||
| module.exports = WebpackThemeJsonPlugin |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.