Skip to content

Commit

Permalink
feat: export version constant (#2016)
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco authored May 27, 2024
1 parent a761a01 commit 78403d3
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- run: yarn install
- run: yarn playwright install --with-deps chromium
- run: yarn test
- run: yarn test-bundles
- run: yarn test:bundles
regression:
name: Test regressions
runs-on: ubuntu-latest
Expand All @@ -65,4 +65,4 @@ jobs:
cache: yarn
- run: yarn install
- run: yarn playwright install --with-deps chromium
- run: yarn test-regression
- run: yarn test:regression
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default [
},
{
languageOptions: {
ecmaVersion: 2021,
ecmaVersion: 'latest',
globals: {
...globals.nodeBuiltin,
},
Expand Down
4 changes: 3 additions & 1 deletion lib/svgo-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import os from 'os';
import fs from 'fs';
import { pathToFileURL } from 'url';
import path from 'path';
import { optimize as optimizeAgnostic } from './svgo.js';
import { VERSION, optimize as optimizeAgnostic } from './svgo.js';

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

export { VERSION };

export const loadConfig = async (configFile, cwd = process.cwd()) => {
if (configFile != null) {
if (path.isAbsolute(configFile)) {
Expand Down
3 changes: 3 additions & 0 deletions lib/svgo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ type Output = {
data: string;
};

/** Installed version of SVGO. */
export declare const VERSION: string;

/** The core of SVGO */
export declare function optimize(input: string, config?: Config): Output;
3 changes: 3 additions & 0 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { stringifySvg } from './stringifier.js';
import { builtin } from './builtin.js';
import { invokePlugins } from './svgo/plugins.js';
import { encodeSVGDatauri } from './svgo/tools.js';
import { VERSION } from './version.js';

const pluginsMap = {};
for (const plugin of builtin) {
Expand Down Expand Up @@ -45,6 +46,8 @@ const resolvePluginConfig = (plugin) => {
return null;
};

export { VERSION };

export const optimize = (input, config) => {
if (config == null) {
config = {};
Expand Down
2 changes: 2 additions & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** Version of SVGO. */
export const VERSION = '4.0.0';
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"packageManager": "[email protected]",
"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.",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -85,15 +85,15 @@
"node": ">=14.0.0"
},
"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 .",
"fix": "eslint --fix . && prettier --write .",
"typecheck": "tsc",
"generate-bundles": "rollup -c",
"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",
"prepublishOnly": "rimraf dist && yarn generate-bundles",
"qa": "yarn lint && yarn typecheck && yarn test && yarn test-bundles && yarn test-regression"
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --maxWorkers=4 --coverage",
"test:bundles": "yarn build && node ./test/svgo.cjs && node ./test/browser.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 build"
},
"jest": {
"coveragePathIgnorePatterns": [
Expand Down
12 changes: 12 additions & 0 deletions scripts/sync-version.js
Original file line number Diff line number Diff line change
@@ -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`,
);
20 changes: 17 additions & 3 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import assert from 'assert';
import fs from 'node:fs/promises';
import http from 'http';
import path from 'path';
import { fileURLToPath } from 'url';
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">
<g attr1="val1">
<g attr2="val2">
Expand All @@ -24,11 +30,12 @@ const expected = `<svg xmlns="http://www.w3.org/2000/svg">

const content = `
<script type="module">
import { optimize } from '/svgo.browser.js';
import { VERSION, optimize } from '/svgo.browser.js';
const result = optimize(${JSON.stringify(fixture)}, {
plugins : [],
js2svg : { pretty: true, indent: 2 }
});
globalThis.version = VERSION;
globalThis.result = result.data;
</script>
`;
Expand All @@ -50,8 +57,15 @@ const runTest = async () => {
const context = await browser.newContext();
const page = await context.newPage();
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();
};

Expand Down
4 changes: 3 additions & 1 deletion test/svgo.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { loadConfig, optimize } = require('../dist/svgo-node.cjs');
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">
<g attr1="val1">
Expand Down Expand Up @@ -27,6 +28,7 @@ const runTest = () => {
});
const actual = result.data;

assert.strictEqual(VERSION, PKG.version);
assert.equal(actual, expected);
assert.notEqual(loadConfig, undefined);
};
Expand Down
12 changes: 9 additions & 3 deletions test/svgo/_index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import fs from 'node:fs/promises';
import path from 'path';
import { EOL } from 'os';
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));

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

const parseFixture = async (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*/);
};

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 () => {
const [original, expected] = await parseFixture('test.svg.txt');
const result = optimize(original, {
Expand Down

0 comments on commit 78403d3

Please sign in to comment.