diff --git a/package.json b/package.json index a941d30f..46c252ce 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,13 @@ "url": "https://github.com/sponsors/fb55" }, "repository": { + "type": "git", "url": "https://github.com/fb55/css-what" }, - "main": "lib/index.js", - "types": "lib/index.d.ts", + "main": "lib/commonjs/index.js", + "module": "lib/es/index.js", + "types": "lib/es/index.d.ts", + "sideEffects": false, "files": [ "lib/**/*" ], @@ -24,7 +27,7 @@ "format:es": "npm run lint:es -- --fix", "format:prettier": "npm run prettier -- --write", "prettier": "prettier '**/*.{ts,md,json,yml}'", - "build": "tsc", + "build": "tsc && tsc -p tsconfig.es.json", "prepare": "npm run build" }, "devDependencies": { @@ -45,7 +48,10 @@ }, "license": "BSD-2-Clause", "jest": { - "preset": "ts-jest" + "preset": "ts-jest", + "roots": [ + "src" + ] }, "prettier": { "tabWidth": 4 diff --git a/src/index.ts b/src/index.ts index 03b8ecd3..1cf4a531 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ -export * from "./parse"; -export { default as parse } from "./parse"; -export { default as stringify } from "./stringify"; +export * from "./types"; +export { isTraversal, parse } from "./parse"; +export { stringify } from "./stringify"; diff --git a/src/parse.ts b/src/parse.ts index 07c53404..f790a0c4 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,83 +1,12 @@ -export interface Options { - /** - * When false, tag names will not be lowercased. - * @default true - */ - lowerCaseAttributeNames?: boolean; - /** - * When false, attribute names will not be lowercased. - * @default true - */ - lowerCaseTags?: boolean; - /** - * When `true`, `xmlMode` implies both `lowerCaseTags` and `lowerCaseAttributeNames` are set to `false`. - * Also, `ignoreCase` on attributes will not be inferred based on HTML rules anymore. - * @default false - */ - xmlMode?: boolean; -} - -export type Selector = - | PseudoSelector - | PseudoElement - | AttributeSelector - | TagSelector - | UniversalSelector - | Traversal; - -export interface AttributeSelector { - type: "attribute"; - name: string; - action: AttributeAction; - value: string; - ignoreCase: boolean | null; - namespace: string | null; -} - -type DataType = Selector[][] | null | string; - -export interface PseudoSelector { - type: "pseudo"; - name: string; - data: DataType; -} - -export interface PseudoElement { - type: "pseudo-element"; - name: string; -} - -export interface TagSelector { - type: "tag"; - name: string; - namespace: string | null; -} - -export interface UniversalSelector { - type: "universal"; - namespace: string | null; -} - -export interface Traversal { - type: TraversalType; -} - -export type AttributeAction = - | "any" - | "element" - | "end" - | "equals" - | "exists" - | "hyphen" - | "not" - | "start"; - -export type TraversalType = - | "adjacent" - | "child" - | "descendant" - | "parent" - | "sibling"; +import type { + Options, + Selector, + AttributeSelector, + Traversal, + AttributeAction, + TraversalType, + DataType, +} from "./types"; const reName = /^[^\\#]?(?:\\(?:[\da-f]{1,6}\s?|.)|[\w\-\u00b0-\uFFFF])+/; const reEscape = /\\([\da-f]{1,6}\s?|(\s)|.)/gi; @@ -220,10 +149,7 @@ function isWhitespace(c: string) { * The first dimension represents selectors separated by commas (eg. `sub1, sub2`), * the second contains the relevant tokens for that selector. */ -export default function parse( - selector: string, - options?: Options -): Selector[][] { +export function parse(selector: string, options?: Options): Selector[][] { const subselects: Selector[][] = []; const endIndex = parseSelector(subselects, `${selector}`, options, 0); diff --git a/src/stringify.ts b/src/stringify.ts index 1287932c..442b6db8 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -1,4 +1,4 @@ -import { Selector } from "./parse"; +import type { Selector } from "./types"; const actionTypes: Record = { equals: "", @@ -29,7 +29,7 @@ const charsToEscape = new Set([ * * @param selector Selector to stringify. */ -export default function stringify(selector: Selector[][]): string { +export function stringify(selector: Selector[][]): string { return selector.map(stringifySubselector).join(", "); } diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..c8d2d7c9 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,80 @@ +export interface Options { + /** + * When false, tag names will not be lowercased. + * @default true + */ + lowerCaseAttributeNames?: boolean; + /** + * When false, attribute names will not be lowercased. + * @default true + */ + lowerCaseTags?: boolean; + /** + * When `true`, `xmlMode` implies both `lowerCaseTags` and `lowerCaseAttributeNames` are set to `false`. + * Also, `ignoreCase` on attributes will not be inferred based on HTML rules anymore. + * @default false + */ + xmlMode?: boolean; +} + +export type Selector = + | PseudoSelector + | PseudoElement + | AttributeSelector + | TagSelector + | UniversalSelector + | Traversal; + +export interface AttributeSelector { + type: "attribute"; + name: string; + action: AttributeAction; + value: string; + ignoreCase: boolean | null; + namespace: string | null; +} + +export type DataType = Selector[][] | null | string; + +export interface PseudoSelector { + type: "pseudo"; + name: string; + data: DataType; +} + +export interface PseudoElement { + type: "pseudo-element"; + name: string; +} + +export interface TagSelector { + type: "tag"; + name: string; + namespace: string | null; +} + +export interface UniversalSelector { + type: "universal"; + namespace: string | null; +} + +export interface Traversal { + type: TraversalType; +} + +export type AttributeAction = + | "any" + | "element" + | "end" + | "equals" + | "exists" + | "hyphen" + | "not" + | "start"; + +export type TraversalType = + | "adjacent" + | "child" + | "descendant" + | "parent" + | "sibling"; diff --git a/tsconfig.es.json b/tsconfig.es.json new file mode 100644 index 00000000..8b7628c2 --- /dev/null +++ b/tsconfig.es.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "target": "ES2019", + "module": "es2015", + "outDir": "lib/es", + "moduleResolution": "node" + } +} diff --git a/tsconfig.json b/tsconfig.json index 5d16dc75..6d6b80f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,7 @@ "declaration": true /* Generates corresponding '.d.ts' file. */, "declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */, // "sourceMap": true, /* Generates corresponding '.map' file. */ - "outDir": "lib" /* Redirect output structure to the directory. */, + "outDir": "lib/commonjs" /* Redirect output structure to the directory. */, // "importHelpers": true, /* Import emit helpers from 'tslib'. */ /* Strict Type-Checking Options */