mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
feat: export version constant (#2016)
This commit is contained in:
4
.github/workflows/main.yml
vendored
4
.github/workflows/main.yml
vendored
@ -47,7 +47,7 @@ jobs:
|
|||||||
- run: yarn install
|
- run: yarn install
|
||||||
- run: yarn playwright install --with-deps chromium
|
- run: yarn playwright install --with-deps chromium
|
||||||
- run: yarn test
|
- run: yarn test
|
||||||
- run: yarn test-bundles
|
- run: yarn test:bundles
|
||||||
regression:
|
regression:
|
||||||
name: Test regressions
|
name: Test regressions
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@ -65,4 +65,4 @@ jobs:
|
|||||||
cache: yarn
|
cache: yarn
|
||||||
- run: yarn install
|
- run: yarn install
|
||||||
- run: yarn playwright install --with-deps chromium
|
- run: yarn playwright install --with-deps chromium
|
||||||
- run: yarn test-regression
|
- run: yarn test:regression
|
||||||
|
@ -20,7 +20,7 @@ export default [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
languageOptions: {
|
languageOptions: {
|
||||||
ecmaVersion: 2021,
|
ecmaVersion: 'latest',
|
||||||
globals: {
|
globals: {
|
||||||
...globals.nodeBuiltin,
|
...globals.nodeBuiltin,
|
||||||
},
|
},
|
||||||
|
@ -2,7 +2,7 @@ import os from 'os';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { pathToFileURL } from 'url';
|
import { pathToFileURL } from 'url';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { optimize as optimizeAgnostic } from './svgo.js';
|
import { VERSION, optimize as optimizeAgnostic } from './svgo.js';
|
||||||
|
|
||||||
const importConfig = async (configFile) => {
|
const importConfig = async (configFile) => {
|
||||||
// dynamic import expects file url instead of path and may fail
|
// dynamic import expects file url instead of path and may fail
|
||||||
@ -25,6 +25,8 @@ const isFile = async (file) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export { VERSION };
|
||||||
|
|
||||||
export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
||||||
if (configFile != null) {
|
if (configFile != null) {
|
||||||
if (path.isAbsolute(configFile)) {
|
if (path.isAbsolute(configFile)) {
|
||||||
|
3
lib/svgo.d.ts
vendored
3
lib/svgo.d.ts
vendored
@ -52,5 +52,8 @@ type Output = {
|
|||||||
data: string;
|
data: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Installed version of SVGO. */
|
||||||
|
export declare const VERSION: string;
|
||||||
|
|
||||||
/** The core of SVGO */
|
/** The core of SVGO */
|
||||||
export declare function optimize(input: string, config?: Config): Output;
|
export declare function optimize(input: string, config?: Config): Output;
|
||||||
|
@ -3,6 +3,7 @@ import { stringifySvg } from './stringifier.js';
|
|||||||
import { builtin } from './builtin.js';
|
import { builtin } from './builtin.js';
|
||||||
import { invokePlugins } from './svgo/plugins.js';
|
import { invokePlugins } from './svgo/plugins.js';
|
||||||
import { encodeSVGDatauri } from './svgo/tools.js';
|
import { encodeSVGDatauri } from './svgo/tools.js';
|
||||||
|
import { VERSION } from './version.js';
|
||||||
|
|
||||||
const pluginsMap = {};
|
const pluginsMap = {};
|
||||||
for (const plugin of builtin) {
|
for (const plugin of builtin) {
|
||||||
@ -45,6 +46,8 @@ const resolvePluginConfig = (plugin) => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export { VERSION };
|
||||||
|
|
||||||
export const optimize = (input, config) => {
|
export const optimize = (input, config) => {
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
config = {};
|
config = {};
|
||||||
|
2
lib/version.js
Normal file
2
lib/version.js
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/** Version of SVGO. */
|
||||||
|
export const VERSION = '4.0.0';
|
16
package.json
16
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"packageManager": "yarn@3.8.2",
|
"packageManager": "yarn@3.8.2",
|
||||||
"name": "svgo",
|
"name": "svgo",
|
||||||
"version": "3.3.1",
|
"version": "4.0.0",
|
||||||
"description": "SVGO is a Node.js library and command-line application for optimizing vector images.",
|
"description": "SVGO is a Node.js library and command-line application for optimizing vector images.",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
@ -85,15 +85,15 @@
|
|||||||
"node": ">=14.0.0"
|
"node": ">=14.0.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --maxWorkers=4 --coverage",
|
"build": "node scripts/sync-version.js && rollup -c",
|
||||||
|
"typecheck": "tsc",
|
||||||
"lint": "eslint . && prettier --check .",
|
"lint": "eslint . && prettier --check .",
|
||||||
"fix": "eslint --fix . && prettier --write .",
|
"fix": "eslint --fix . && prettier --write .",
|
||||||
"typecheck": "tsc",
|
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --maxWorkers=4 --coverage",
|
||||||
"generate-bundles": "rollup -c",
|
"test:bundles": "yarn build && node ./test/svgo.cjs && node ./test/browser.js",
|
||||||
"test-bundles": "yarn generate-bundles && node ./test/svgo.cjs && node ./test/browser.js",
|
"test:regression": "node ./test/regression-extract.js && cross-env NO_DIFF=1 node ./test/regression.js",
|
||||||
"test-regression": "node ./test/regression-extract.js && cross-env NO_DIFF=1 node ./test/regression.js",
|
"qa": "yarn typecheck && yarn lint && yarn test && yarn test:bundles && yarn test:regression",
|
||||||
"prepublishOnly": "rimraf dist && yarn generate-bundles",
|
"prepublishOnly": "rimraf dist && yarn build"
|
||||||
"qa": "yarn lint && yarn typecheck && yarn test && yarn test-bundles && yarn test-regression"
|
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
"coveragePathIgnorePatterns": [
|
"coveragePathIgnorePatterns": [
|
||||||
|
12
scripts/sync-version.js
Normal file
12
scripts/sync-version.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import fs from 'node:fs/promises';
|
||||||
|
import path from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
const pkgPath = path.join(__dirname, '../package.json');
|
||||||
|
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
'./lib/version.js',
|
||||||
|
`/** Version of SVGO. */\nexport const VERSION = '${version}';\n`,
|
||||||
|
);
|
@ -1,8 +1,14 @@
|
|||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
|
import path from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
import { chromium } from 'playwright';
|
import { chromium } from 'playwright';
|
||||||
|
|
||||||
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
const pkgPath = path.join(__dirname, '../package.json');
|
||||||
|
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
|
||||||
|
|
||||||
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
|
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
<g attr1="val1">
|
<g attr1="val1">
|
||||||
<g attr2="val2">
|
<g attr2="val2">
|
||||||
@ -24,11 +30,12 @@ const expected = `<svg xmlns="http://www.w3.org/2000/svg">
|
|||||||
|
|
||||||
const content = `
|
const content = `
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import { optimize } from '/svgo.browser.js';
|
import { VERSION, optimize } from '/svgo.browser.js';
|
||||||
const result = optimize(${JSON.stringify(fixture)}, {
|
const result = optimize(${JSON.stringify(fixture)}, {
|
||||||
plugins : [],
|
plugins : [],
|
||||||
js2svg : { pretty: true, indent: 2 }
|
js2svg : { pretty: true, indent: 2 }
|
||||||
});
|
});
|
||||||
|
globalThis.version = VERSION;
|
||||||
globalThis.result = result.data;
|
globalThis.result = result.data;
|
||||||
</script>
|
</script>
|
||||||
`;
|
`;
|
||||||
@ -50,8 +57,15 @@ const runTest = async () => {
|
|||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
await page.goto('http://localhost:5000');
|
await page.goto('http://localhost:5000');
|
||||||
const actual = await page.evaluate(() => globalThis.result);
|
|
||||||
assert.equal(actual, expected);
|
const actual = await page.evaluate(() => ({
|
||||||
|
version: globalThis.version,
|
||||||
|
result: globalThis.result,
|
||||||
|
}));
|
||||||
|
|
||||||
|
assert.strictEqual(actual.version, version);
|
||||||
|
assert.equal(actual.result, expected);
|
||||||
|
|
||||||
await browser.close();
|
await browser.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const { loadConfig, optimize } = require('../dist/svgo-node.cjs');
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const { VERSION, optimize, loadConfig } = require('../dist/svgo-node.cjs');
|
||||||
|
const PKG = require('../package.json');
|
||||||
|
|
||||||
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
|
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
<g attr1="val1">
|
<g attr1="val1">
|
||||||
@ -27,6 +28,7 @@ const runTest = () => {
|
|||||||
});
|
});
|
||||||
const actual = result.data;
|
const actual = result.data;
|
||||||
|
|
||||||
|
assert.strictEqual(VERSION, PKG.version);
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
assert.notEqual(loadConfig, undefined);
|
assert.notEqual(loadConfig, undefined);
|
||||||
};
|
};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import fs from 'fs';
|
import fs from 'node:fs/promises';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { EOL } from 'os';
|
import { EOL } from 'os';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { optimize } from '../../lib/svgo.js';
|
import { VERSION, optimize } from '../../lib/svgo.js';
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
@ -14,11 +14,17 @@ const normalize = (file) => {
|
|||||||
|
|
||||||
const parseFixture = async (file) => {
|
const parseFixture = async (file) => {
|
||||||
const filepath = path.resolve(__dirname, file);
|
const filepath = path.resolve(__dirname, file);
|
||||||
const content = await fs.promises.readFile(filepath, 'utf-8');
|
const content = await fs.readFile(filepath, 'utf-8');
|
||||||
return normalize(content).split(/\s*@@@\s*/);
|
return normalize(content).split(/\s*@@@\s*/);
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('svgo', () => {
|
describe('svgo', () => {
|
||||||
|
it('version should match package.json', async () => {
|
||||||
|
const pkgPath = path.resolve(__dirname, '../../package.json');
|
||||||
|
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
|
||||||
|
expect(VERSION).toStrictEqual(version);
|
||||||
|
});
|
||||||
|
|
||||||
it('should create indent with 2 spaces', async () => {
|
it('should create indent with 2 spaces', async () => {
|
||||||
const [original, expected] = await parseFixture('test.svg.txt');
|
const [original, expected] = await parseFixture('test.svg.txt');
|
||||||
const result = optimize(original, {
|
const result = optimize(original, {
|
||||||
|
Reference in New Issue
Block a user