Skip to content

Commit

Permalink
chore: refactor build
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed May 7, 2024
1 parent 8c91b66 commit 6ccccd7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 206 deletions.
4 changes: 4 additions & 0 deletions .tazerc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"write": true,
"interactive": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions packages/istanbul-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"./*": "./*"
},
"sideEffects": [
"**/*.css"
"**/*.css",
"./dist/istanbul-widget.lib.js",
"./dist/istanbul-widget.min.js"
],
"scripts": {
"dev": "tsup --watch",
Expand Down Expand Up @@ -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"
}
}
3 changes: 0 additions & 3 deletions packages/istanbul-widget/shims.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/istanbul-widget/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="vite/client" />

declare global {
declare var __VERSION__: string

Expand Down
122 changes: 9 additions & 113 deletions packages/istanbul-widget/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,18 @@
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<string, string> = {}
entries.forEach((e) => {
entry[rmExt(stripDirs(e, 1))] = e
})

return entry
}

const fileSuffixPlugin = (
format: 'cjs' | 'esm',
tsupOptions?: Options,
): Exclude<Options['esbuildPlugins'], undefined>[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' }),
outDir: 'dist',
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 => ({
Expand All @@ -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) => {
Expand Down
98 changes: 19 additions & 79 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ccccd7

Please sign in to comment.