mirror of
https://github.com/HackPlan/polaris-react.git
synced 2026-04-27 00:01:24 +08:00
Previously this tried to optimize svgs within the node_modules folder too, all of which we never use. This makes the optimize build step take about 250ms instead of 4300ms - shaving about 4 seconds off our build time.
38 lines
948 B
JavaScript
38 lines
948 B
JavaScript
/* eslint-disable no-console */
|
|
|
|
const {resolve: resolvePath, basename, dirname} = require('path');
|
|
const SVGO = require('svgo');
|
|
const glob = require('glob');
|
|
const {paramCase} = require('change-case');
|
|
const {readFileSync, writeFileSync, removeSync} = require('fs-extra');
|
|
|
|
const {svgOptions} = require('@shopify/images/optimize');
|
|
|
|
const svgo = new SVGO(svgOptions());
|
|
|
|
glob(resolvePath(__dirname, '../src/**/*.svg'), (error, files) => {
|
|
if (error) {
|
|
console.error(error);
|
|
process.exit(1);
|
|
return;
|
|
}
|
|
|
|
files.forEach((file) => optimizeFile(file));
|
|
});
|
|
|
|
function optimizeFile(file) {
|
|
return new Promise((resolve) => {
|
|
const data = readFileSync(file, 'utf8');
|
|
svgo.optimize(data, (result) => {
|
|
removeSync(file);
|
|
|
|
const newFile = resolvePath(
|
|
dirname(file),
|
|
`${paramCase(basename(file, '.svg'))}.svg`,
|
|
);
|
|
writeFileSync(newFile, `${result.data}\n`);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|