forked from go-gitea/gitea
-
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.
Direct SVG rendering (go-gitea#12157)
Introduce 'make svg' which calls a node script that compiles svg files to `public/img/svg`. These files are vendored to not create a dependency on Node for the backend build. On the frontend side, configure webpack using `raw-loader` so SVGs can be imported as string. Also moved our existing SVGs to web_src/svg for consistency. Fixes: go-gitea#11618
- Loading branch information
1 parent
6359101
commit 8188176
Showing
227 changed files
with
452 additions
and
609 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
const fastGlob = require('fast-glob'); | ||
const Svgo = require('svgo'); | ||
const {resolve, parse} = require('path'); | ||
const {readFile, writeFile, mkdir} = require('fs').promises; | ||
|
||
const glob = (pattern) => fastGlob.sync(pattern, {cwd: resolve(__dirname), absolute: true}); | ||
const outputDir = resolve(__dirname, '../public/img/svg'); | ||
|
||
function exit(err) { | ||
if (err) console.error(err); | ||
process.exit(err ? 1 : 0); | ||
} | ||
|
||
async function processFile(file, {prefix = ''} = {}) { | ||
const name = `${prefix}${parse(file).name}`; | ||
|
||
const svgo = new Svgo({ | ||
plugins: [ | ||
{removeXMLNS: true}, | ||
{removeDimensions: true}, | ||
{ | ||
addClassesToSVGElement: { | ||
classNames: [ | ||
'svg', | ||
name, | ||
], | ||
}, | ||
}, | ||
{ | ||
addAttributesToSVGElement: { | ||
attributes: [ | ||
{'width': '16'}, | ||
{'height': '16'}, | ||
{'aria-hidden': 'true'}, | ||
], | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
const {data} = await svgo.optimize(await readFile(file, 'utf8')); | ||
await writeFile(resolve(outputDir, `${name}.svg`), data); | ||
} | ||
|
||
async function main() { | ||
try { | ||
await mkdir(outputDir); | ||
} catch {} | ||
|
||
for (const file of glob('../node_modules/@primer/octicons/build/svg/*.svg')) { | ||
await processFile(file, {prefix: 'octicon-'}); | ||
} | ||
|
||
for (const file of glob('../web_src/svg/*.svg')) { | ||
await processFile(file); | ||
} | ||
} | ||
|
||
main().then(exit).catch(exit); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build bindata | ||
|
||
package svg | ||
|
||
import ( | ||
"path" | ||
"path/filepath" | ||
|
||
"code.gitea.io/gitea/modules/public" | ||
) | ||
|
||
// Discover returns a map of discovered SVG icons in bindata | ||
func Discover() map[string]string { | ||
svgs := make(map[string]string) | ||
|
||
for _, file := range public.AssetNames() { | ||
matched, _ := filepath.Match("img/svg/*.svg", file) | ||
if matched { | ||
content, err := public.Asset(file) | ||
if err == nil { | ||
filename := path.Base(file) | ||
svgs[filename[:len(filename)-4]] = string(content) | ||
} | ||
} | ||
} | ||
|
||
return svgs | ||
} |
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,31 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build !bindata | ||
|
||
package svg | ||
|
||
import ( | ||
"io/ioutil" | ||
"path" | ||
"path/filepath" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// Discover returns a map of discovered SVG icons in the file system | ||
func Discover() map[string]string { | ||
svgs := make(map[string]string) | ||
|
||
files, _ := filepath.Glob(path.Join(setting.StaticRootPath, "public", "img", "svg", "*.svg")) | ||
for _, file := range files { | ||
content, err := ioutil.ReadFile(file) | ||
if err == nil { | ||
filename := path.Base(file) | ||
svgs[filename[:len(filename)-4]] = string(content) | ||
} | ||
} | ||
|
||
return svgs | ||
} |
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,13 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package svg | ||
|
||
// SVGs contains discovered SVGs | ||
var SVGs map[string]string | ||
|
||
// Init discovers SVGs and populates the `SVGs` variable | ||
func Init() { | ||
SVGs = Discover() | ||
} |
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.