This repository was archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
[WIP] pancake-svg package enhancement
#103
Open
mikey0000
wants to merge
4
commits into
govau:master
Choose a base branch
from
mikey0000:feature/pancake-svg-enhancement
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
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| { | ||
| "presets": ["@babel/preset-env"], | ||
| "presets": [ | ||
| [ | ||
| "@babel/preset-env", { | ||
| "targets": { | ||
| "node": "current" | ||
| } | ||
| } | ||
| ] | ||
| ] | ||
| } |
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,123 @@ | ||
| /*************************************************************************************************************************************************************** | ||
| * | ||
| * Generate and compile SVG's | ||
| * | ||
| * @repo - https://github.com/govau/pancake | ||
| * @author - Michael Arthur | ||
| * @license - https://raw.githubusercontent.com/govau/pancake/master/LICENSE (MIT) | ||
| * | ||
| **************************************************************************************************************************************************************/ | ||
|
|
||
|
|
||
| //-------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
| // Dependencies | ||
| //-------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
| import Path from 'path'; | ||
| import fs from 'fs'; | ||
| import SVGSpriter from 'svg-sprite'; | ||
| import { from as svgToImgFrom } from 'svg-to-img'; | ||
| import {promisify} from 'util'; | ||
| const readdir = promisify(fs.readdir); | ||
|
|
||
| //-------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
| // Included modules | ||
| //-------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
| import { Log, ReadFile, WriteFile, CreateDir } from '@gov.au/pancake'; | ||
|
|
||
|
|
||
| export const SvgToPng = async (svgs, location) => { | ||
| await CreateDir(location); | ||
|
|
||
| svgs.map(async svg => { | ||
| const png = await ReadFile( svg, { encoding: 'utf-8' } ); | ||
| svgToImgFrom(png).to({ | ||
| type: 'png', | ||
| path: Path.join(location, Path.basename(svg, '.svg') + '.png') | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| export const CompileAllSvgs = ( compiledAll, SettingsSVG, pkgPath ) => { | ||
| return new Promise( ( resolve, reject ) => { | ||
| Promise.all( compiledAll ) | ||
| .catch( error => { | ||
| Log.error(`SVG: Compiling SVG ran into an error: ${ error }`); | ||
| }) | ||
| .then(async ( svgModulePath ) => { | ||
|
|
||
| //get all svgs | ||
| const svglist = await readdir( `${svgModulePath}` ); //read all files in the svg folder | ||
| const svgs = svglist.filter( name => !name.startsWith('.') ) //let’s hide hidden files | ||
| .map( name => Path.normalize(`${ svgModulePath }/${ name }`) ); //making them absolute paths | ||
|
|
||
| //convert svg to png | ||
| if( SettingsSVG.pngs !== false ) { | ||
| Log.verbose(`SVG: Converting svgs to PNGs`); | ||
|
|
||
| const location = Path.normalize(`${ pkgPath }/${ SettingsSVG.pngs }/`); | ||
|
|
||
| SvgToPng( svgs, location ); | ||
|
|
||
| } | ||
|
|
||
| //create svg sprite | ||
| if( SettingsSVG.name !== false ) { | ||
|
|
||
| const location = Path.normalize(`${ pkgPath }/${ SettingsSVG.location }/`); | ||
|
|
||
| const spriter = new SVGSpriter({ | ||
| dest: location, | ||
| log: null, // Logging verbosity (default: no logging), | ||
| shape: { // SVG shape related options | ||
| id: { // SVG shape ID related options | ||
| separator: '--', // Separator for directory name traversal | ||
| generator: (name) => (name.split('.')[0]), // SVG shape ID generator callback | ||
| pseudo: '~' // File name separator for shape states (e.g. ':hover') | ||
| }, | ||
| transform: ['svgo'], // List of transformations / optimizations | ||
| meta: null, // Path to YAML file with meta / accessibility data | ||
| align: null, // Path to YAML file with extended alignment data | ||
| dest: 'opt' // Output directory for optimized intermediate SVG shapes | ||
| }, | ||
| svg: { // General options for created SVG files | ||
| xmlDeclaration: true, // Add XML declaration to SVG sprite | ||
| doctypeDeclaration: true, // Add DOCTYPE declaration to SVG sprite | ||
| namespaceIDs: true, // Add namespace token to all IDs in SVG shapes | ||
| namespaceClassnames: true, // Add namespace token to all CSS class names in SVG shapes | ||
| dimensionAttributes: true // Width and height attributes on the sprite | ||
| }, | ||
| variables: {}, // Custom Mustache templating variables and functions | ||
| mode: { | ||
| symbol: { | ||
| sprite: SettingsSVG.name, | ||
| }, | ||
| shapes: false | ||
| } | ||
| }); | ||
| Log.verbose(`SVG: Adding sprites to the sprite machine`); | ||
| svgs.map(svg => { | ||
| spriter.add( svg, null, fs.readFileSync( svg, { encoding: 'utf-8' } ) ); | ||
| }); | ||
|
|
||
| Log.verbose(`SVG: Compile sprites into spritesheet`); | ||
| spriter.compile( async ( error, result ) => { | ||
| if( error ) { | ||
| Log.error( error ); | ||
| return reject( error ); | ||
| } | ||
|
|
||
| for (let mode in result) { | ||
| for (let resource in result[mode]) { | ||
| await WriteFile(result[mode][resource].path, result[mode][resource].contents); | ||
| } | ||
| } | ||
|
|
||
| resolve( true ); | ||
|
|
||
|
|
||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this is available since node v8