diff options
author | silverwind <me@silverwind.io> | 2022-06-06 05:27:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-06 11:27:25 +0800 |
commit | df0fb17d041b34854c5a0bdd4a14ad50807ded4a (patch) | |
tree | b42e06afa668ba74dd6d79467c053c537e02f1f8 /build | |
parent | 0a8c0306004382263ba86610ba4111c569b99f14 (diff) | |
download | gitea-df0fb17d041b34854c5a0bdd4a14ad50807ded4a.tar.gz gitea-df0fb17d041b34854c5a0bdd4a14ad50807ded4a.zip |
Modernize JS build scripts (#19824)
- Remove __dirname, use file URLs instead
- Upgrade fabric dependency
- Use fs/promises syntax, this breaks node 12 but we require 14 already
The change in public/img/favicon.svg is not caused by the fabric
upgrade, but it seems it was not properly generated when introduced.
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'build')
-rwxr-xr-x | build/generate-images.js | 35 | ||||
-rwxr-xr-x | build/generate-svg.js | 26 |
2 files changed, 28 insertions, 33 deletions
diff --git a/build/generate-images.js b/build/generate-images.js index 0a91d896a8..62ce5244f0 100755 --- a/build/generate-images.js +++ b/build/generate-images.js @@ -1,14 +1,8 @@ +#!/usr/bin/env node import imageminZopfli from 'imagemin-zopfli'; import {optimize} from 'svgo'; import {fabric} from 'fabric'; -import fs from 'fs'; -import {resolve, dirname} from 'path'; -import {fileURLToPath} from 'url'; - -const {readFile, writeFile} = fs.promises; -const __dirname = dirname(fileURLToPath(import.meta.url)); -const logoFile = resolve(__dirname, '../assets/logo.svg'); -const faviconFile = resolve(__dirname, '../assets/favicon.svg'); +import {readFile, writeFile} from 'fs/promises'; function exit(err) { if (err) console.error(err); @@ -23,8 +17,10 @@ function loadSvg(svg) { }); } -async function generate(svg, outputFile, {size, bg}) { - if (outputFile.endsWith('.svg')) { +async function generate(svg, path, {size, bg}) { + const outputFile = new URL(path, import.meta.url); + + if (String(outputFile).endsWith('.svg')) { const {data} = optimize(svg, { plugins: [ 'preset-default', @@ -69,19 +65,18 @@ async function generate(svg, outputFile, {size, bg}) { async function main() { const gitea = process.argv.slice(2).includes('gitea'); - const logoSvg = await readFile(logoFile, 'utf8'); - const faviconSvg = await readFile(faviconFile, 'utf8'); + const logoSvg = await readFile(new URL('../assets/logo.svg', import.meta.url), 'utf8'); + const faviconSvg = await readFile(new URL('../assets/favicon.svg', import.meta.url), 'utf8'); await Promise.all([ - generate(logoSvg, resolve(__dirname, '../public/img/logo.svg'), {size: 32}), - generate(logoSvg, resolve(__dirname, '../public/img/logo.png'), {size: 512}), - generate(faviconSvg, resolve(__dirname, '../public/img/favicon.svg'), {size: 32}), - generate(faviconSvg, resolve(__dirname, '../public/img/favicon.png'), {size: 180}), - generate(logoSvg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200}), - generate(logoSvg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true}), - gitea && generate(logoSvg, resolve(__dirname, '../public/img/gitea.svg'), {size: 32}), + generate(logoSvg, '../public/img/logo.svg', {size: 32}), + generate(logoSvg, '../public/img/logo.png', {size: 512}), + generate(faviconSvg, '../public/img/favicon.svg', {size: 32}), + generate(faviconSvg, '../public/img/favicon.png', {size: 180}), + generate(logoSvg, '../public/img/avatar_default.png', {size: 200}), + generate(logoSvg, '../public/img/apple-touch-icon.png', {size: 180, bg: true}), + gitea && generate(logoSvg, '../public/img/gitea.svg', {size: 32}), ]); } main().then(exit).catch(exit); - diff --git a/build/generate-svg.js b/build/generate-svg.js index 29b7d47693..c4f3d5a7f9 100755 --- a/build/generate-svg.js +++ b/build/generate-svg.js @@ -1,13 +1,14 @@ +#!/usr/bin/env node import fastGlob from 'fast-glob'; import {optimize} from 'svgo'; -import {resolve, parse, dirname} from 'path'; -import fs from 'fs'; +import {parse} from 'path'; +import {readFile, writeFile, mkdir} from 'fs/promises'; import {fileURLToPath} from 'url'; -const {readFile, writeFile, mkdir} = fs.promises; -const __dirname = dirname(fileURLToPath(import.meta.url)); -const glob = (pattern) => fastGlob.sync(pattern, {cwd: resolve(__dirname), absolute: true}); -const outputDir = resolve(__dirname, '../public/img/svg'); +const glob = (pattern) => fastGlob.sync(pattern, { + cwd: fileURLToPath(new URL('..', import.meta.url)), + absolute: true, +}); function exit(err) { if (err) console.error(err); @@ -16,7 +17,6 @@ function exit(err) { async function processFile(file, {prefix, fullName} = {}) { let name; - if (fullName) { name = fullName; } else { @@ -35,7 +35,8 @@ async function processFile(file, {prefix, fullName} = {}) { {name: 'addAttributesToSVGElement', params: {attributes: [{'width': '16'}, {'height': '16'}, {'aria-hidden': 'true'}]}}, ], }); - await writeFile(resolve(outputDir, `${name}.svg`), data); + + await writeFile(fileURLToPath(new URL(`../public/img/svg/${name}.svg`, import.meta.url)), data); } function processFiles(pattern, opts) { @@ -44,15 +45,14 @@ function processFiles(pattern, opts) { async function main() { try { - await mkdir(outputDir); + await mkdir(fileURLToPath(new URL('../public/img/svg', import.meta.url)), {recursive: true}); } catch {} await Promise.all([ - ...processFiles('../node_modules/@primer/octicons/build/svg/*-16.svg', {prefix: 'octicon'}), - ...processFiles('../web_src/svg/*.svg'), - ...processFiles('../public/img/gitea.svg', {fullName: 'gitea-gitea'}), + ...processFiles('node_modules/@primer/octicons/build/svg/*-16.svg', {prefix: 'octicon'}), + ...processFiles('web_src/svg/*.svg'), + ...processFiles('public/img/gitea.svg', {fullName: 'gitea-gitea'}), ]); } main().then(exit).catch(exit); - |