element-plus/build/component.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

import fs from 'fs'
import path from 'path'
2021-09-26 01:29:07 +08:00
import { series, parallel } from 'gulp'
import { rollup } from 'rollup'
import vue from 'rollup-plugin-vue'
import css from 'rollup-plugin-css-only'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import esbuild from 'rollup-plugin-esbuild'
import { sync as globSync } from 'fast-glob'
2021-09-26 01:29:07 +08:00
import filesize from 'rollup-plugin-filesize'
import { compRoot, buildOutput } from './utils/paths'
import {
generateExternal,
rollupPathRewriter,
writeBundles,
} from './utils/rollup'
2021-09-26 01:29:07 +08:00
import { run } from './utils/process'
import { withTaskName } from './utils/gulp'
2021-09-26 01:29:07 +08:00
import { genComponentTypes } from './component-types'
import { buildConfig } from './info'
import reporter from './size-reporter'
2021-09-26 01:29:07 +08:00
import type { OutputOptions } from 'rollup'
import type { Module, BuildConfigEntries } from './info'
const plugins = [
css(),
vue({
target: 'browser',
// css: false,
}),
nodeResolve(),
commonjs(),
esbuild(),
]
async function getComponents() {
2021-09-26 01:29:07 +08:00
const files = globSync('*', {
cwd: compRoot,
onlyDirectories: true,
})
return files.map((file) => ({
path: path.resolve(compRoot, file),
name: file,
}))
}
export async function buildEachComponent() {
const componentPaths = await getComponents()
2021-09-26 01:29:07 +08:00
const external = await generateExternal({ full: false })
const pathRewriter = await rollupPathRewriter()
const builds = componentPaths.map(
async ({ path: p, name: componentName }) => {
2021-09-26 01:29:07 +08:00
const entry = path.resolve(p, 'index.ts')
if (!fs.existsSync(entry)) return
const rollupConfig = {
input: entry,
plugins,
external,
}
const opts = (Object.entries(buildConfig) as BuildConfigEntries).map(
2021-09-26 01:29:07 +08:00
([module, config]): OutputOptions => ({
format: config.format,
file: path.resolve(
config.output.path,
'components',
componentName,
'index.js'
),
exports: module === 'cjs' ? 'named' : undefined,
paths: pathRewriter(module),
2021-09-26 01:29:07 +08:00
plugins: [filesize({ reporter })],
})
)
const bundle = await rollup(rollupConfig)
await writeBundles(bundle, opts)
}
)
2021-09-26 01:29:07 +08:00
await Promise.all(builds)
}
export async function buildComponentEntry() {
const entry = path.resolve(compRoot, 'index.ts')
const config = {
input: entry,
plugins,
external: () => true,
}
2021-09-26 01:29:07 +08:00
const opts = Object.values(buildConfig).map(
(config): OutputOptions => ({
format: config.format,
file: path.resolve(config.output.path, 'components/index.js'),
plugins: [filesize({ reporter })],
})
2021-09-26 01:29:07 +08:00
)
2021-09-26 01:29:07 +08:00
const bundle = await rollup(config)
await writeBundles(bundle, opts)
}
2021-09-26 01:29:07 +08:00
function copyTypes() {
const src = `${buildOutput}/types/components/`
const copy = (module: Module) =>
withTaskName(`copyTypes:${module}`, () =>
run(`rsync -a ${src} ${buildConfig[module].output.path}/components/`)
)
return parallel(copy('esm'), copy('cjs'))
}
export const buildComponent = series(
2021-09-26 01:29:07 +08:00
parallel(genComponentTypes, buildEachComponent, buildComponentEntry),
copyTypes()
)
export { genComponentTypes }