refactor: build
This commit is contained in:
parent
4e48537b39
commit
7c6020e11b
|
@ -36,7 +36,7 @@
|
||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"lint": "eslint --ext .ts src/**",
|
"lint": "eslint --ext .ts src/**",
|
||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
"build": "npm run lint && node scripts/build.js"
|
"build": "pnpm run lint && rollup -c rollup.config.ts --configPlugin typescript"
|
||||||
},
|
},
|
||||||
"simple-git-hooks": {
|
"simple-git-hooks": {
|
||||||
"pre-commit": "npx lint-staged",
|
"pre-commit": "npx lint-staged",
|
||||||
|
@ -70,7 +70,6 @@
|
||||||
"eslint": "^8.55.0",
|
"eslint": "^8.55.0",
|
||||||
"eslint-config-prettier": "^9.1.0",
|
"eslint-config-prettier": "^9.1.0",
|
||||||
"eslint-plugin-prettier": "^5.0.1",
|
"eslint-plugin-prettier": "^5.0.1",
|
||||||
"fs-extra": "^11.2.0",
|
|
||||||
"lint-staged": "^15.2.0",
|
"lint-staged": "^15.2.0",
|
||||||
"prettier": "^3.1.0",
|
"prettier": "^3.1.0",
|
||||||
"rollup": "^4.6.1",
|
"rollup": "^4.6.1",
|
||||||
|
|
|
@ -52,9 +52,6 @@ devDependencies:
|
||||||
eslint-plugin-prettier:
|
eslint-plugin-prettier:
|
||||||
specifier: ^5.0.1
|
specifier: ^5.0.1
|
||||||
version: 5.0.1(eslint-config-prettier@9.1.0)(eslint@8.55.0)(prettier@3.1.0)
|
version: 5.0.1(eslint-config-prettier@9.1.0)(eslint@8.55.0)(prettier@3.1.0)
|
||||||
fs-extra:
|
|
||||||
specifier: ^11.2.0
|
|
||||||
version: 11.2.0
|
|
||||||
lint-staged:
|
lint-staged:
|
||||||
specifier: ^15.2.0
|
specifier: ^15.2.0
|
||||||
version: 15.2.0
|
version: 15.2.0
|
||||||
|
@ -1579,15 +1576,6 @@ packages:
|
||||||
resolution: {integrity: sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==}
|
resolution: {integrity: sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/fs-extra@11.2.0:
|
|
||||||
resolution: {integrity: sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==}
|
|
||||||
engines: {node: '>=14.14'}
|
|
||||||
dependencies:
|
|
||||||
graceful-fs: 4.2.11
|
|
||||||
jsonfile: 6.1.0
|
|
||||||
universalify: 2.0.1
|
|
||||||
dev: true
|
|
||||||
|
|
||||||
/fs.realpath@1.0.0:
|
/fs.realpath@1.0.0:
|
||||||
resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==}
|
resolution: {integrity: sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==}
|
||||||
dev: true
|
dev: true
|
||||||
|
@ -1673,10 +1661,6 @@ packages:
|
||||||
slash: 3.0.0
|
slash: 3.0.0
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/graceful-fs@4.2.11:
|
|
||||||
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
|
|
||||||
dev: true
|
|
||||||
|
|
||||||
/graphemer@1.4.0:
|
/graphemer@1.4.0:
|
||||||
resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==}
|
resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==}
|
||||||
dev: true
|
dev: true
|
||||||
|
@ -1867,14 +1851,6 @@ packages:
|
||||||
hasBin: true
|
hasBin: true
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
/jsonfile@6.1.0:
|
|
||||||
resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==}
|
|
||||||
dependencies:
|
|
||||||
universalify: 2.0.1
|
|
||||||
optionalDependencies:
|
|
||||||
graceful-fs: 4.2.11
|
|
||||||
dev: true
|
|
||||||
|
|
||||||
/keyv@4.5.4:
|
/keyv@4.5.4:
|
||||||
resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==}
|
resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==}
|
||||||
dependencies:
|
dependencies:
|
||||||
|
@ -2453,11 +2429,6 @@ packages:
|
||||||
resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
|
resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/universalify@2.0.1:
|
|
||||||
resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==}
|
|
||||||
engines: {node: '>= 10.0.0'}
|
|
||||||
dev: true
|
|
||||||
|
|
||||||
/untildify@4.0.0:
|
/untildify@4.0.0:
|
||||||
resolution: {integrity: sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==}
|
resolution: {integrity: sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
|
|
54
rollup.config.ts
Normal file
54
rollup.config.ts
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
import { createRequire } from 'node:module'
|
||||||
|
import fs from 'node:fs/promises'
|
||||||
|
import { type Plugin, defineConfig } from 'rollup'
|
||||||
|
import ts from '@rollup/plugin-typescript'
|
||||||
|
import resolve from '@rollup/plugin-node-resolve'
|
||||||
|
import dts from 'rollup-plugin-dts'
|
||||||
|
|
||||||
|
const require = createRequire(import.meta.url)
|
||||||
|
const pkg = require('./package.json')
|
||||||
|
|
||||||
|
const external = ['esbuild', ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]
|
||||||
|
|
||||||
|
function clean(when: 'buildStart' | 'buildEnd', target: string): Plugin {
|
||||||
|
const _clean = async (target): Promise<void> => {
|
||||||
|
await fs.rm(target, { recursive: true, force: true }).catch(() => {})
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name: 'clean',
|
||||||
|
buildStart: async (): Promise<void> => {
|
||||||
|
if (when !== 'buildStart') return
|
||||||
|
await _clean(target)
|
||||||
|
},
|
||||||
|
buildEnd: async (): Promise<void> => {
|
||||||
|
if (when !== 'buildEnd') return
|
||||||
|
await _clean(target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineConfig([
|
||||||
|
{
|
||||||
|
input: ['src/index.ts', 'src/cli.ts'],
|
||||||
|
output: {
|
||||||
|
dir: 'dist',
|
||||||
|
entryFileNames: '[name].js',
|
||||||
|
chunkFileNames: 'chunks/lib-[hash].js',
|
||||||
|
format: 'cjs'
|
||||||
|
},
|
||||||
|
external,
|
||||||
|
plugins: [
|
||||||
|
clean('buildStart', 'dist'),
|
||||||
|
ts({ compilerOptions: { rootDir: 'src', declaration: true, declarationDir: 'dist/types' } }),
|
||||||
|
resolve()
|
||||||
|
],
|
||||||
|
treeshake: {
|
||||||
|
moduleSideEffects: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: 'dist/types/index.d.ts',
|
||||||
|
output: [{ file: pkg.types, format: 'es' }],
|
||||||
|
plugins: [dts(), clean('buildEnd', 'dist/types')]
|
||||||
|
}
|
||||||
|
])
|
|
@ -1,62 +0,0 @@
|
||||||
const path = require('path')
|
|
||||||
const colors = require('picocolors')
|
|
||||||
const fs = require('fs-extra')
|
|
||||||
const rollup = require('rollup')
|
|
||||||
const typescript = require('@rollup/plugin-typescript')
|
|
||||||
const { nodeResolve } = require('@rollup/plugin-node-resolve')
|
|
||||||
const { dts } = require('rollup-plugin-dts')
|
|
||||||
|
|
||||||
;(async () => {
|
|
||||||
const dist = path.resolve(__dirname, '../dist')
|
|
||||||
|
|
||||||
await fs.remove(dist)
|
|
||||||
|
|
||||||
console.log()
|
|
||||||
console.log(colors.bold(colors.yellow(`Rolling up ts code...`)))
|
|
||||||
|
|
||||||
const pkg = require('../package.json')
|
|
||||||
|
|
||||||
const external = ['esbuild', ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]
|
|
||||||
|
|
||||||
const bundle = await rollup.rollup({
|
|
||||||
input: {
|
|
||||||
index: path.resolve(__dirname, '../src/index.ts'),
|
|
||||||
cli: path.resolve(__dirname, '../src/cli.ts')
|
|
||||||
},
|
|
||||||
external,
|
|
||||||
plugins: [
|
|
||||||
typescript({
|
|
||||||
tsconfig: path.resolve(__dirname, '../tsconfig.json'),
|
|
||||||
compilerOptions: { rootDir: 'src' }
|
|
||||||
}),
|
|
||||||
nodeResolve()
|
|
||||||
],
|
|
||||||
treeshake: {
|
|
||||||
moduleSideEffects: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
await bundle.write({
|
|
||||||
dir: dist,
|
|
||||||
entryFileNames: '[name].js',
|
|
||||||
chunkFileNames: 'chunks/lib-[hash].js',
|
|
||||||
format: 'cjs'
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log(colors.bold(colors.yellow(`Rolling up type definitions...`)))
|
|
||||||
|
|
||||||
if (pkg.types) {
|
|
||||||
const bundle = await rollup.rollup({
|
|
||||||
input: path.resolve(__dirname, '../dist/types/index.d.ts'),
|
|
||||||
external,
|
|
||||||
plugins: [dts()]
|
|
||||||
})
|
|
||||||
await bundle.write({
|
|
||||||
file: pkg.types,
|
|
||||||
format: 'es'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.remove(path.resolve(dist, 'types'))
|
|
||||||
|
|
||||||
console.log(colors.green(`Build ${pkg.name}@${pkg.version} successfully`))
|
|
||||||
})()
|
|
|
@ -14,10 +14,7 @@
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
"noUnusedParameters": true,
|
"noUnusedParameters": true,
|
||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"noImplicitReturns": true,
|
"noImplicitReturns": true
|
||||||
"declaration": true,
|
|
||||||
"declarationDir": "dist/types",
|
|
||||||
"outDir": "dist"
|
|
||||||
},
|
},
|
||||||
"include": ["src"]
|
"include": ["src", "rollup.config.ts"]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue