feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
98
frontend/config/vitest-config/src/define-config.ts
Normal file
98
frontend/config/vitest-config/src/define-config.ts
Normal 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);
|
||||
};
|
||||
6
frontend/config/vitest-config/src/index.js
Normal file
6
frontend/config/vitest-config/src/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
require('sucrase/register/ts');
|
||||
|
||||
process.env.VITE_CJS_IGNORE_WARNING = true;
|
||||
const { defineConfig } = require('./define-config');
|
||||
|
||||
module.exports = { defineConfig };
|
||||
56
frontend/config/vitest-config/src/preset-default.ts
Normal file
56
frontend/config/vitest-config/src/preset-default.ts
Normal 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'],
|
||||
},
|
||||
},
|
||||
};
|
||||
21
frontend/config/vitest-config/src/preset-node.ts
Normal file
21
frontend/config/vitest-config/src/preset-node.ts
Normal 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, {});
|
||||
30
frontend/config/vitest-config/src/preset-web.ts
Normal file
30
frontend/config/vitest-config/src/preset-web.ts
Normal 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',
|
||||
},
|
||||
},
|
||||
});
|
||||
17
frontend/config/vitest-config/src/tsc-only.ts
Normal file
17
frontend/config/vitest-config/src/tsc-only.ts
Normal 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
|
||||
Reference in New Issue
Block a user