chore(api-client): bundler to tsup (#944)
This commit is contained in:
1
packages/api-client/dtos/index.ts
Normal file
1
packages/api-client/dtos/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './comment'
|
||||
@@ -1,9 +1,11 @@
|
||||
import { createClient } from './core'
|
||||
|
||||
export * from './controllers'
|
||||
export * from './models'
|
||||
export * from './dtos'
|
||||
|
||||
export { createClient, RequestError } from './core'
|
||||
export type { HTTPClient } from './core'
|
||||
export * from './models'
|
||||
export { camelcaseKeys as simpleCamelcaseKeys } from './utils/camelcase-keys'
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
|
||||
18
packages/api-client/mod-dts.mjs
Normal file
18
packages/api-client/mod-dts.mjs
Normal file
@@ -0,0 +1,18 @@
|
||||
import { readFileSync, writeFileSync } from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
const __dirname = new URL(import.meta.url).pathname.replace(/\/[^/]*$/, '')
|
||||
const PKG = JSON.parse(readFileSync(path.resolve(__dirname, './package.json')))
|
||||
|
||||
const dts = path.resolve(__dirname, './dist/index.d.ts')
|
||||
const content = readFileSync(dts, 'utf-8')
|
||||
|
||||
// replace declare module '../core/client'
|
||||
// with declare module '@mx-space/api-client'
|
||||
writeFileSync(
|
||||
dts,
|
||||
content.replace(
|
||||
/declare module '..\/core\/client'/g,
|
||||
'declare module ' + `'${PKG.name}'`,
|
||||
),
|
||||
)
|
||||
@@ -11,4 +11,5 @@ export * from './recently'
|
||||
export * from './say'
|
||||
export * from './setting'
|
||||
export * from './snippet'
|
||||
export * from './topic'
|
||||
export * from './user'
|
||||
|
||||
@@ -7,56 +7,27 @@
|
||||
"license": "MIT",
|
||||
"main": "dist/index.cjs",
|
||||
"module": "dist/index.js",
|
||||
"types": "types/index.d.ts",
|
||||
"unpkg": "dist/index.umd.min.js",
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
".": [
|
||||
"./types/index.d.ts"
|
||||
],
|
||||
"./adaptors/*": [
|
||||
"./types/adaptors/*.d.ts"
|
||||
]
|
||||
}
|
||||
},
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./types/index.d.ts",
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs"
|
||||
},
|
||||
"./dist/*": {
|
||||
"types": "./types/*.d.ts",
|
||||
"import": "./dist/*.js",
|
||||
"require": "./dist/*.cjs"
|
||||
},
|
||||
"./lib/*": {
|
||||
"types": "./lib/*.d.ts",
|
||||
"import": "./lib/*.js",
|
||||
"require": "./lib/*.js"
|
||||
},
|
||||
"./esm/*": {
|
||||
"types": "./esm/*.d.ts",
|
||||
"import": "./esm/*.js",
|
||||
"require": "./esm/*.js"
|
||||
},
|
||||
"./dist/adaptors/*": {
|
||||
"types": "./types/adaptors/*.d.ts",
|
||||
"import": "./dist/adaptors/*.js",
|
||||
"require": "./dist/adaptors/*.cjs"
|
||||
},
|
||||
"./types/*": {
|
||||
"types": "./types/*.d.ts"
|
||||
},
|
||||
"./package.json": "./package.json",
|
||||
"./adaptors/*": {
|
||||
"types": "./types/adaptors/*.d.ts",
|
||||
"import": {
|
||||
"type": "./types/adaptors/*.d.ts",
|
||||
"default": "./dist/adaptors/*.js"
|
||||
},
|
||||
"require": {
|
||||
"type": "./types/adaptors/*.d.ts",
|
||||
"default": "./dist/adaptors/*.cjs"
|
||||
}
|
||||
}
|
||||
@@ -88,38 +59,26 @@
|
||||
"pnpm": ">=6"
|
||||
},
|
||||
"scripts": {
|
||||
"prebuild": "rm -rf lib && rm -rf esm",
|
||||
"build": "concurrently \"tsc --build tsconfig.build.json\" \"tsc --build tsconfig.cjs.json\"",
|
||||
"postbuild": "concurrently \"tsc-alias -p tsconfig.build.json\" \"tsc-alias -p tsconfig.cjs.json\" \"npm run types\"",
|
||||
"types": "rm -rf types && tsc --build tsconfig.types.json && tsc-alias -p tsconfig.types.json",
|
||||
"package": "NODE_ENV=production concurrently \"npm run build\" \"rollup -c\"",
|
||||
"package": "rm -rf dist && tsup && node mod-dts.mjs",
|
||||
"prepackage": "rm -rf dist",
|
||||
"test": "vitest",
|
||||
"dev": "vitest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "22.0.2",
|
||||
"@rollup/plugin-node-resolve": "14.0.1",
|
||||
"@rollup/plugin-typescript": "8.5.0",
|
||||
"@types/cors": "2.8.13",
|
||||
"@types/express": "4.17.15",
|
||||
"@types/lodash": "4.14.186",
|
||||
"abort-controller": "3.0.0",
|
||||
"axios": "^1.2.2",
|
||||
"camelcase-keys": "*",
|
||||
"concurrently": "7.6.0",
|
||||
"cors": "2.8.5",
|
||||
"dts-bundle-generator": "7.0.0",
|
||||
"express": "4.18.2",
|
||||
"isomorphic-unfetch": "3.1.0",
|
||||
"ky": "0.33.2",
|
||||
"lodash": "4.17.21",
|
||||
"node-fetch": "3.2.10",
|
||||
"rollup": "3.10.0",
|
||||
"rollup-plugin-peer-deps-external": "2.2.4",
|
||||
"rollup-plugin-terser": "7.0.2",
|
||||
"tsc-alias": "1.8.2",
|
||||
"umi-request": "1.4.0",
|
||||
"globby": "^13.0.0"
|
||||
"tsup": "6.5.0",
|
||||
"umi-request": "1.4.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
// @ts-check
|
||||
import { execSync } from 'child_process'
|
||||
import { readFileSync } from 'fs'
|
||||
import { globbySync } from 'globby'
|
||||
import path, { resolve } from 'path'
|
||||
import peerDepsExternal from 'rollup-plugin-peer-deps-external'
|
||||
import { terser } from 'rollup-plugin-terser'
|
||||
|
||||
import commonjs from '@rollup/plugin-commonjs'
|
||||
import { nodeResolve } from '@rollup/plugin-node-resolve'
|
||||
import typescript from '@rollup/plugin-typescript'
|
||||
|
||||
const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'))
|
||||
const __dirname = new URL(import.meta.url).pathname.replace(/\/[^/]*$/, '')
|
||||
|
||||
const umdName = packageJson.name
|
||||
|
||||
const globals = {
|
||||
...packageJson.devDependencies,
|
||||
// @ts-ignore
|
||||
...(packageJson.dependencies || []),
|
||||
}
|
||||
|
||||
const dir = 'dist'
|
||||
|
||||
/**
|
||||
* @type {Partial<import('rollup').RollupOptions>}
|
||||
*/
|
||||
const baseRollupConfig = {
|
||||
plugins: [
|
||||
nodeResolve(),
|
||||
commonjs({ include: 'node_modules/**' }),
|
||||
typescript({ tsconfig: './tsconfig.json', declaration: false }),
|
||||
|
||||
// @ts-ignore
|
||||
peerDepsExternal(),
|
||||
],
|
||||
external: [...Object.keys(globals), 'lodash', 'lodash-es'],
|
||||
treeshake: true,
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {import('rollup').RollupOptions[]}
|
||||
*/
|
||||
const buildAdaptorConfig = () => {
|
||||
const paths = globbySync('./adaptors/*.ts')
|
||||
const filename = (path_) => path.parse(path_.split('/').pop()).name
|
||||
|
||||
return paths.map((path) => {
|
||||
const libName = filename(path)
|
||||
execSync(
|
||||
`npx dts-bundle-generator -o dist/adaptors/${libName}.d.ts ${resolve(
|
||||
__dirname,
|
||||
'adaptors/',
|
||||
)}/${libName}.ts` + ` --external-types ${libName}`,
|
||||
)
|
||||
|
||||
return {
|
||||
input: path,
|
||||
output: [
|
||||
{
|
||||
file: `${dir}/adaptors/${libName}.umd.js`,
|
||||
format: 'umd',
|
||||
sourcemap: true,
|
||||
name: umdName,
|
||||
},
|
||||
{
|
||||
file: `${dir}/adaptors/${libName}.umd.min.js`,
|
||||
format: 'umd',
|
||||
sourcemap: true,
|
||||
name: umdName,
|
||||
plugins: [terser()],
|
||||
},
|
||||
{
|
||||
file: `${dir}/adaptors/${libName}.cjs`,
|
||||
format: 'cjs',
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: `${dir}/adaptors/${libName}.min.cjs`,
|
||||
format: 'cjs',
|
||||
sourcemap: true,
|
||||
plugins: [terser()],
|
||||
},
|
||||
{
|
||||
file: `${dir}/adaptors/${libName}.js`,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: `${dir}/adaptors/${libName}.min.js`,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
plugins: [terser()],
|
||||
},
|
||||
],
|
||||
...baseRollupConfig,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {import('rollup').RollupOptions[]}
|
||||
*/
|
||||
const config = [
|
||||
{
|
||||
input: './index.ts',
|
||||
|
||||
output: [
|
||||
{
|
||||
file: `${dir}/index.umd.js`,
|
||||
format: 'umd',
|
||||
sourcemap: true,
|
||||
name: umdName,
|
||||
},
|
||||
{
|
||||
file: `${dir}/index.umd.min.js`,
|
||||
format: 'umd',
|
||||
sourcemap: true,
|
||||
name: umdName,
|
||||
plugins: [terser()],
|
||||
},
|
||||
{
|
||||
file: `${dir}/index.cjs`,
|
||||
format: 'cjs',
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: `${dir}/index.min.cjs`,
|
||||
format: 'cjs',
|
||||
sourcemap: true,
|
||||
plugins: [terser()],
|
||||
},
|
||||
{
|
||||
file: `${dir}/index.js`,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: `${dir}/index.min.js`,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
plugins: [terser()],
|
||||
},
|
||||
],
|
||||
...baseRollupConfig,
|
||||
},
|
||||
...buildAdaptorConfig(),
|
||||
]
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default config
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["__tests__/**/*.ts"]
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.build.json",
|
||||
"compilerOptions": {
|
||||
"module": "CommonJS",
|
||||
"outDir": "./lib"
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,11 @@
|
||||
"baseUrl": ".",
|
||||
"jsx": "react",
|
||||
"target": "ES2020",
|
||||
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
||||
"lib": [
|
||||
"ESNext",
|
||||
"DOM",
|
||||
"DOM.Iterable"
|
||||
],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"strict": true,
|
||||
@@ -15,8 +19,16 @@
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"sourceMap": true,
|
||||
"paths": {
|
||||
"~/*": ["*"]
|
||||
"~/*": [
|
||||
"*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"exclude": ["esm/*", "build/*", "node_modules/*", "lib/*"]
|
||||
}
|
||||
"exclude": [
|
||||
"esm/*",
|
||||
"build/*",
|
||||
"node_modules/*",
|
||||
"lib/*",
|
||||
"dist/**"
|
||||
]
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"rootDir": ".",
|
||||
"outDir": "types",
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"isolatedModules": false,
|
||||
"noEmit": false,
|
||||
"allowJs": false,
|
||||
"emitDeclarationOnly": true
|
||||
},
|
||||
"exclude": ["__tests__/**/*", "**/*.test.ts"]
|
||||
}
|
||||
18
packages/api-client/tsup.config.ts
Normal file
18
packages/api-client/tsup.config.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { readdirSync } from 'fs'
|
||||
import path from 'path'
|
||||
import { defineConfig } from 'tsup'
|
||||
|
||||
const __dirname = new URL(import.meta.url).pathname.replace(/\/[^/]*$/, '')
|
||||
|
||||
const adaptorNames = readdirSync(path.resolve(__dirname, './adaptors')).map(
|
||||
(i) => path.parse(i).name,
|
||||
)
|
||||
|
||||
export default defineConfig({
|
||||
clean: true,
|
||||
target: 'es2020',
|
||||
entry: ['index.ts', ...adaptorNames.map((name) => `adaptors/${name}.ts`)],
|
||||
external: adaptorNames,
|
||||
dts: true,
|
||||
format: ['cjs', 'esm', 'iife'],
|
||||
})
|
||||
Reference in New Issue
Block a user