From 6ccccd791bbed4d272c7448f90ff780408a1e803 Mon Sep 17 00:00:00 2001 From: hemengke <23536175@qq.com> Date: Tue, 7 May 2024 10:04:32 +0800 Subject: [PATCH] chore: refactor build --- .tazerc.json | 4 + package.json | 2 +- packages/istanbul-widget/package.json | 14 ++- packages/istanbul-widget/shims.d.ts | 3 - packages/istanbul-widget/src/types.d.ts | 2 - packages/istanbul-widget/tsup.config.ts | 122 ++---------------------- pnpm-lock.yaml | 98 ++++--------------- 7 files changed, 39 insertions(+), 206 deletions(-) create mode 100644 .tazerc.json delete mode 100644 packages/istanbul-widget/shims.d.ts diff --git a/.tazerc.json b/.tazerc.json new file mode 100644 index 0000000..85fbabc --- /dev/null +++ b/.tazerc.json @@ -0,0 +1,4 @@ +{ + "write": true, + "interactive": true +} diff --git a/package.json b/package.json index 2fd3213..42672df 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "lint": "turbo lint --filter=\"./packages/*\"", "prepare": "simple-git-hooks", "commitlint": "commitlint -e", - "up": "taze -I -w -r", + "up": "taze -I -w", "changeset": "changeset", "version": "changeset version && changeset tag", "report": "nyc report --excludeAfterRemap=false" diff --git a/packages/istanbul-widget/package.json b/packages/istanbul-widget/package.json index 1db5103..44134c5 100644 --- a/packages/istanbul-widget/package.json +++ b/packages/istanbul-widget/package.json @@ -33,7 +33,9 @@ "./*": "./*" }, "sideEffects": [ - "**/*.css" + "**/*.css", + "./dist/istanbul-widget.lib.js", + "./dist/istanbul-widget.min.js" ], "scripts": { "dev": "tsup --watch", @@ -68,24 +70,20 @@ "tailwindcss-animate": "^1.0.7" }, "devDependencies": { - "@babel/preset-env": "^7.24.4", "@iconify/tailwind": "^0.1.4", - "@rollup/plugin-babel": "^6.0.4", "@swc/core": "^1.4.17", "@types/node": "^20.11.30", "@types/react": "^18.2.73", "@types/react-dom": "^18.2.23", "@vitejs/plugin-react": "^4.2.1", "autoprefixer": "^10.4.19", - "bundle-require": "^4.0.3", - "fast-glob": "^3.3.2", "npm-run-all": "^4.1.5", "postcss": "^8.4.38", "rollup-plugin-visualizer": "^5.12.0", - "strip-dirs": "^3.0.0", "tailwindcss": "3.2.7", "tailwindcss-rem-to-px": "^0.1.0", - "tsc-alias": "^1.8.8", - "vite": "^5.0.8" + "tsup-plugin-bundleless": "^0.0.1", + "tsup-plugin-css-legacy": "^0.0.1", + "vite": "^5.2.10" } } diff --git a/packages/istanbul-widget/shims.d.ts b/packages/istanbul-widget/shims.d.ts deleted file mode 100644 index 268003a..0000000 --- a/packages/istanbul-widget/shims.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare module 'strip-dirs' { - export default (path: string, length: number) => string -} diff --git a/packages/istanbul-widget/src/types.d.ts b/packages/istanbul-widget/src/types.d.ts index 6f78d38..16666dc 100644 --- a/packages/istanbul-widget/src/types.d.ts +++ b/packages/istanbul-widget/src/types.d.ts @@ -1,5 +1,3 @@ -/// - declare global { declare var __VERSION__: string diff --git a/packages/istanbul-widget/tsup.config.ts b/packages/istanbul-widget/tsup.config.ts index a02711b..cef62d7 100644 --- a/packages/istanbul-widget/tsup.config.ts +++ b/packages/istanbul-widget/tsup.config.ts @@ -1,71 +1,8 @@ -import { match } from 'bundle-require' -import glob from 'fast-glob' -import fs from 'node:fs' -import path from 'node:path' -import stripDirs from 'strip-dirs' -import { replaceTscAliasPaths } from 'tsc-alias' import { type Options, defineConfig } from 'tsup' +import { bundless } from 'tsup-plugin-bundleless' +import { cssLegacy } from 'tsup-plugin-css-legacy' import pkg from './package.json' -function rmExt(filePath: string) { - return filePath.split(path.extname(filePath))[0] -} - -export function getEntry(entryGlob = 'src/index.ts{,x}') { - const entries = glob.sync(entryGlob) - const entry: Record = {} - entries.forEach((e) => { - entry[rmExt(stripDirs(e, 1))] = e - }) - - return entry -} - -const fileSuffixPlugin = ( - format: 'cjs' | 'esm', - tsupOptions?: Options, -): Exclude[number] => ({ - name: 'add-file-suffix', - setup(build) { - build.onResolve({ filter: /.*/ }, (args) => { - if (args.kind === 'entry-point') return - let importeePath = args.path - - if (importeePath.endsWith('.css')) { - return { external: true } - } - - const { external, noExternal } = tsupOptions ?? {} - if (match(importeePath, noExternal)) { - return - } - if (match(importeePath, external)) { - return { external: true } - } - if (match(args.importer, noExternal)) { - return - } - if (match(args.importer, external)) { - return { external: true } - } - if (importeePath[0] !== '.' && !path.isAbsolute(importeePath)) { - return { external: true } - } - const suffix = format === 'cjs' ? '.cjs' : '.js' - if (!path.extname(importeePath) && !importeePath.endsWith('.js')) { - const filePath = path.join(args.resolveDir, importeePath) - - if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { - importeePath += `/index${suffix}` - } else { - importeePath += suffix - } - return { path: importeePath, external: true } - } - }) - }, -}) - const common = (option: Options): Options => ({ clean: true, outExtension: () => ({ js: '.js' }), @@ -73,33 +10,9 @@ const common = (option: Options): Options => ({ minify: !option.watch, target: 'es3', define: { - '__VERSION__': JSON.stringify(pkg.version), - 'process.env.NODE_ENV': JSON.stringify(option.watch ? 'development' : 'production'), - }, - esbuildOptions(options, ...args) { - option.esbuildOptions?.(options, ...args) - - // css 兼容处理 - // https://esbuild.github.io/api/#supported - const cssUnSupported = [ - 'color-functions', - 'gradient-double-position', - 'gradient-interpolation', - 'gradient-midpoints', - 'hwb', - 'hex-rgba', - 'inline-style', - 'inset-property', - 'is-pseudo-class', - 'modern-rgb-hsl', - 'nesting', - 'rebecca-purple', - ] - options.supported ??= {} - cssUnSupported.forEach((css) => { - options.supported![css] = false - }) + __VERSION__: JSON.stringify(pkg.version), }, + plugins: [...(option.plugins || []), cssLegacy()], }) const lib = (option: Options): Options => ({ @@ -126,37 +39,20 @@ const iife = (_option: Options): Options => ({ 'istanbul-widget.min': 'src/istanbul-widget.iife.ts', }, esbuildOptions(options) { - if (!options.logOverride) { - options.logOverride = {} - } - options.logOverride!['empty-import-meta'] = 'silent' + options.logOverride ??= {} + options.logOverride['empty-import-meta'] = 'silent' }, dts: false, }) -const esOutDir = 'dist/es' - const es = (option: Options): Options => ({ entry: ['src/**/*.{ts,tsx,css}'], - dts: option.watch - ? false - : { - entry: getEntry('src/**/*.{ts,tsx}'), - }, + dts: !option.watch, format: 'esm', - outDir: esOutDir, - bundle: false, - outExtension: () => ({ js: '.js' }), - esbuildPlugins: [fileSuffixPlugin('esm')], + outDir: 'dist/es', splitting: false, minify: false, - skipNodeModulesBundle: true, - async onSuccess() { - replaceTscAliasPaths({ - configFile: './tsconfig.json', - outDir: esOutDir, - }) - }, + plugins: [bundless()], }) export default defineConfig((option) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c1613a3..674b4d0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -198,15 +198,9 @@ importers: specifier: ^1.0.7 version: 1.0.7(tailwindcss@3.2.7) devDependencies: - '@babel/preset-env': - specifier: ^7.24.4 - version: 7.24.4(@babel/core@7.24.4) '@iconify/tailwind': specifier: ^0.1.4 version: 0.1.4 - '@rollup/plugin-babel': - specifier: ^6.0.4 - version: 6.0.4(@babel/core@7.24.4) '@swc/core': specifier: ^1.4.17 version: 1.4.17 @@ -221,19 +215,10 @@ importers: version: 18.2.24 '@vitejs/plugin-react': specifier: ^4.2.1 - version: 4.2.1(vite@5.2.8) + version: 4.2.1(vite@5.2.10) autoprefixer: specifier: ^10.4.19 version: 10.4.19(postcss@8.4.38) - bundle-require: - specifier: ^4.0.3 - version: 4.0.3(esbuild@0.20.2) - fast-glob: - specifier: ^3.3.2 - version: 3.3.2 - istanbul-widget: - specifier: 'link: ./' - version: 'link: .' npm-run-all: specifier: ^4.1.5 version: 4.1.5 @@ -243,21 +228,21 @@ importers: rollup-plugin-visualizer: specifier: ^5.12.0 version: 5.12.0 - strip-dirs: - specifier: ^3.0.0 - version: 3.0.0 tailwindcss: specifier: 3.2.7 version: 3.2.7(postcss@8.4.38) tailwindcss-rem-to-px: specifier: ^0.1.0 version: 0.1.0 - tsc-alias: - specifier: ^1.8.8 - version: 1.8.8 + tsup-plugin-bundleless: + specifier: ^0.0.1 + version: 0.0.1 + tsup-plugin-css-legacy: + specifier: ^0.0.1 + version: 0.0.1 vite: - specifier: ^5.0.8 - version: 5.2.8(@types/node@20.12.5) + specifier: ^5.2.10 + version: 5.2.10(@types/node@20.12.5) packages/vite-plugin-istanbul-widget: dependencies: @@ -3655,38 +3640,6 @@ packages: '@babel/runtime': 7.24.4 dev: false - /@rollup/plugin-babel@6.0.4(@babel/core@7.24.4): - resolution: {integrity: sha512-YF7Y52kFdFT/xVSuVdjkV5ZdX/3YtmX0QulG+x0taQOtJdHYzVU61aSSkAgVJ7NOv6qPkIYiJSgSWWN/DM5sGw==} - engines: {node: '>=14.0.0'} - peerDependencies: - '@babel/core': ^7.0.0 - '@types/babel__core': ^7.1.9 - rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 - peerDependenciesMeta: - '@types/babel__core': - optional: true - rollup: - optional: true - dependencies: - '@babel/core': 7.24.4 - '@babel/helper-module-imports': 7.24.3 - '@rollup/pluginutils': 5.1.0 - dev: true - - /@rollup/pluginutils@5.1.0: - resolution: {integrity: sha512-XTIWOPPcpvyKI6L1NHo0lFlCyznUEyPmPY1mc3KpPVDYulHSTvyeLNVW00QTLIAFNhR3kYnJTQHeGqU4M3n09g==} - engines: {node: '>=14.0.0'} - peerDependencies: - rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 - peerDependenciesMeta: - rollup: - optional: true - dependencies: - '@types/estree': 1.0.5 - estree-walker: 2.0.2 - picomatch: 2.3.1 - dev: true - /@rollup/rollup-android-arm-eabi@4.14.1: resolution: {integrity: sha512-fH8/o8nSUek8ceQnT7K4EQbSiV7jgkHq81m9lWZFIXjJ7lJzpWXbQFpT/Zh6OZYnpFykvzC3fbEvEAFZu03dPA==} cpu: [arm] @@ -5374,16 +5327,6 @@ packages: load-tsconfig: 0.2.5 dev: true - /bundle-require@4.0.3(esbuild@0.20.2): - resolution: {integrity: sha512-2iscZ3fcthP2vka4Y7j277YJevwmsby/FpFDwjgw34Nl7dtCpt7zz/4TexmHMzY6KZEih7En9ImlbbgUNNQGtA==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} - peerDependencies: - esbuild: '>=0.17' - dependencies: - esbuild: 0.20.2 - load-tsconfig: 0.2.5 - dev: true - /bytes@3.1.2: resolution: {integrity: sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==} engines: {node: '>= 0.8'} @@ -8131,12 +8074,6 @@ packages: fast-loops: 1.1.3 dev: false - /inspect-with-kind@1.0.5: - resolution: {integrity: sha512-MAQUJuIo7Xqk8EVNP+6d3CKq9c80hi4tjIbIAT6lmGW9W6WzlHiu9PS8uSuUYU+Do+j1baiFp3H25XEVxDIG2g==} - dependencies: - kind-of: 6.0.3 - dev: true - /internal-slot@1.0.7: resolution: {integrity: sha512-NGnrKwXzSms2qUUih/ILZ5JBqNTSa1+ZmP6flaIp6KmSElgE9qdndzS3cqjrDovwFdmwsGsLdeFgB6suw+1e9g==} engines: {node: '>= 0.4'} @@ -11902,13 +11839,6 @@ packages: resolution: {integrity: sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==} engines: {node: '>=8'} - /strip-dirs@3.0.0: - resolution: {integrity: sha512-I0sdgcFTfKQlUPZyAqPJmSG3HLO9rWDFnxonnIbskYNM3DwFOeTNB5KzVq3dA1GdRAc/25b5Y7UO2TQfKWw4aQ==} - dependencies: - inspect-with-kind: 1.0.5 - is-plain-obj: 1.1.0 - dev: true - /strip-final-newline@2.0.0: resolution: {integrity: sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==} engines: {node: '>=6'} @@ -12374,6 +12304,16 @@ packages: /tslib@2.6.2: resolution: {integrity: sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==} + /tsup-plugin-bundleless@0.0.1: + resolution: {integrity: sha512-McuLB63jZkkfJDpyChe+3UaXYVjDSFRepcwHr/fHhaOR34J8pysIbierYyGAq2r5uuCCdz3mfM6wjOunMCWT/g==} + dependencies: + tsc-alias: 1.8.8 + dev: true + + /tsup-plugin-css-legacy@0.0.1: + resolution: {integrity: sha512-pZNSotQJ4+v+prBYtNVqnhoUuDc3nqlS3wlm29584gfRGR9LR/je/ugp+pTcClEr03YFXNTO6l47czHhzBe4Tg==} + dev: true + /tsup@8.0.2(postcss@8.4.38)(typescript@5.4.5): resolution: {integrity: sha512-NY8xtQXdH7hDUAZwcQdY/Vzlw9johQsaqf7iwZ6g1DOUlFYQ5/AtVAjTvihhEyeRlGo4dLRVHtrRaL35M1daqQ==} engines: {node: '>=18'}