Skip to content

Commit

Permalink
Merge pull request #245 from mizdra/update-deps
Browse files Browse the repository at this point in the history
[happy-css-modules] Update dependencies
  • Loading branch information
mizdra authored May 18, 2024
2 parents d22d60f + 0a95660 commit b5fb70c
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 174 deletions.
480 changes: 382 additions & 98 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions packages/happy-css-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@
"node": ">=18.0.0"
},
"dependencies": {
"@file-cache/core": "^1.1.4",
"@file-cache/npm": "^1.1.4",
"@file-cache/core": "^2.0.0",
"@file-cache/npm": "^2.0.0",
"await-lock": "^2.2.2",
"camelcase": "^7.0.1",
"chalk": "^5.0.1",
"chokidar": "^3.5.3",
"enhanced-resolve": "^5.10.0",
"glob": "^8.0.3",
"import-meta-resolve": "^2.2.2",
"minimatch": "^5.1.6",
"postcss": "^8.4.31",
"postcss-load-config": "^4.0.1",
"postcss-modules": "^4.3.1",
"postcss-selector-parser": "^6.0.13",
"camelcase": "^8.0.0",
"chalk": "^5.3.0",
"chokidar": "^3.6.0",
"enhanced-resolve": "^5.16.1",
"glob": "^10.3.15",
"import-meta-resolve": "^4.1.0",
"minimatch": "^9.0.4",
"postcss": "^8.4.38",
"postcss-load-config": "^5.1.0",
"postcss-modules": "^6.0.0",
"postcss-selector-parser": "^6.0.16",
"postcss-value-parser": "^4.2.0",
"source-map": "^0.7.4",
"yargs": "^17.5.1"
"yargs": "^17.7.2"
},
"peerDependencies": {
"less": "^3.0.0 || ^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { run } from '../runner.js';
import { createTSServer } from '../test-util/tsserver.js';
import { createFixtures, getFixturePath } from '../test-util/util.js';

const server = await createTSServer();
const server = createTSServer();

const defaultOptions: RunnerOptions = {
pattern: 'test/**/*.{css,scss}',
Expand Down
4 changes: 2 additions & 2 deletions packages/happy-css-modules/src/resolver/node-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { fileURLToPath, pathToFileURL } from 'url';
import { resolve } from 'import-meta-resolve';
import type { Resolver } from './index.js';

export const createNodeResolver: () => Resolver = () => async (specifier, options) => {
return fileURLToPath(await resolve(specifier, pathToFileURL(options.request).href));
export const createNodeResolver: () => Resolver = () => (specifier, options) => {
return fileURLToPath(resolve(specifier, pathToFileURL(options.request).href));
};
9 changes: 3 additions & 6 deletions packages/happy-css-modules/src/resolver/webpack-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ export const createWebpackResolver: (webpackResolverOptions?: WebpackResolverOpt
* @see https://github.com/webpack-contrib/css-loader/blob/897e7dd250ccdb0d31e6c66d4cd0d009f2022a85/src/plugins/postcss-import-parser.js#L228-L235
*/
const cssLoaderResolver = enhancedResolve.create.sync({
dependencyType: 'css',
conditionNames: ['style'],
// We are not sure how "..." affects behavior...
mainFields: ['css', 'style', 'main', '...'],
mainFiles: ['index', '...'],
extensions: ['.css', '...'],
preferRelative: true,
alias: webpackResolveAlias,
alias: webpackResolveAlias ?? {},
});

/**
Expand All @@ -60,14 +59,13 @@ export const createWebpackResolver: (webpackResolverOptions?: WebpackResolverOpt
* @see https://github.com/webpack-contrib/sass-loader/blob/49a578a218574ddc92a597c7e365b6c21960717e/src/utils.js#L531-L539
*/
const sassLoaderResolver = enhancedResolve.create.sync({
dependencyType: 'sass',
conditionNames: ['sass', 'style'],
mainFields: ['sass', 'style', 'main', '...'],
mainFiles: ['_index', 'index', '...'],
extensions: ['.sass', '.scss', '.css'],
restrictions: [/\.((sa|sc|c)ss)$/iu],
preferRelative: true,
alias: webpackResolveAlias,
alias: webpackResolveAlias ?? {},
modules: ['node_modules', ...(sassLoadPaths ?? [])],
});

Expand All @@ -77,13 +75,12 @@ export const createWebpackResolver: (webpackResolverOptions?: WebpackResolverOpt
* @see https://github.com/webpack-contrib/less-loader/blob/d74f740c100c4006b00dfb3e02c6d5aaf8713519/src/utils.js#L35-L42
*/
const lessLoaderResolver = enhancedResolve.create.sync({
dependencyType: 'less',
conditionNames: ['less', 'style'],
mainFields: ['less', 'style', 'main', '...'],
mainFiles: ['index', '...'],
extensions: ['.less', '.css'],
preferRelative: true,
alias: webpackResolveAlias,
alias: webpackResolveAlias ?? {},
modules: ['node_modules', ...(lessIncludePaths ?? [])],
});

Expand Down
23 changes: 11 additions & 12 deletions packages/happy-css-modules/src/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ import { randomUUID } from 'node:crypto';
import { createRequire } from 'node:module';
import { dirname, join, resolve } from 'path';
import { fileURLToPath } from 'url';
import * as fileCacheNpm from '@file-cache/npm';
import * as fileCacheCore from '@file-cache/core';
import type { CreateCacheOptions } from '@file-cache/core';
import { jest } from '@jest/globals';
import dedent from 'dedent';
import type { RunnerOptions, Watcher } from './runner.js';
import { createFixtures, exists, getFixturePath, waitForAsyncTask } from './test-util/util.js';

const packageRootDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');

const require = createRequire(import.meta.url);

jest.unstable_mockModule('@file-cache/npm', () => ({
...fileCacheNpm, // Inherit native functions
createNpmPackageKey: () => 'mocked-key',
}));

jest.unstable_mockModule('pkg-dir', () => ({
packageDirectory: () => packageRootDir,
const uuid = randomUUID();
jest.unstable_mockModule('@file-cache/core', () => ({
...fileCacheCore, // Inherit native functions
createCache: async (options: CreateCacheOptions) => {
options.keys.push(() => uuid); // Add a random key to avoid cache collision
return fileCacheCore.createCache(options);
},
}));

const { run } = await import('./runner.js');
Expand Down Expand Up @@ -231,8 +230,8 @@ test('returns an error if the file fails to process in non-watch mode', async ()
const error = maybeError as AggregateError;
expect(error.message).toMatchInlineSnapshot(`"Failed to process files"`);
expect(error.errors).toHaveLength(2);
expect(error.errors[0]).toMatchInlineSnapshot(`<fixtures>/test/2.css:1:1: Unknown word`);
expect(error.errors[1]).toMatchInlineSnapshot(`<fixtures>/test/3.css:1:1: Unknown word`);
expect(error.errors[0]).toMatchInlineSnapshot(`<fixtures>/test/3.css:1:1: Unknown word`);
expect(error.errors[1]).toMatchInlineSnapshot(`<fixtures>/test/2.css:1:1: Unknown word`);

// The valid files are emitted.
expect(await exists(getFixturePath('/test/1.css.d.ts'))).toBe(true);
Expand Down
8 changes: 3 additions & 5 deletions packages/happy-css-modules/src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { resolve, relative } from 'path';
import * as process from 'process';
import * as util from 'util';
import { createCache } from '@file-cache/core';
import { createNpmPackageKey } from '@file-cache/npm';
import AwaitLock from 'await-lock';
import chalk from 'chalk';
import * as chokidar from 'chokidar';
import _glob from 'glob';
import { glob } from 'glob';
import { DEFAULT_ARBITRARY_EXTENSIONS } from './config.js';
import { isGeneratedFilesExist, emitGeneratedFiles } from './emitter/index.js';
import { Locator } from './locator/index.js';
Expand All @@ -16,8 +15,6 @@ import { createDefaultResolver } from './resolver/index.js';
import { createDefaultTransformer, type Transformer } from './transformer/index.js';
import { getInstalledPeerDependencies, isMatchByGlob } from './util.js';

const glob = util.promisify(_glob);

export type Watcher = {
close: () => Promise<void>;
};
Expand Down Expand Up @@ -109,8 +106,9 @@ export async function run(options: RunnerOptions): Promise<Watcher | void> {
});
const transformer = options.transformer ?? createDefaultTransformer({ cwd, postcssConfig: options.postcssConfig });

const installedPeerDependencies = await getInstalledPeerDependencies();
const installedPeerDependencies = getInstalledPeerDependencies();
const cache = await createCache({
name: 'happy-css-modules',
mode: options.cacheStrategy ?? 'content',
keys: [
() => createNpmPackageKey(['happy-css-modules', ...installedPeerDependencies]),
Expand Down
17 changes: 6 additions & 11 deletions packages/happy-css-modules/src/test-util/tsserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { readFileSync } from 'fs';
import { mkdir, writeFile as nativeWriteFile } from 'fs/promises';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { promisify } from 'util';
import serverHarness from '@typescript/server-harness';
import _glob from 'glob';
import { glob } from 'glob';
import { resolve } from 'import-meta-resolve';
import lineColumn from 'line-column';
import type { server } from 'typescript/lib/tsserverlibrary.js';
import { getFixturePath } from './util.js';
const glob = promisify(_glob);

async function writeFile(path: string, content: string): Promise<void> {
await mkdir(dirname(path), { recursive: true });
Expand Down Expand Up @@ -37,14 +35,11 @@ type Definition = {
};
};

export async function createTSServer() {
const server = serverHarness.launchServer(
fileURLToPath(await resolve('typescript/lib/tsserver.js', import.meta.url)),
[
// ATA generates some extra network traffic and isn't usually relevant when profiling
'--disableAutomaticTypingAcquisition',
],
);
export function createTSServer() {
const server = serverHarness.launchServer(fileURLToPath(resolve('typescript/lib/tsserver.js', import.meta.url)), [
// ATA generates some extra network traffic and isn't usually relevant when profiling
'--disableAutomaticTypingAcquisition',
]);

return {
async getIdentifierDefinitions(filePath: string, identifier: string): Promise<Definition[]> {
Expand Down
16 changes: 9 additions & 7 deletions packages/happy-css-modules/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { access } from 'fs/promises';
import { join, dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import minimatch from 'minimatch';
import { minimatch } from 'minimatch';

/**
* The SystemError type of Node.js.
Expand Down Expand Up @@ -73,15 +73,17 @@ export function getPackageJson() {
return JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf-8'));
}

export async function getInstalledPeerDependencies(): Promise<string[]> {
export function getInstalledPeerDependencies(): string[] {
const pkgJson = getPackageJson();
const result = [];
for (const deps of Object.keys(pkgJson.peerDependencies)) {
// eslint-disable-next-line no-await-in-loop
const isInstalled = await importMetaResolve(deps, import.meta.url)
.then(() => true)
.catch(() => false);
if (isInstalled) result.push(deps);
try {
importMetaResolve(deps, import.meta.url);
// If the package is installed, push it to the result array.
result.push(deps);
} catch {
// If the package is not installed, do nothing.
}
}
return result;
}
18 changes: 0 additions & 18 deletions patches/postcss-load-config+4.0.1.dev.patch

This file was deleted.

0 comments on commit b5fb70c

Please sign in to comment.