Skip to content

Commit 6c8d957

Browse files
committed
feat: export version constant
1 parent 4daaa42 commit 6c8d957

File tree

10 files changed

+61
-17
lines changed

10 files changed

+61
-17
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default [
2020
},
2121
{
2222
languageOptions: {
23-
ecmaVersion: 2021,
23+
ecmaVersion: 'latest',
2424
globals: {
2525
...globals.nodeBuiltin,
2626
},

lib/svgo-node.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import os from 'os';
22
import fs from 'fs';
33
import { pathToFileURL } from 'url';
44
import path from 'path';
5-
import { optimize as optimizeAgnostic } from './svgo.js';
5+
import { VERSION, optimize as optimizeAgnostic } from './svgo.js';
66

77
const importConfig = async (configFile) => {
88
// dynamic import expects file url instead of path and may fail
@@ -25,6 +25,8 @@ const isFile = async (file) => {
2525
}
2626
};
2727

28+
export { VERSION };
29+
2830
export const loadConfig = async (configFile, cwd = process.cwd()) => {
2931
if (configFile != null) {
3032
if (path.isAbsolute(configFile)) {

lib/svgo.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,8 @@ type Output = {
5252
data: string;
5353
};
5454

55+
/** Installed version of SVGO. */
56+
export declare const VERSION: string;
57+
5558
/** The core of SVGO */
5659
export declare function optimize(input: string, config?: Config): Output;

lib/svgo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { stringifySvg } from './stringifier.js';
33
import { builtin } from './builtin.js';
44
import { invokePlugins } from './svgo/plugins.js';
55
import { encodeSVGDatauri } from './svgo/tools.js';
6+
import { VERSION } from './version.js';
67

78
const pluginsMap = {};
89
for (const plugin of builtin) {
@@ -45,6 +46,8 @@ const resolvePluginConfig = (plugin) => {
4546
return null;
4647
};
4748

49+
export { VERSION };
50+
4851
export const optimize = (input, config) => {
4952
if (config == null) {
5053
config = {};

lib/version.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** Version of SVGO. */
2+
export const VERSION = '4.0.0';

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"packageManager": "[email protected]",
33
"name": "svgo",
4-
"version": "3.3.1",
4+
"version": "4.0.0",
55
"description": "SVGO is a Node.js library and command-line application for optimizing vector images.",
66
"license": "MIT",
77
"type": "module",
@@ -85,15 +85,15 @@
8585
"node": ">=14.0.0"
8686
},
8787
"scripts": {
88-
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --maxWorkers=4 --coverage",
88+
"build": "node scripts/sync-version.js && rollup -c",
89+
"typecheck": "tsc",
8990
"lint": "eslint . && prettier --check .",
9091
"fix": "eslint --fix . && prettier --write .",
91-
"typecheck": "tsc",
92-
"generate-bundles": "rollup -c",
93-
"test-bundles": "yarn generate-bundles && node ./test/svgo.cjs && node ./test/browser.js",
94-
"test-regression": "node ./test/regression-extract.js && cross-env NO_DIFF=1 node ./test/regression.js",
95-
"prepublishOnly": "rimraf dist && yarn generate-bundles",
96-
"qa": "yarn lint && yarn typecheck && yarn test && yarn test-bundles && yarn test-regression"
92+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --maxWorkers=4 --coverage",
93+
"test:bundles": "yarn build && node ./test/svgo.cjs && node ./test/browser.js",
94+
"test:regression": "node ./test/regression-extract.js && cross-env NO_DIFF=1 node ./test/regression.js",
95+
"qa": "yarn typecheck && yarn lint && yarn test && yarn test:bundles && yarn test:regression",
96+
"prepublishOnly": "rimraf dist && yarn build"
9797
},
9898
"jest": {
9999
"coveragePathIgnorePatterns": [

scripts/sync-version.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
6+
const pkgPath = path.join(__dirname, '../package.json');
7+
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
8+
9+
await fs.writeFile(
10+
'./lib/version.js',
11+
`/** Version of SVGO. */\nexport const VERSION = '${version}';\n`,
12+
);

test/browser.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import assert from 'assert';
22
import fs from 'node:fs/promises';
33
import http from 'http';
4+
import path from 'path';
5+
import { fileURLToPath } from 'url';
46
import { chromium } from 'playwright';
57

8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9+
const pkgPath = path.join(__dirname, '../package.json');
10+
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
11+
612
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
713
<g attr1="val1">
814
<g attr2="val2">
@@ -24,11 +30,12 @@ const expected = `<svg xmlns="http://www.w3.org/2000/svg">
2430

2531
const content = `
2632
<script type="module">
27-
import { optimize } from '/svgo.browser.js';
33+
import { VERSION, optimize } from '/svgo.browser.js';
2834
const result = optimize(${JSON.stringify(fixture)}, {
2935
plugins : [],
3036
js2svg : { pretty: true, indent: 2 }
3137
});
38+
globalThis.version = VERSION;
3239
globalThis.result = result.data;
3340
</script>
3441
`;
@@ -50,8 +57,15 @@ const runTest = async () => {
5057
const context = await browser.newContext();
5158
const page = await context.newPage();
5259
await page.goto('http://localhost:5000');
53-
const actual = await page.evaluate(() => globalThis.result);
54-
assert.equal(actual, expected);
60+
61+
const actual = await page.evaluate(() => ({
62+
version: globalThis.version,
63+
result: globalThis.result,
64+
}));
65+
66+
assert.strictEqual(actual.version, version);
67+
assert.equal(actual.result, expected);
68+
5569
await browser.close();
5670
};
5771

test/svgo.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { loadConfig, optimize } = require('../dist/svgo-node.cjs');
21
const assert = require('assert');
2+
const { VERSION, optimize, loadConfig } = require('../dist/svgo-node.cjs');
3+
const PKG = require('../package.json');
34

45
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
56
<g attr1="val1">
@@ -27,6 +28,7 @@ const runTest = () => {
2728
});
2829
const actual = result.data;
2930

31+
assert.strictEqual(VERSION, PKG.version);
3032
assert.equal(actual, expected);
3133
assert.notEqual(loadConfig, undefined);
3234
};

test/svgo/_index.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import fs from 'fs';
1+
import fs from 'node:fs/promises';
22
import path from 'path';
33
import { EOL } from 'os';
44
import { fileURLToPath } from 'url';
5-
import { optimize } from '../../lib/svgo.js';
5+
import { VERSION, optimize } from '../../lib/svgo.js';
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
88

@@ -14,11 +14,17 @@ const normalize = (file) => {
1414

1515
const parseFixture = async (file) => {
1616
const filepath = path.resolve(__dirname, file);
17-
const content = await fs.promises.readFile(filepath, 'utf-8');
17+
const content = await fs.readFile(filepath, 'utf-8');
1818
return normalize(content).split(/\s*@@@\s*/);
1919
};
2020

2121
describe('svgo', () => {
22+
it('version should match package.json', async () => {
23+
const pkgPath = path.resolve(__dirname, '../../package.json');
24+
const { version } = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
25+
expect(VERSION).toStrictEqual(version);
26+
});
27+
2228
it('should create indent with 2 spaces', async () => {
2329
const [original, expected] = await parseFixture('test.svg.txt');
2430
const result = optimize(original, {

0 commit comments

Comments
 (0)