├── .babelrc ├── .nvmrc ├── example ├── src │ ├── side.ts │ ├── index.css │ └── index.ts ├── build.sh ├── types.sh ├── serve.sh ├── web │ ├── index.ts │ └── index.html ├── typehead.config.cjs ├── tsconfig.types.json └── tsconfig.json ├── assets └── logo.png ├── .travis.yml ├── .eslintrc.json ├── src ├── util │ ├── regex.mjs │ ├── findEntryPoint.mjs │ └── getEsbuildConfig.mjs ├── serve.mjs └── build.mjs ├── CHANGELOG.md ├── index.mjs ├── LICENSE ├── package.json ├── .gitignore └── README.md /.babelrc: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v14.16.0 -------------------------------------------------------------------------------- /example/src/side.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /example/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | width: 100%; 3 | } 4 | -------------------------------------------------------------------------------- /example/build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | node ../src/build.mjs 4 | -------------------------------------------------------------------------------- /assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mapbox/typehead/HEAD/assets/logo.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | matrix: 3 | include: 4 | - script: npm run lint 5 | -------------------------------------------------------------------------------- /example/types.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ../node_modules/.bin/tsc -p tsconfig.types.json -------------------------------------------------------------------------------- /example/serve.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | node ../src/serve.mjs --print-esbuild-config 4 | -------------------------------------------------------------------------------- /example/web/index.ts: -------------------------------------------------------------------------------- 1 | import { hello } from '../src/index'; 2 | 3 | document.querySelector('#hello').innerHTML = hello(); 4 | -------------------------------------------------------------------------------- /example/typehead.config.cjs: -------------------------------------------------------------------------------- 1 | console.log('hello!'); 2 | 3 | module.exports = { 4 | loader: { 5 | '.css': 'text', 6 | }, 7 | globalName: 'example' 8 | }; 9 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["@mapbox/eslint-config-mapbox/base", "prettier"], 3 | "parser": "@babel/eslint-parser", 4 | "env": { 5 | "node": true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /example/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/src/index.ts: -------------------------------------------------------------------------------- 1 | import { isString } from 'lodash'; 2 | 3 | // @ts-expect-error - this is a string 4 | import css from './index.css'; 5 | 6 | export { css }; 7 | 8 | export function hello() { 9 | return isString('Hello world!'); 10 | } 11 | -------------------------------------------------------------------------------- /example/tsconfig.types.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "declaration": true, 5 | "emitDeclarationOnly": true, 6 | "outDir": "./dist" 7 | }, 8 | "include": ["src/*", "src/**/*"] 9 | } 10 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "esnext", 4 | "target": "ES2020", 5 | // This is needed since we use both 'esnext' and 'node' module resolution. 6 | // https://www.typescriptlang.org/docs/handbook/module-resolution.html#module-resolution-strategies 7 | "moduleResolution": "node", 8 | "lib": ["dom", "ES2020", "ESNext"], 9 | // Treat JSX as React. 10 | "jsx": "react", 11 | // Make sure CommonJS modules are read correctly. 12 | "esModuleInterop": true, 13 | // Significant perf increase by skipping checking .d.ts files. 14 | "skipLibCheck": true 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/util/regex.mjs: -------------------------------------------------------------------------------- 1 | /** 2 | * These are inverses of the same regex string from here: 3 | * https://github.com/evanw/esbuild/issues/619#issuecomment-751995294 4 | * 5 | * This uses Node resolution behavior. Any file starting like ./, ../, or / 6 | * is local to the project. This means we should bundle it and run optimizations. 7 | * 8 | * Inversely, any import like 'this-import' is external and shouldn't be bundled. 9 | */ 10 | export const IS_RELATIVE = /^[./]|^\.[./]|^\.\.[/]/; 11 | export const IS_EXTERNAL = /^[^./]|^\.[^./]|^\.\.[^/]/; 12 | 13 | /** 14 | * This is the same as IS_RELATIVE, but makes sure the file has the 15 | * extensions js/jsx/ts/tsx/cjs/mjs. 16 | */ 17 | export const IS_RELATIVE_AND_JS = 18 | /(^[./]|^\.[./]|^\.\.[/]).*\.(mjs|cjs|jsx*|tsx*)$/; 19 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # v1.2.1 2 | 3 | - Allow for `"platform": "node"` to be specified and not overridden. 4 | 5 | # v1.2.0 6 | 7 | - If `globalName` is specified in `typehead.config.js`, then a fourth IIFE ("CDN") build will be created with that name. 8 | 9 | # v1.1.0 10 | 11 | - 🚨 [breaking change] Upgrade from ESBuild 0.12.0 to 0.14.0. 12 | - 🚨 [breaking change] Only run Lodash plugin on JavaScript files (expected but was bug). 13 | 14 | - Add `--print-esbuild-config` flag for debugging. 15 | - Remove outdated reference to `config.mjs` 16 | 17 | # v1.0.3 18 | 19 | - Drop fork of `esbuild-plugin-lodash` for published release. 20 | 21 | # v1.0.2 22 | 23 | - Add infrastructure linting and bump "engines" version in `package.json`. 24 | 25 | # v1.0.1 26 | 27 | - Make public on NPM. 🎉 28 | 29 | # v1.0.0 30 | 31 | - Initial release! 32 | -------------------------------------------------------------------------------- /src/util/findEntryPoint.mjs: -------------------------------------------------------------------------------- 1 | import chalk from 'chalk'; 2 | import { access } from 'fs/promises'; 3 | 4 | /** 5 | * Find the entry point with a suitable extension. 6 | */ 7 | export async function findEntryPoint( 8 | filename, 9 | extensions = ['.tsx', '.ts', '.jsx', '.js'] 10 | ) { 11 | for (const ext of extensions) { 12 | try { 13 | // Will throw error if not found. 14 | await access(filename + ext); 15 | return filename + ext; 16 | // eslint-disable-next-line no-empty 17 | } catch {} 18 | } 19 | 20 | console.error( 21 | chalk.red( 22 | `Couldn't find ${filename}! Alternatively, specify 'entryPoints' in typehead.config.js / your Typehead configuration.\n\nReference: https://github.com/mapbox/typehead#customization\nESBuild Reference: https://esbuild.github.io/api/#entry-points` 23 | ) 24 | ); 25 | process.exit(1); 26 | } 27 | -------------------------------------------------------------------------------- /index.mjs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import chalk from 'chalk'; 4 | 5 | import { spawn } from 'child_process'; 6 | 7 | function showHelp() { 8 | console.warn(`Usage: typehead [subcommand] 9 | 10 | Refreshingly simple CLI for TypeScript packages. 11 | 12 | Options: 13 | -h, --help Display help for [subcommand] 14 | `); 15 | } 16 | 17 | if (process.argv.length < 3) { 18 | showHelp(); 19 | process.exit(1); 20 | } 21 | 22 | const subcommand = process.argv[2]; 23 | const child = spawn(`typehead-${subcommand}`, process.argv.slice(3)); 24 | 25 | child.stdout.pipe(process.stdout); 26 | child.stderr.pipe(process.stderr); 27 | 28 | child.on('error', (e) => { 29 | if (e.message.includes('ENOENT')) { 30 | console.warn( 31 | chalk.yellow(`Couldn't find typehead-${subcommand} in your PATH. ⚠️\n`) 32 | ); 33 | showHelp(); 34 | process.exit(1); 35 | return; 36 | } 37 | 38 | console.error(e); 39 | }); 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Mapbox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/util/getEsbuildConfig.mjs: -------------------------------------------------------------------------------- 1 | import lodashPlugin from 'esbuild-plugin-lodash'; 2 | 3 | import deepmerge from 'deepmerge'; 4 | import { cosmiconfig } from 'cosmiconfig'; 5 | 6 | import { IS_RELATIVE_AND_JS } from './regex.mjs'; 7 | 8 | /** 9 | * Gets the base ESBuild config for build/serve. 10 | */ 11 | export async function getEsbuildConfig() { 12 | const configFile = {}; 13 | 14 | // Try loading the config file and assign any options it has. 15 | const explorer = cosmiconfig('typehead'); 16 | try { 17 | const result = await explorer.search(); 18 | Object.assign(configFile, result.config); 19 | } catch (e) { 20 | console.debug('Skipping typehead config, using defaults.'); 21 | } 22 | 23 | const config = deepmerge(configFile, { 24 | sourcemap: true, 25 | bundle: true, 26 | plugins: [ 27 | // Automatically rewrite Lodash statements. 28 | lodashPlugin({ 29 | filter: IS_RELATIVE_AND_JS, 30 | }), 31 | ], 32 | }); 33 | 34 | // Only set platform if not set by the configuration. 35 | if (!config.platform) { 36 | config.platform = 'neutral'; 37 | } 38 | 39 | return config; 40 | } 41 | -------------------------------------------------------------------------------- /src/serve.mjs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import * as esbuild from 'esbuild'; 4 | 5 | import { Command } from 'commander'; 6 | import chalk from 'chalk'; 7 | 8 | import deepmerge from 'deepmerge'; 9 | import { resolve } from 'path'; 10 | 11 | import { getEsbuildConfig } from './util/getEsbuildConfig.mjs'; 12 | import { findEntryPoint } from './util/findEntryPoint.mjs'; 13 | 14 | // Setup command line arguments. 15 | const program = new Command() 16 | .name('typehead-serve') 17 | .option('-h --host