feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { resolve } from 'path';
import { mergeConfig, type UserConfig } from 'vitest/config';
import { webPreset } from './preset-web';
import { nodePreset } from './preset-node';
import { defaultVitestConfig } from './preset-default';
export interface VitestConfig extends UserConfig {
/**
* A string representing the project root directory.
*/
dirname: string;
/**
* A string representing the preset configuration style, which can be one of 'default', 'node', or 'web'.
*/
preset: 'default' | 'node' | 'web';
}
const calBasePreset = (preset: string) => {
switch (preset) {
case 'node':
return nodePreset;
case 'web':
return webPreset;
default:
return defaultVitestConfig;
}
};
export interface OtherConfig {
/**
* 用于修复semi的package.json导出的配置问题
*/
fixSemi: boolean;
}
export const defineConfig = (
config: VitestConfig,
otherConfig?: OtherConfig,
): UserConfig => {
const { dirname, preset, ...userVitestConfig } = config;
if (typeof dirname !== 'string') {
throw new Error('define VitestConfig need a dirname.');
}
const baseConfig = calBasePreset(preset);
if (otherConfig?.fixSemi) {
const alias = [
{
find: /^@douyinfe\/semi-ui$/,
replacement: '@douyinfe/semi-ui/lib/es',
},
{
find: /^@douyinfe\/semi-foundation$/,
replacement: '@douyinfe/semi-foundation/lib/es',
},
{
find: 'lottie-web',
replacement: resolve(__dirname, './tsc-only.ts'),
},
];
if (Array.isArray(userVitestConfig.test?.alias)) {
alias.push(...userVitestConfig.test.alias);
} else if (typeof userVitestConfig.test?.alias === 'object') {
alias.push(
...Object.entries(userVitestConfig.test.alias).map(([key, value]) => ({
find: key,
replacement: value,
})),
);
}
userVitestConfig.test = {
...userVitestConfig.test,
alias,
};
}
return mergeConfig(baseConfig, userVitestConfig);
};

View File

@@ -0,0 +1,6 @@
require('sucrase/register/ts');
process.env.VITE_CJS_IGNORE_WARNING = true;
const { defineConfig } = require('./define-config');
module.exports = { defineConfig };

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { coverageConfigDefaults, type UserConfig } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';
export const defaultVitestConfig: UserConfig = {
plugins: [tsconfigPaths()],
resolve: {
// 优先识别 main如果没有配置 main则识别 module
mainFields: ['main', 'module', 'exports'],
},
server: {
hmr: {
port: undefined,
},
},
test: {
testTimeout: 10 * 1000,
pool: 'forks',
poolOptions: {
forks: {
maxForks: 32,
minForks: 1,
},
},
sequence: {
// vitest 2.0之后,所有钩子默认串行运行
hooks: 'parallel',
},
globals: true,
mockReset: false,
silent: process.env.CI === 'true',
coverage: {
// 逐步对各包开启
all: false,
include: ['src/**/*.ts', 'src/**/*.tsx'],
exclude: coverageConfigDefaults.exclude,
provider: 'v8',
reporter: ['cobertura', 'text', 'html', 'clover', 'json', 'json-summary'],
},
},
};

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { mergeConfig } from 'vitest/config';
import { defaultVitestConfig } from './preset-default';
export const nodePreset = mergeConfig(defaultVitestConfig, {});

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { mergeConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import { defaultVitestConfig } from './preset-default';
export const webPreset = mergeConfig(defaultVitestConfig, {
plugins: [react()],
test: {
environment: 'happy-dom',
framework: {
hmr: 'page',
},
},
});

View File

@@ -0,0 +1,17 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// TODO: should be remove