Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Sep 2, 2021
1 parent bf8f5b5 commit 8ea57fb
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"root": true,
"extends": [
"eslint-config-niksy",
"eslint-config-niksy/typescript",
"eslint-config-niksy/next",
"eslint-config-prettier"
],
Expand Down
49 changes: 33 additions & 16 deletions generate-list.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
/// <reference lib="dom" />

/* globals Document */
/* eslint-disable unicorn/prefer-spread */

import path from 'path';
import { URL } from 'url';
import fetch from 'isomorphic-unfetch';
import { JSDOM } from 'jsdom';
import writeJsonFile from 'write-json-file';

const references = [
{
url: 'https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix',
transform: (document) => {
let list = document.querySelectorAll('#css_prefixes + div li code');
list = [...list].map((node) => node.textContent);
transform: (/** @type {Document} */ document) => {
const list = Array.from(
document.querySelectorAll('#css_prefixes + div li code')
).map((node) => node.textContent || '');
return list;
}
},
{
url: 'https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Functions',
transform: (document) => {
let list = document.querySelectorAll('.main-page-content dt code');
list = [...list]
.map((node) => node.textContent)
transform: (/** @type {Document} */ document) => {
const list = Array.from(
document.querySelectorAll('.main-page-content dt code')
)
.map((node) => node.textContent || '')
.map((name) => name.replace('()', ''));
return list;
}
},
{
url: 'https://developer.mozilla.org/en-US/docs/Web/CSS/Reference',
transform: (document) => {
let list = document.querySelectorAll('#index + div .index li code');
list = [...list]
.map((node) => node.textContent)
transform: (/** @type {Document} */ document) => {
const list = Array.from(
document.querySelectorAll('#index + div .index li code')
)
.map((node) => node.textContent || '')
.filter((name) => /^[^:]\S+\(\)$/.test(name))
.map((name) => name.replace('()', ''));
return list;
Expand All @@ -48,14 +57,22 @@ async function main() {
} = new JSDOM(markup);
return references[index].transform(document);
});
let results = [...new Set([].concat(...functions))].sort();
results = [
...results,

/** @type {string[]} */
let results = [];
results = [...new Set(results.concat(...functions))].sort();

const combinedResults = [
results,
...prefixes.map((prefix) => results.map((name) => `${prefix}${name}`))
];
results = [].concat(...results);

/** @type {string[]} */
let flattenedResults = [];
flattenedResults = flattenedResults.concat(...combinedResults);

const filename = new URL('index.json', import.meta.url).pathname;
await writeJsonFile(filename, results);
await writeJsonFile(filename, flattenedResults);
console.log('Done!');
}

Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { URL } from 'url';

/**
* Path to CSS functions list JSON file.
*/
const location = new URL('index.json', import.meta.url).pathname;

export default location;
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "esm/index.d.ts",
"directories": {
"test": "test"
},
Expand All @@ -38,9 +39,14 @@
"test:watch": "npm test -- --watch",
"build": "rollup --config rollup.config.js",
"module-check": "node -e 'require(\"css-functions-list\");' && node --input-type=module -e 'import \"css-functions-list\";'",
"prepublishOnly": "npm run build && npm run module-check"
"prepublishOnly": "npm run build && npm run lint:types && npm run module-check",
"lint:types": "tsc"
},
"devDependencies": {
"@types/jsdom": "^16.2.13",
"@types/mocha": "^8.2.3",
"@types/node": "^16.3.0",
"@types/node-fetch": "^2.5.12",
"changelog-verify": "^1.1.2",
"cpy": "^8.1.2",
"eslint": "^7.31.0",
Expand All @@ -54,6 +60,7 @@
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-unicorn": "^31.0.0",
"esm": "^3.0.51",
"execa": "^5.1.1",
"github-release-from-changelog": "^2.1.1",
"husky": "^4.3.0",
"isomorphic-unfetch": "^3.1.0",
Expand All @@ -63,6 +70,7 @@
"np": "^6.5.0",
"prettier": "^2.1.2",
"rollup": "^2.32.1",
"typescript": "^4.3.5",
"version-changelog": "^3.1.1",
"write-json-file": "^4.3.0"
},
Expand Down
50 changes: 50 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const { promises: fs } = require('fs');
const execa = require('execa');
const cpy = require('cpy');

module.exports = {
Expand All @@ -20,6 +21,55 @@ module.exports = {
}
],
plugins: [
(() => {
return {
name: 'types',
async writeBundle(output) {
let prefix;
if (output.file.includes('cjs/')) {
prefix = 'cjs';
} else if (output.file.includes('esm/')) {
prefix = 'esm';
}
if (typeof prefix !== 'undefined') {
const tsconfig = {
extends: './tsconfig',
exclude: ['test/**/*.js', 'generate-list.js'],
compilerOptions: {
types: ['node'],
declaration: true,
declarationMap: true,
declarationDir: prefix,
emitDeclarationOnly: true,
noEmit: false,
listEmittedFiles: true
}
};
const file = `.${prefix}.tsconfig.json`;
try {
await fs.writeFile(
file,
JSON.stringify(tsconfig),
'utf-8'
);
const { stdout } = await execa(
'tsc',
['-p', file],
{
preferLocal: true
}
);
try {
await cpy('types', `${prefix}/types`);
} catch (error) {}
console.log(stdout);
} finally {
await fs.unlink(file);
}
}
}
};
})(),
(() => {
return {
name: 'package-type',
Expand Down
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext"],
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"noEmit": true,
"newLine": "lf",
"strict": true,
"noPropertyAccessFromIndexSignature": true,
"noEmitOnError": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"checkJs": true
},
"include": ["index.js", "lib/**/*.js", "test/**/*.js", "generate-list.js"]
}

0 comments on commit 8ea57fb

Please sign in to comment.