Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert project to TypeScript #6

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ jobs:
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Build project
run: pnpm build
- name: Run unit tests
run: pnpm test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ npm-debug.log
yarn-error.log

coverage/

# build products
index.js
index.d.ts
27 changes: 24 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
// @ts-check

import path from "node:path";
import { fileURLToPath } from "node:url";
import noOnlyPlugin from "eslint-plugin-no-only-tests";
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

// mimic CommonJS variables
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default [
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
ignores: ["coverage/*", "index.js", "index.test.mjs", "index.d.ts"],
},
{
languageOptions: {
parserOptions: {
project: "./tsconfig.eslint.json",
tsconfigRootDir: __dirname,
},
},
},
{
rules: {
semi: "error",
Expand All @@ -24,5 +45,5 @@ export default [
"no-only-tests": noOnlyPlugin,
},
rules: { "no-only-tests/no-only-tests": "error" },
},
];
}
);
12 changes: 8 additions & 4 deletions index.test.mjs → index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import path from "node:path";
import postcss from "postcss";
import prettier from "prettier";
import { describe, it, expect } from "vitest";

const plugin = (await import(".")).default;
import { plugin, type ConfigItem } from "./index.js";

// We don't care about formatting differences, so normalize with prettier
function format(css) {
function format(css: string) {
return prettier.format(css, { parser: "css" });
}

async function run(input, output, opts, postcssOpts = {}) {
async function run(
input: string,
output: string,
opts?: ConfigItem[],
postcssOpts = {}
) {
const result = await postcss([plugin(opts)]).process(input, postcssOpts);
expect(format(result.css)).toEqual(format(output));
expect(result.warnings()).toHaveLength(0);
Expand Down
25 changes: 15 additions & 10 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
const { createFilter } = require("@rollup/pluginutils");
import { createFilter } from "@rollup/pluginutils";
import type { PluginCreator } from "postcss";

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

/**
* @type {import('postcss').PluginCreator}
*/
module.exports = (
export type ConfigItem = {
include?: string;
layerName?: string;
};
type PluginOptions = ConfigItem[];

export const plugin: PluginCreator<PluginOptions> = (
configItems = [
{
include: DEFAULT_INCLUDE,
layerName: DEFAULT_LAYERNAME,
},
]
) => {
const filters = [];
const filters: { filter: (id: string) => boolean; layerName: string }[] = [];

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

return {
postcssPlugin: "postcss-assign-layers",
async Once(root, { AtRule }) {
const inputFile = root.source.input.file;
Once(root, { AtRule }) {
const inputFile = root.source?.input.file;
const layerNames = [];

for (const { filter, layerName } of filters) {
if (filter(inputFile)) {
if (inputFile && filter(inputFile)) {
layerNames.push(layerName);
}
}
Expand All @@ -45,5 +49,6 @@ module.exports = (
},
};
};
plugin.postcss = true;

module.exports.postcss = true;
export default plugin;
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"postcss-assign-layer"
],
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "vitest run --coverage && eslint .",
"test:watch": "vitest"
"test:watch": "vitest",
"prepublishOnly": "pnpm build && pnpm test"
},
"author": "Ian VanSchooten <ian.vanschooten@gmail.com>",
"license": "MIT",
Expand All @@ -20,7 +22,8 @@
},
"homepage": "https://github.com/DefinedNet/postcss-assign-layer#readme",
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"engines": {
"node": ">=12.0.0"
Expand All @@ -30,14 +33,20 @@
},
"devDependencies": {
"@eslint/js": "^8.57.0",
"@types/eslint": "^8.56.6",
"@types/node": "^16.18.91",
"@types/prettier": "^2.6.0",
"@vitest/coverage-v8": "^0.34.0",
"c8": "^7.11.3",
"clean-publish": "^3.4.2",
"eslint": "^8.57.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"postcss": "^8.3.11",
"prettier": "^2.6.2",
"vite": "^2.9.9",
"vitest": "^0.12.6"
"typescript": "^5.4.2",
"typescript-eslint": "^7.3.1",
"vite": "^4.0.0",
"vitest": "^0.34.0"
},
"dependencies": {
"@rollup/pluginutils": "^4.2.1"
Expand Down
Loading
Loading