From c7f2645d3fbaf7fc3c33d88ccb1b462c00652e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Thu, 9 Jul 2020 10:54:38 +0200 Subject: [PATCH] feat(docsearch): introduce DocSearch.js v3 (#56) --- babel.config.js | 27 +++++++++++++++++++++++++++ package.json | 36 ++++++++++++++++++++++++++++++++++++ rollup.config.js | 24 ++++++++++++++++++++++++ src/docsearch.tsx | 21 +++++++++++++++++++++ src/index.ts | 1 + tsconfig.declaration.json | 8 ++++++++ tsconfig.json | 3 +++ 7 files changed, 120 insertions(+) create mode 100644 babel.config.js create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 src/docsearch.tsx create mode 100644 src/index.ts create mode 100644 tsconfig.declaration.json create mode 100644 tsconfig.json diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 00000000..ec916c22 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,27 @@ +/* eslint-disable import/no-commonjs */ + +module.exports = (api) => { + const isTest = api.env('test'); + const modules = isTest ? 'commonjs' : false; + const targets = {}; + + if (isTest) { + targets.node = true; + } else { + targets.browsers = ['last 2 versions', 'ie >= 11']; + } + + return { + presets: [ + '@babel/preset-typescript', + [ + '@babel/preset-env', + { + modules, + targets, + }, + ], + ], + plugins: [['@babel/plugin-transform-react-jsx']], + }; +}; diff --git a/package.json b/package.json new file mode 100644 index 00000000..f562bbd8 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "@docsearch/js", + "version": "1.0.0-alpha.21", + "license": "MIT", + "homepage": "https://github.com/algolia/autocomplete.js", + "repository": "algolia/autocomplete.js", + "author": { + "name": "Algolia, Inc.", + "url": "https://www.algolia.com" + }, + "sideEffects": false, + "files": [ + "dist/" + ], + "source": "src/index.ts", + "types": "dist/esm/index.d.ts", + "module": "dist/esm/index.js", + "main": "dist/umd/index.js", + "umd:main": "dist/umd/index.js", + "unpkg": "dist/umd/index.js", + "jsdelivr": "dist/umd/index.js", + "scripts": { + "build": "yarn build:clean && yarn build:umd && yarn build:esm", + "build:esm": "babel src --root-mode upward --extensions '.ts,.tsx' --out-dir dist/esm", + "build:esm:watch": "yarn build:esm --watch", + "build:umd": "rollup --config", + "build:types": "tsc -p ./tsconfig.declaration.json --outDir ./dist/esm", + "build:types:watch": "chokidar \"**/*.ts\" \"**/*.tsx\" --command \"yarn build:types\" --ignore \"dist\"", + "build:clean": "rm -rf ./dist", + "watch": "concurrently \"yarn build:esm:watch\" \"yarn build:types:watch\"" + }, + "dependencies": { + "@docsearch/react": "^1.0.0-alpha.21", + "preact": "^10.0.0" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..e36fda13 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,24 @@ +import json from '@rollup/plugin-json'; +import alias from '@rollup/plugin-alias'; + +import { sharedPlugins } from '../autocomplete-core/rollup.config'; + +export default { + input: 'src/index.ts', + output: { + file: 'dist/umd/index.js', + format: 'umd', + sourcemap: true, + name: 'docsearch', + }, + plugins: [ + json(), + alias({ + entries: [ + { find: 'react', replacement: 'preact/compat' }, + { find: 'react-dom', replacement: 'preact/compat' }, + ], + }), + ...sharedPlugins, + ], +}; diff --git a/src/docsearch.tsx b/src/docsearch.tsx new file mode 100644 index 00000000..7d36a846 --- /dev/null +++ b/src/docsearch.tsx @@ -0,0 +1,21 @@ +import React, { render } from 'preact/compat'; + +import { DocSearch } from '@docsearch/react'; + +function getHTMLElement( + value: string | HTMLElement, + environment: typeof window = window +): HTMLElement { + if (typeof value === 'string') { + return environment.document.querySelector(value)!; + } + + return value; +} + +export function docsearch(props) { + render( + , + getHTMLElement(props.container, props.environment) + ); +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 00000000..4f419249 --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export { docsearch as default } from './docsearch'; diff --git a/tsconfig.declaration.json b/tsconfig.declaration.json new file mode 100644 index 00000000..9bd7baf2 --- /dev/null +++ b/tsconfig.declaration.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "noEmit": false, + "declaration": true, + "emitDeclarationOnly": true + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..41716a7d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig" +}