diff --git a/README.md b/README.md new file mode 100644 index 00000000..b52602bb --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# @docsearch/js + +JavaScript package for [DocSearch](http://docsearch.algolia.com/), the best search experience for docs. + +## Installation + +```sh +yarn add @docsearch/js@alpha +# or +npm install @docsearch/js@alpha +``` + +## Documentation + +[Read documentation →](https://autocomplete-experimental.netlify.app/docs/docsearch-js) diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 00000000..a614d94d --- /dev/null +++ b/babel.config.js @@ -0,0 +1,39 @@ +/* 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'], + [ + 'module-resolver', + { + root: ['./src'], + alias: { + react: 'preact/compat', + 'react-dom': 'preact/compat', + }, + }, + ], + ], + }; +}; diff --git a/package.json b/package.json new file mode 100644 index 00000000..4419da8f --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "@docsearch/js", + "description": "JavaScript package for DocSearch, the best search experience for docs.", + "version": "1.0.0-alpha.28", + "license": "MIT", + "homepage": "https://github.com/francoischalifour/autocomplete.js", + "repository": "francoischalifour/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 && yarn build:types", + "build:umd": "BUILD=umd rollup --config", + "build:esm": "BUILD=esm rollup --config", + "build:types": "tsc -p ./tsconfig.declaration.json --outFile ./dist/esm/index.d.ts", + "build:clean": "rm -rf ./dist", + "on:change": "concurrently \"yarn build:esm\" \"yarn build:types\"", + "watch": "watch \"yarn on:change\" --ignoreDirectoryPattern \"/dist/\"" + }, + "dependencies": { + "@docsearch/react": "^1.0.0-alpha.28", + "preact": "^10.0.0" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..14b8a11d --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,30 @@ +import { plugins } from '../../rollup.base.config'; +import { getBundleBanner } from '../../scripts/getBundleBanner'; + +import pkg from './package.json'; + +if (!process.env.BUILD) { + throw new Error('The `BUILD` environment variable is required to build.'); +} + +const output = { + umd: { + file: 'dist/umd/index.js', + format: 'umd', + sourcemap: true, + name: 'docsearch', + banner: getBundleBanner(pkg), + }, + esm: { + file: 'dist/esm/index.js', + format: 'es', + sourcemap: true, + banner: getBundleBanner(pkg), + }, +}; + +export default { + input: 'src/index.ts', + output: output[process.env.BUILD], + plugins, +}; diff --git a/src/docsearch.tsx b/src/docsearch.tsx new file mode 100644 index 00000000..f0c0baa4 --- /dev/null +++ b/src/docsearch.tsx @@ -0,0 +1,38 @@ +import { + DocSearch, + DocSearchProps as DocSearchComponentProps, + version, +} from '@docsearch/react'; +import React, { render } from 'preact/compat'; + +function getHTMLElement( + value: string | HTMLElement, + environment: DocSearchProps['environment'] = window +): HTMLElement { + if (typeof value === 'string') { + return environment.document.querySelector(value)!; + } + + return value; +} + +interface DocSearchProps extends DocSearchComponentProps { + container: string | HTMLElement; + environment?: typeof window; +} + +export function docsearch(props: DocSearchProps) { + render( + { + searchClient.addAlgoliaAgent('docsearch.js', version); + + return props.transformSearchClient + ? props.transformSearchClient(searchClient) + : searchClient; + }} + />, + 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" +}