2023-10-07 15:49:09 +08:00
|
|
|
import react from '@vitejs/plugin-react';
|
2023-09-03 10:59:33 +08:00
|
|
|
import fg from 'fast-glob';
|
2023-09-15 08:51:20 +08:00
|
|
|
import fs from 'fs-extra';
|
2023-10-07 15:49:09 +08:00
|
|
|
import path from 'path';
|
2023-09-03 10:59:33 +08:00
|
|
|
import { build as tsupBuild } from 'tsup';
|
2023-10-07 15:49:09 +08:00
|
|
|
import { build as viteBuild } from 'vite';
|
2023-09-03 10:59:33 +08:00
|
|
|
import { libInjectCss } from 'vite-plugin-lib-inject-css';
|
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
import { globExcludeFiles } from './constant';
|
2023-10-07 15:49:09 +08:00
|
|
|
import { PkgLog, UserConfig } from './utils';
|
2023-09-03 10:59:33 +08:00
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
export async function buildClient(cwd: string, userConfig: UserConfig, sourcemap: boolean = false, log: PkgLog) {
|
|
|
|
log('build client');
|
2023-09-03 10:59:33 +08:00
|
|
|
const cwdWin = cwd.replaceAll(/\\/g, '/');
|
|
|
|
const cwdUnix = cwd.replaceAll(/\//g, '\\');
|
2023-09-15 08:51:20 +08:00
|
|
|
const external = function (id: string) {
|
|
|
|
if (id.startsWith('.') || id.startsWith(cwdUnix) || id.startsWith(cwdWin)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2023-10-07 15:49:09 +08:00
|
|
|
};
|
2023-09-15 08:51:20 +08:00
|
|
|
await buildEsm(cwd, userConfig, sourcemap, external, log);
|
|
|
|
await buildLib(cwd, userConfig, sourcemap, external, log);
|
|
|
|
await buildLocale(cwd, userConfig, log);
|
|
|
|
}
|
|
|
|
|
|
|
|
type External = (id: string) => boolean;
|
2023-09-03 10:59:33 +08:00
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
export function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean, external: External, log: PkgLog) {
|
|
|
|
log('build client esm');
|
|
|
|
const entry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/');
|
|
|
|
const outDir = path.resolve(cwd, 'es');
|
2023-10-07 15:49:09 +08:00
|
|
|
return viteBuild(
|
|
|
|
userConfig.modifyViteConfig({
|
|
|
|
mode: 'production',
|
|
|
|
define: {
|
|
|
|
'process.env.NODE_ENV': JSON.stringify('production'),
|
|
|
|
'process.env.__TEST__': false,
|
2023-11-01 12:00:28 +08:00
|
|
|
'process.env.__E2E__': process.env.__E2E__ ? true : false,
|
2023-09-03 10:59:33 +08:00
|
|
|
},
|
2023-10-07 15:49:09 +08:00
|
|
|
build: {
|
|
|
|
minify: false,
|
|
|
|
outDir,
|
|
|
|
cssCodeSplit: true,
|
|
|
|
emptyOutDir: true,
|
|
|
|
sourcemap,
|
|
|
|
lib: {
|
|
|
|
entry,
|
|
|
|
formats: ['es'],
|
|
|
|
fileName: 'index',
|
|
|
|
},
|
|
|
|
target: ['es2015', 'edge88', 'firefox78', 'chrome87', 'safari14'],
|
|
|
|
rollupOptions: {
|
|
|
|
cache: true,
|
|
|
|
treeshake: true,
|
|
|
|
external,
|
|
|
|
},
|
2023-09-03 10:59:33 +08:00
|
|
|
},
|
2023-10-07 15:49:09 +08:00
|
|
|
plugins: [react(), libInjectCss()],
|
|
|
|
}),
|
|
|
|
);
|
2023-09-15 08:51:20 +08:00
|
|
|
}
|
|
|
|
|
2023-10-07 15:49:09 +08:00
|
|
|
export async function buildLib(
|
|
|
|
cwd: string,
|
|
|
|
userConfig: UserConfig,
|
|
|
|
sourcemap: boolean,
|
|
|
|
external: External,
|
|
|
|
log: PkgLog,
|
|
|
|
) {
|
2023-09-15 08:51:20 +08:00
|
|
|
log('build client lib');
|
|
|
|
const outDir = path.resolve(cwd, 'lib');
|
|
|
|
const esDir = path.resolve(cwd, 'es');
|
2023-10-07 15:49:09 +08:00
|
|
|
const entry = path.join(esDir, 'index.ts');
|
2023-09-15 08:51:20 +08:00
|
|
|
|
|
|
|
fs.removeSync(entry);
|
|
|
|
fs.linkSync(path.join(cwd, 'es/index.mjs'), entry);
|
|
|
|
|
2023-10-07 15:49:09 +08:00
|
|
|
await viteBuild(
|
|
|
|
userConfig.modifyViteConfig({
|
|
|
|
mode: 'production',
|
|
|
|
esbuild: {
|
|
|
|
format: 'cjs',
|
2023-09-15 08:51:20 +08:00
|
|
|
},
|
2023-10-07 15:49:09 +08:00
|
|
|
build: {
|
|
|
|
outDir,
|
|
|
|
minify: false,
|
|
|
|
sourcemap,
|
|
|
|
lib: {
|
|
|
|
entry: path.join(cwd, 'es/index.ts'),
|
|
|
|
formats: ['cjs'],
|
|
|
|
fileName: 'index',
|
|
|
|
},
|
|
|
|
rollupOptions: {
|
|
|
|
external,
|
|
|
|
},
|
2023-09-15 08:51:20 +08:00
|
|
|
},
|
2023-10-07 15:49:09 +08:00
|
|
|
}),
|
|
|
|
);
|
2023-09-15 08:51:20 +08:00
|
|
|
|
|
|
|
fs.removeSync(entry);
|
|
|
|
|
|
|
|
const css = fg.sync('*.css', { cwd: esDir, absolute: true });
|
|
|
|
css.forEach((file) => {
|
|
|
|
fs.copySync(file, path.join(outDir, path.basename(file)));
|
2023-09-03 10:59:33 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-15 08:51:20 +08:00
|
|
|
export function buildLocale(cwd: string, userConfig: UserConfig, log: PkgLog) {
|
|
|
|
log('build client locale');
|
|
|
|
|
2023-09-03 10:59:33 +08:00
|
|
|
const entry = fg.globSync(['src/locale/**', ...globExcludeFiles], { cwd, absolute: true });
|
|
|
|
const outDir = path.resolve(cwd, 'lib', 'locale');
|
2023-10-07 15:49:09 +08:00
|
|
|
return tsupBuild(
|
|
|
|
userConfig.modifyTsupConfig({
|
|
|
|
entry,
|
|
|
|
splitting: false,
|
|
|
|
clean: false,
|
|
|
|
bundle: false,
|
|
|
|
silent: true,
|
|
|
|
treeshake: false,
|
|
|
|
target: 'node16',
|
|
|
|
keepNames: true,
|
|
|
|
outDir,
|
|
|
|
format: 'cjs',
|
|
|
|
skipNodeModulesBundle: true,
|
|
|
|
}),
|
|
|
|
);
|
2023-09-15 08:51:20 +08:00
|
|
|
}
|