Skip to content

Commit

Permalink
upgrade packages and cleanup node 16
Browse files Browse the repository at this point in the history
  • Loading branch information
atlowChemi committed Feb 26, 2024
1 parent fe61e2f commit f00393d
Show file tree
Hide file tree
Showing 12 changed files with 2,379 additions and 3,359 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
node_modules
test/fixtures/*
test/fixtures/*
coverage/*
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint -- --max-warnings=0 \
&& npx prettier --check . \
&& npm run type-check \
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
dist-*
webfont
node_modules
node_modules
coverage/*
1 change: 0 additions & 1 deletion example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { iconBaseSelector, iconClassPrefix, icons } from './webfont/icons';

const baseSelector = iconBaseSelector.replace('.', '');

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
<div>
<h1>vite-svg-2-webfont</h1>
Expand Down
5,643 changes: 2,337 additions & 3,306 deletions package-lock.json

Large diffs are not rendered by default.

31 changes: 14 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"type-check": "tsc --noEmit",
"test": "vitest",
"coverage": "vitest run --coverage",
"prepare": "husky install"
"prepare": "husky"
},
"repository": {
"type": "git",
Expand All @@ -50,26 +50,23 @@
"vite": "^3.0.0 || ^4.0.0 || ^5.0.0"
},
"devDependencies": {
"@types/glob": "^8.0.0",
"@types/node": "^18.11.18",
"@types/node-fetch": "^2.6.2",
"@types/vusion__webfonts-generator": "^0.8.1",
"@typescript-eslint/eslint-plugin": "^5.47.1",
"@typescript-eslint/parser": "^5.47.1",
"@vitest/coverage-istanbul": "^1.2.2",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.5.0",
"husky": "^8.0.0",
"node-fetch": "^3.3.0",
"@types/node": "^20.11.20",
"@types/vusion__webfonts-generator": "^0.8.6",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"@vitest/coverage-istanbul": "^1.3.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"husky": "^9.0.11",
"prettier": "^2.8.1",
"tsup": "^6.5.0",
"typescript": "^4.9.4",
"vite": "^5.0.12",
"vitest": "^1.2.2"
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"vite": "^5.1.4",
"vitest": "^1.3.1"
},
"dependencies": {
"@vusion/webfonts-generator": "^0.8.0",
"glob": "^8.1.0"
"glob": "^10.3.10"
},
"overrides": {
"make-fetch-happen": "^13.0.0"
Expand Down
10 changes: 0 additions & 10 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { fileURLToPath } from 'url';
import { readFile } from 'fs/promises';
import nodeFetch from 'node-fetch';
import { describe, it, beforeAll, afterAll, expect } from 'vitest';
import { build, createServer, preview, normalizePath } from 'vite';
import type { RollupOutput } from 'rollup';
import type { PreviewServer, ViteDevServer, InlineConfig } from 'vite';

// Currently @types/node doesn't include the fetch typing yet.
declare global {
// eslint-disable-next-line no-var
var fetch: typeof import('node-fetch').default;
}
global.fetch ||= nodeFetch;

// #region test utils
const root = new URL('./fixtures/', import.meta.url);
const types = ['svg', 'eot', 'woff', 'woff2', 'ttf'];
Expand Down Expand Up @@ -111,15 +103,13 @@ describe('build', () => {
});

it.concurrent('injects fonts css to page', async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const cssFileName = output.find(({ type, name }) => type === 'asset' && name === 'index.css')!.fileName;
const res = await fetchTextContent(server, `/${cssFileName}`);
expect(res).toMatch(/^@font-face{font-family:iconfont;/);
});

types.forEach(type => {
it.concurrent(`has font of type ${type} available`, async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const iconAssetName = output.find(({ fileName }) => fileName.startsWith('assets/iconfont-') && fileName.endsWith(type))!.fileName;
const [expected, res] = await Promise.all([loadFileContent(`fonts/iconfont.${type}`, 'buffer'), fetchBufferContent(server, `/${iconAssetName}`)]);
expect(res).toStrictEqual(expected);
Expand Down
33 changes: 19 additions & 14 deletions src/optionParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import glob from 'glob';
import * as optionParser from './optionParser';
import { describe, it, expect, vi, afterEach, Mock } from 'vitest';
import { globSync } from 'glob';
import { describe, it, expect, vi, afterEach, beforeAll } from 'vitest';
import { NoIconsAvailableError, InvalidWriteFilesTypeError } from './errors';
import type { GeneratedFontTypes } from '@vusion/webfonts-generator';

vi.mock('glob', () => ({ default: { sync: vi.fn() } }));
vi.mock('glob', () => ({ globSync: vi.fn() }));

describe('optionParser', () => {
describe.concurrent('parseIconTypesOption', () => {
Expand All @@ -31,35 +31,37 @@ describe('optionParser', () => {
vi.restoreAllMocks();
});

const { sync } = glob as unknown as { sync: Mock };

it('defaults to all svg files in context', () => {
optionParser.parseFiles({ context: '' });
expect(sync).toHaveBeenCalledOnce();
expect(sync).toBeCalledWith('*.svg', { cwd: '' });
try {
optionParser.parseFiles({ context: '' });
} catch {
/* ignore */
}
expect(globSync).toHaveBeenCalledOnce();
expect(globSync).toBeCalledWith(['*.svg'], { cwd: '' });
});

it('concatenates the context to the file name', () => {
const file = 'ex.svg';
const context = 'prefix';
vi.mocked(sync).mockReturnValueOnce([file]);
vi.mocked(globSync).mockReturnValueOnce([file]);
const resp = optionParser.parseFiles({ context });
expect(sync).toHaveBeenCalledOnce();
expect(sync).toBeCalledWith('*.svg', { cwd: context });
expect(globSync).toHaveBeenCalledOnce();
expect(globSync).toBeCalledWith(['*.svg'], { cwd: context });
expect(resp).to.be.lengthOf(1);
expect(resp[0]).to.be.eq(`${context}/${file}`);
});

it('throws if no files found', async () => {
vi.mocked(sync).mockReturnValueOnce([]);
vi.mocked(globSync).mockReturnValueOnce([]);
try {
optionParser.parseFiles({ context: '' });
throw new Error('Should never get to this error!');
} catch (err) {
expect(err).to.be.instanceOf(NoIconsAvailableError);
}
expect(sync).toHaveBeenCalledOnce();
expect(sync).toBeCalledWith('*.svg', { cwd: '' });
expect(globSync).toHaveBeenCalledOnce();
expect(globSync).toBeCalledWith(['*.svg'], { cwd: '' });
});
});

Expand Down Expand Up @@ -230,6 +232,9 @@ describe('optionParser', () => {

describe.concurrent('parseOptions', () => {
const context = '';
beforeAll(() => {
vi.mocked(globSync).mockReturnValue(['']);
});

it.concurrent('returns order identical to types', () => {
const types: GeneratedFontTypes[] = ['ttf', 'woff', 'svg'];
Expand Down
6 changes: 2 additions & 4 deletions src/optionParser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import glob from 'glob';
import { resolve } from 'path';
import { globSync } from 'glob';
import { hasFileExtension } from './utils';
import { InvalidWriteFilesTypeError, NoIconsAvailableError } from './errors';
import type { WebfontsGeneratorOptions, GeneratedFontTypes } from '@vusion/webfonts-generator';

const { sync } = glob;

const FILE_TYPE_OPTIONS = ['html', 'css', 'fonts'] as const;
type FileType = (typeof FILE_TYPE_OPTIONS)[number];

Expand Down Expand Up @@ -152,7 +150,7 @@ export function parseIconTypesOption<T extends GeneratedFontTypes = GeneratedFon

export function parseFiles({ files, context }: Pick<IconPluginOptions, 'files' | 'context'>) {
files ||= ['*.svg'];
const resolvedFiles = files.flatMap(fileGlob => sync(fileGlob, { cwd: context })).map(file => `${context}/${file}`);
const resolvedFiles = globSync(files, { cwd: context })?.map(file => `${context}/${file}`) || [];
if (!resolvedFiles.length) {
throw new NoIconsAvailableError('The specified file globs did not resolve any files in the context.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function handleWatchEvent(
onIconAdded: (e: FileChangeInfo<string>) => void,
_doesFileExist = doesFileExist,
) {
if (eventType !== 'rename' || !filename.endsWith('.svg') || !(await _doesFileExist(folderPath, filename))) {
if (eventType !== 'rename' || !filename?.endsWith('.svg') || !(await _doesFileExist(folderPath, filename))) {
return;
}
onIconAdded({ eventType, filename });
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"importsNotUsedAsValues": "error",
"verbatimModuleSyntax": true,
"moduleResolution": "node",
"baseUrl": ".",
"esModuleInterop": true,
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default defineConfig({
test: {
coverage: {
provider: 'istanbul',
exclude: ['example/**', '.eslint*', 'src/fixtures/**'],
},
},
});

0 comments on commit f00393d

Please sign in to comment.