element-plus/build/utils/pkg.ts
三咲智子 4e99d0b5ba
build!: simplify build & support esm import (#4018)
* build!: simplify build & support native esm import

* build: refactor build

* refactor: reorganize files

* refactor: refactor build

* build: improve perf

* fix: scripts

* build: add rollup-plugin-filesize

* chore: scripts ignore no-console

* build: disable tree-shaking

* build: improve code

* build: add sourcemap

* build: add banner

* refactor: remove annotation

* build!: improve esm exports (#3871)

* build: improve esm import

* refactor: change mjs for esm version

* chore: improve exports map

* fix: add sideEffects

* refactor: improve alias

* build: upgrade dependencies
2021-10-25 17:07:48 +08:00

60 lines
1.9 KiB
TypeScript

import findWorkspacePackages from '@pnpm/find-workspace-packages'
import { buildConfig } from '../build-info'
import { EP_PREFIX } from './constants'
import { pkgRoot, projRoot } from './paths'
import type { Module } from '../build-info'
import type { ProjectManifest } from '@pnpm/types'
export const getWorkspacePackages = () => findWorkspacePackages(projRoot)
export const getWorkspaceNames = async (dir = projRoot) => {
const pkgs = await findWorkspacePackages(projRoot)
return pkgs
.filter((pkg) => pkg.dir.startsWith(dir))
.map((pkg) => pkg.manifest.name)
.filter((name): name is string => !!name)
}
export const getPackageManifest = (pkgPath: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(pkgPath) as ProjectManifest
}
export const getPackageDependencies = (pkgPath: string): string[] => {
const manifest = getPackageManifest(pkgPath)
const { dependencies } = manifest
return Object.keys(dependencies ?? {})
}
export const pathRewriter = (module: Module) => {
const config = buildConfig[module]
return (id: string) => {
id = id.replaceAll(`${EP_PREFIX}/theme-chalk`, 'element-plus/theme-chalk')
// TODO: handle @element-plus/icons
id = id.replaceAll(`${EP_PREFIX}/`, `${config.bundle.path}/`)
return id
}
}
export const excludeFiles = (files: string[]) => {
const excludes = ['node_modules', 'test', 'mock', 'gulpfile', 'dist']
return files.filter(
(path) => !excludes.some((exclude) => path.includes(exclude))
)
}
/**
* get package list (theme-chalk excluded)
*/
export const getDistPackages = async () =>
(await getWorkspacePackages())
.map((pkg) => ({ name: pkg.manifest.name, dir: pkg.dir }))
.filter(
(pkg): pkg is { name: string; dir: string } =>
!!pkg.name &&
!!pkg.dir &&
pkg.name.startsWith(EP_PREFIX) &&
pkg.dir.startsWith(pkgRoot) &&
pkg.name !== `${EP_PREFIX}/theme-chalk`
)