Skip to content

Commit

Permalink
Moved jest-diff to devDependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Yzen90 committed Feb 26, 2023
1 parent 3d2902e commit 0a8a47e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xrnoz/vuetify-svg-icons",
"version": "2.0.0-2",
"version": "2.0.0",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -21,7 +21,6 @@
"dependencies": {
"acorn": "8.8.2",
"acorn-walk": "^8.2.0",
"jest-diff": "29.4.3",
"picocolors": "^1.0.0"
},
"peerDependencies": {
Expand All @@ -34,6 +33,7 @@
"@tsconfig/node18-strictest-esm": "1.0.1",
"@types/estree": "1.0.0",
"@types/node": "18.14.0",
"jest-diff": "29.4.3",
"prettier": "2.8.4",
"rimraf": "4.1.2",
"ts-node": "10.9.1",
Expand Down
7 changes: 2 additions & 5 deletions plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse } from 'path';
import { createFilter, PluginOption, FilterPattern } from 'vite';
import c from 'picocolors';

import { stripImports, useReplacer, difference } from './utils.js';
import { stripImports, useReplacer } from './utils.js';

export interface ExtractionOptions {
/** Package that provides the icons that will be embeded. */
Expand Down Expand Up @@ -155,13 +155,12 @@ export default (options: IconEmbedOptions) => {
transformed = transformed.replace(matcher, useReplacer(showReplacements, extractorFn, icons, matches));

if (removeImports) {
const original = showReplacements ? transformed : '';

const iconsPkgRemoved = removed.includes(iconsPkg);
const extractorPkgRemoved = removed.includes(extractorPkg);

transformed = stripImports(
transformed,
showReplacements,
iconsPkgRemoved ? undefined : iconsPkg,
extractorPkgRemoved ? undefined : extractorPkg,
);
Expand All @@ -171,8 +170,6 @@ export default (options: IconEmbedOptions) => {

transformed = transformed.replace(RegExp(`${extractorPkg},?`, 'm'), '');
if (matches) for (const match of matches) transformed = transformed.replace(RegExp(`${match},?`, 'm'), '');

if (showReplacements && original !== transformed) console.info(difference(original, transformed));
}
}

Expand Down
29 changes: 22 additions & 7 deletions scripts/parse-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { stripImports, difference } from '../utils.js';
import { diff } from 'jest-diff';
import c from 'picocolors';

import { stripImports } from '../utils.js';

const difference = (original: string, transformed: string) =>
diff(original, transformed, {
omitAnnotationLines: true,
aColor: c.yellow,
aIndicator: ' -',
bColor: c.yellow,
bIndicator: ' +',
commonIndicator: ' ',
contextLines: 2,
expand: false,
})?.replace(/^[^@]*@@[^,]+,[^,]+,[^,]+@@[^(?:\r?\n|\r)]*$(?:\r?\n|\r)?/gm, '');

console.log('=============== Sampe 1 ===============');

Expand All @@ -17,7 +32,7 @@ export default {};
`;

console.log(
difference(sample1, stripImports(sample1, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
difference(sample1, stripImports(sample1, false, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
);

console.log('=============== Sampe 2 ===============');
Expand All @@ -32,7 +47,7 @@ export default {};
`;

console.log(
difference(sample2, stripImports(sample2, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
difference(sample2, stripImports(sample2, false, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
);

console.log('=============== Sampe 3 ===============');
Expand All @@ -52,7 +67,7 @@ export default {};
`;

console.log(
difference(sample3, stripImports(sample3, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
difference(sample3, stripImports(sample3, false, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
);

console.log('=============== Sampe 4 ===============');
Expand All @@ -72,7 +87,7 @@ export default {};
`;

console.log(
difference(sample4, stripImports(sample4, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
difference(sample4, stripImports(sample4, false, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
);

console.log('=============== Sampe 5 ===============');
Expand All @@ -93,7 +108,7 @@ export default {};
`;

console.log(
difference(sample5, stripImports(sample5, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
difference(sample5, stripImports(sample5, false, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
);

console.log('=============== Sampe 6 ===============');
Expand All @@ -107,5 +122,5 @@ export default {};
`;

console.log(
difference(sample6, stripImports(sample6, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
difference(sample6, stripImports(sample6, false, '@fortawesome/free-solid-svg-icons', '@xrnoz/vuetify-svg-icons')),
);
22 changes: 7 additions & 15 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parse, Node } from 'acorn';
import { ancestor, simple } from 'acorn-walk';
import { diff } from 'jest-diff';
import c from 'picocolors';

import type { ImportDeclaration /* , ImportExpression */ } from 'estree';
Expand All @@ -17,7 +16,7 @@ const getLocation = (ancestors: Node[]): Location | null => {
return null;
};

export const stripImports = (code: string, iconPackage?: string, extractorPkg?: string) => {
export const stripImports = (code: string, showReplacements: boolean, iconPackage?: string, extractorPkg?: string) => {
const ast = parse(code, { ecmaVersion: 'latest', sourceType: 'module' });

const imports: Location[] = [];
Expand Down Expand Up @@ -67,25 +66,18 @@ export const stripImports = (code: string, iconPackage?: string, extractorPkg?:
}
stripped = stripped + code.slice(next);

if (showReplacements) {
if (packageFound && extractorPkg) console.info(` ${c.yellow(`- Removed import of \`${extractorPkg}\`.`)}`);
if (iconPackageFound && iconPackage) console.info(` ${c.yellow(`- Removed import of \`${iconPackage}\`.`)}`);
}

return stripped;
}

return code;
};

export const difference = (original: string, transformed: string) =>
diff(original, transformed, {
omitAnnotationLines: true,
aColor: c.yellow,
aIndicator: ' -',
bColor: c.yellow,
bIndicator: ' +',
commonIndicator: ' ',
contextLines: 0,
expand: false,
})?.replace(/^[^@]*@@[^,]+,[^,]+,[^,]+@@[^(?:\r?\n|\r)]*$(?:\r?\n|\r)?/gm, '');

const truncate = (text: string) => (text.length > 50 ? text.slice(0, 49) + '…' : text);
export const truncate = (text: string) => (text.length > 50 ? text.slice(0, 49) + '…' : text);

export const useReplacer = (
showReplacements: boolean,
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@
"@jridgewell/sourcemap-codec" "^1.4.10"

"@sinclair/typebox@^0.25.16":
version "0.25.21"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.25.21.tgz#763b05a4b472c93a8db29b2c3e359d55b29ce272"
integrity sha512-gFukHN4t8K4+wVC+ECqeqwzBDeFeTzBXroBTqE6vcWrQGbEUpHO7LYdG0f4xnvYq4VOEwITSlHlp0JBAIFMS/g==
version "0.25.24"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.25.24.tgz#8c7688559979f7079aacaf31aa881c3aa410b718"
integrity sha512-XJfwUVUKDHF5ugKwIcxEgc9k8b7HbznCp6eUfWgu710hMPNIO4aw4/zB5RogDQz8nd6gyCDpU9O/m6qYEWY6yQ==

"@tsconfig/node10@^1.0.7":
version "1.0.9"
Expand Down

0 comments on commit 0a8a47e

Please sign in to comment.