Skip to content

Commit 4f00112

Browse files
committed
Convert project to TypeScript
1 parent 8c29011 commit 4f00112

10 files changed

+954
-341
lines changed

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ jobs:
2727
cache: "pnpm"
2828
- name: Install dependencies
2929
run: pnpm install
30+
- name: Build project
31+
run: pnpm build
3032
- name: Run unit tests
3133
run: pnpm test

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ npm-debug.log
33
yarn-error.log
44

55
coverage/
6+
7+
# build products
8+
index.js
9+
index.d.ts

eslint.config.mjs

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
// @ts-check
2+
3+
import path from "node:path";
4+
import { fileURLToPath } from "node:url";
15
import noOnlyPlugin from "eslint-plugin-no-only-tests";
26
import eslint from "@eslint/js";
7+
import tseslint from "typescript-eslint";
8+
9+
// mimic CommonJS variables
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = path.dirname(__filename);
312

4-
export default [
13+
export default tseslint.config(
514
eslint.configs.recommended,
15+
...tseslint.configs.recommended,
16+
{
17+
ignores: ["coverage/*", "index.js", "index.test.mjs", "index.d.ts"],
18+
},
19+
{
20+
languageOptions: {
21+
parserOptions: {
22+
project: "./tsconfig.eslint.json",
23+
tsconfigRootDir: __dirname,
24+
},
25+
},
26+
},
627
{
728
rules: {
829
semi: "error",
@@ -24,5 +45,5 @@ export default [
2445
"no-only-tests": noOnlyPlugin,
2546
},
2647
rules: { "no-only-tests/no-only-tests": "error" },
27-
},
28-
];
48+
}
49+
);

index.test.mjs index.test.mts

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import path from "node:path";
33
import postcss from "postcss";
44
import prettier from "prettier";
55
import { describe, it, expect } from "vitest";
6-
7-
const plugin = (await import(".")).default;
6+
import { plugin, type ConfigItem } from "./index.js";
87

98
// We don't care about formatting differences, so normalize with prettier
10-
function format(css) {
9+
function format(css: string) {
1110
return prettier.format(css, { parser: "css" });
1211
}
1312

14-
async function run(input, output, opts, postcssOpts = {}) {
13+
async function run(
14+
input: string,
15+
output: string,
16+
opts?: ConfigItem[],
17+
postcssOpts = {}
18+
) {
1519
const result = await postcss([plugin(opts)]).process(input, postcssOpts);
1620
expect(format(result.css)).toEqual(format(output));
1721
expect(result.warnings()).toHaveLength(0);

index.js index.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
const { createFilter } = require("@rollup/pluginutils");
1+
import { createFilter } from "@rollup/pluginutils";
2+
import type { PluginCreator } from "postcss";
23

34
const DEFAULT_INCLUDE = "**/*.module.css";
45
const DEFAULT_LAYERNAME = "components";
56

6-
/**
7-
* @type {import('postcss').PluginCreator}
8-
*/
9-
module.exports = (
7+
export type ConfigItem = {
8+
include?: string;
9+
layerName?: string;
10+
};
11+
type PluginOptions = ConfigItem[];
12+
13+
export const plugin: PluginCreator<PluginOptions> = (
1014
configItems = [
1115
{
1216
include: DEFAULT_INCLUDE,
1317
layerName: DEFAULT_LAYERNAME,
1418
},
1519
]
1620
) => {
17-
const filters = [];
21+
const filters: { filter: (id: string) => boolean; layerName: string }[] = [];
1822

1923
for (const config of configItems) {
2024
const filter = createFilter(config.include ?? DEFAULT_INCLUDE);
@@ -23,12 +27,12 @@ module.exports = (
2327

2428
return {
2529
postcssPlugin: "postcss-assign-layers",
26-
async Once(root, { AtRule }) {
27-
const inputFile = root.source.input.file;
30+
Once(root, { AtRule }) {
31+
const inputFile = root.source?.input.file;
2832
const layerNames = [];
2933

3034
for (const { filter, layerName } of filters) {
31-
if (filter(inputFile)) {
35+
if (inputFile && filter(inputFile)) {
3236
layerNames.push(layerName);
3337
}
3438
}
@@ -45,5 +49,6 @@ module.exports = (
4549
},
4650
};
4751
};
52+
plugin.postcss = true;
4853

49-
module.exports.postcss = true;
54+
export default plugin;

package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"postcss-assign-layer"
1010
],
1111
"scripts": {
12+
"build": "tsc -p tsconfig.json",
1213
"test": "vitest run --coverage && eslint .",
1314
"test:watch": "vitest"
1415
},
@@ -20,7 +21,8 @@
2021
},
2122
"homepage": "https://github.com/DefinedNet/postcss-assign-layer#readme",
2223
"files": [
23-
"index.js"
24+
"index.js",
25+
"index.d.ts"
2426
],
2527
"engines": {
2628
"node": ">=12.0.0"
@@ -30,14 +32,20 @@
3032
},
3133
"devDependencies": {
3234
"@eslint/js": "^8.57.0",
35+
"@types/eslint": "^8.56.6",
36+
"@types/node": "^16.18.91",
37+
"@types/prettier": "^2.6.0",
38+
"@vitest/coverage-v8": "^0.34.0",
3339
"c8": "^7.11.3",
3440
"clean-publish": "^3.4.2",
3541
"eslint": "^8.57.0",
3642
"eslint-plugin-no-only-tests": "^3.1.0",
3743
"postcss": "^8.3.11",
3844
"prettier": "^2.6.2",
39-
"vite": "^2.9.9",
40-
"vitest": "^0.12.6"
45+
"typescript": "^5.4.2",
46+
"typescript-eslint": "^7.3.1",
47+
"vite": "^4.0.0",
48+
"vitest": "^0.34.0"
4149
},
4250
"dependencies": {
4351
"@rollup/pluginutils": "^4.2.1"

0 commit comments

Comments
 (0)