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 @@
__tests__/outputs

View File

@@ -0,0 +1,68 @@
# @coze-arch/pkg-root-webpack-plugin
> 用于支持 `@` 根目录引用的插件
## Overview
This package is part of the Coze Studio monorepo and provides architecture functionality. It includes hook, plugin.
## Getting Started
### Installation
Add this package to your `package.json`:
```json
{
"dependencies": {
"@coze-arch/pkg-root-webpack-plugin": "workspace:*"
}
}
```
Then run:
```bash
rush update
```
### Usage
```typescript
import { /* exported functions/components */ } from '@coze-arch/pkg-root-webpack-plugin';
// Example usage
// TODO: Add specific usage examples
```
## Features
- Hook
- Plugin
## API Reference
### Exports
- `default PkgRootWebpackPlugin;`
- `PkgRootWebpackPlugin ;`
For detailed API documentation, please refer to the TypeScript definitions.
## Development
This package is built with:
- TypeScript
- Modern JavaScript
- Vitest for testing
- ESLint for code quality
## Contributing
This package is part of the Coze Studio monorepo. Please follow the monorepo contribution guidelines.
## License
Apache-2.0

View File

@@ -0,0 +1,169 @@
/*
* 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 { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { RushConfiguration } from '@rushstack/rush-sdk';
import OriginPkgRootWebpackPlugin from '@coze-arch/pkg-root-webpack-plugin-origin';
import PkgRootWebpackPlugin from '../src/index';
// Mock @coze-arch/pkg-root-webpack-plugin-origin
vi.mock('@coze-arch/pkg-root-webpack-plugin-origin', () => ({
default: vi.fn().mockImplementation(function (this: any, options: any) {
this.options = options;
}),
}));
// Mock @rushstack/rush-sdk
vi.mock('@rushstack/rush-sdk', () => ({
RushConfiguration: {
loadFromDefaultLocation: vi.fn(),
},
}));
describe('PkgRootWebpackPlugin', () => {
const mockRushConfiguration = {
projects: [
{ projectFolder: 'packages/project1' },
{ projectFolder: 'packages/project2' },
{ projectFolder: 'apps/app1' },
{ projectFolder: 'apps/app2' },
],
};
beforeEach(() => {
vi.clearAllMocks();
(RushConfiguration.loadFromDefaultLocation as any).mockReturnValue(
mockRushConfiguration,
);
});
afterEach(() => {
vi.resetAllMocks();
});
it('应该正确导出 PkgRootWebpackPlugin 类', () => {
expect(PkgRootWebpackPlugin).toBeDefined();
expect(typeof PkgRootWebpackPlugin).toBe('function');
});
it('应该能够创建 PkgRootWebpackPlugin 实例', () => {
const plugin = new PkgRootWebpackPlugin();
expect(plugin).toBeInstanceOf(PkgRootWebpackPlugin);
expect(plugin).toBeInstanceOf(OriginPkgRootWebpackPlugin);
});
it('应该使用默认配置创建插件', () => {
new PkgRootWebpackPlugin();
expect(OriginPkgRootWebpackPlugin).toHaveBeenCalledWith({
root: '@',
packagesDirs: [
'packages/project1',
'packages/project2',
'apps/app1',
'apps/app2',
],
excludeFolders: [],
});
});
it('应该能够合并用户提供的配置选项', () => {
const customOptions = {
customProp: 'customValue',
excludeFolders: ['custom-exclude'],
};
new PkgRootWebpackPlugin(customOptions);
// 注意Object.assign 中后面的对象会覆盖前面的对象,所以默认配置会覆盖用户配置
expect(OriginPkgRootWebpackPlugin).toHaveBeenCalledWith({
customProp: 'customValue',
root: '@', // 被默认值覆盖
packagesDirs: [
'packages/project1',
'packages/project2',
'apps/app1',
'apps/app2',
],
excludeFolders: [], // 被默认值覆盖
});
});
it('默认配置会覆盖用户提供的 root 配置', () => {
const customOptions = {
root: '$',
};
new PkgRootWebpackPlugin(customOptions);
// Object.assign 的行为:后面的对象会覆盖前面的对象属性
expect(OriginPkgRootWebpackPlugin).toHaveBeenCalledWith({
root: '@', // 被默认值覆盖
packagesDirs: [
'packages/project1',
'packages/project2',
'apps/app1',
'apps/app2',
],
excludeFolders: [],
});
});
it('应该能够处理空的配置选项', () => {
new PkgRootWebpackPlugin({});
expect(OriginPkgRootWebpackPlugin).toHaveBeenCalledWith({
root: '@',
packagesDirs: [
'packages/project1',
'packages/project2',
'apps/app1',
'apps/app2',
],
excludeFolders: [],
});
});
it('验证所有导出都正确', () => {
// 验证模块导出了正确的类和默认导出
expect(PkgRootWebpackPlugin).toBeDefined();
expect(typeof PkgRootWebpackPlugin).toBe('function');
});
it('应该正确处理 Rush 配置中的项目文件夹', () => {
new PkgRootWebpackPlugin();
// 验证传递给父类的 packagesDirs 包含所有项目文件夹
const call = (OriginPkgRootWebpackPlugin as any).mock.calls[0];
const options = call[0];
expect(options.packagesDirs).toEqual([
'packages/project1',
'packages/project2',
'apps/app1',
'apps/app2',
]);
});
it('测试插件基本功能正常工作', () => {
// 这个测试验证插件能正常实例化并调用父类构造函数
new PkgRootWebpackPlugin();
// 验证确实调用了父类构造函数
expect(OriginPkgRootWebpackPlugin).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,16 @@
{
"operationSettings": [
{
"operationName": "build",
"outputFolderNames": []
},
{
"operationName": "test:cov",
"outputFolderNames": ["coverage"]
},
{
"operationName": "ts-check",
"outputFolderNames": []
}
]
}

View File

@@ -0,0 +1,5 @@
{
"codecov": {
"coverage": 70
}
}

View File

@@ -0,0 +1,18 @@
const { defineConfig } = require('@coze-arch/eslint-config');
module.exports = defineConfig({
preset: 'node',
packageRoot: __dirname,
overrides: [
{
files: ['__tests__/**'],
rules: {
'@coze-arch/package-require-author': 'off',
'unicorn/filename-case': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/consistent-type-assertions': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
},
},
],
});

View File

@@ -0,0 +1,23 @@
/*
* 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 OriginPkgRootWebpackPlugin from '@coze-arch/pkg-root-webpack-plugin-origin';
type PkgRootWebpackPluginOptions = Record<string, unknown>;
declare class PkgRootWebpackPlugin extends OriginPkgRootWebpackPlugin {
constructor(options?: Partial<PkgRootWebpackPluginOptions>);
}
export default PkgRootWebpackPlugin;
export { PkgRootWebpackPlugin };

View File

@@ -0,0 +1,33 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PkgRootWebpackPlugin = void 0;
const rush_sdk_1 = require("@rushstack/rush-sdk");
const getRushConfiguration = (() => {
let rushConfig;
return () => {
if (!rushConfig) {
rushConfig = rush_sdk_1.RushConfiguration.loadFromDefaultLocation({});
}
return rushConfig;
};
})();
const pkg_root_webpack_plugin_origin_1 = __importDefault(require("@coze-arch/pkg-root-webpack-plugin-origin"));
class PkgRootWebpackPlugin extends pkg_root_webpack_plugin_origin_1.default {
constructor(options) {
const rushJson = getRushConfiguration();
const rushJsonPackagesDir = rushJson.projects.map(item => item.projectFolder);
// .filter(item => !item.includes('/apps/'));
const mergedOptions = Object.assign({}, options || {}, {
root: '@',
packagesDirs: rushJsonPackagesDir,
// 排除apps/*,减少处理时间
excludeFolders: [],
});
super(mergedOptions);
}
}
exports.PkgRootWebpackPlugin = PkgRootWebpackPlugin;
exports.default = PkgRootWebpackPlugin;

View File

@@ -0,0 +1,36 @@
{
"name": "@coze-arch/pkg-root-webpack-plugin",
"version": "1.0.0",
"description": "> 用于支持 `@` 根目录引用的插件",
"keywords": [],
"license": "Apache-2.0",
"author": "fanwenjie.fe@bytedance.com",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc -b ./tsconfig.build.json --force",
"dev": "tsc -w",
"lint": "eslint ./ --cache",
"test": "vitest --run --passWithNoTests",
"test:cov": "npm run test -- --coverage"
},
"dependencies": {
"@coze-arch/pkg-root-webpack-plugin-origin": "npm:@coze-arch/pkg-root-webpack-plugin@1.0.0-alpha.74b402",
"@rushstack/rush-sdk": "5.100.2"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@coze-arch/eslint-config": "workspace:*",
"@coze-arch/ts-config": "workspace:*",
"@coze-arch/vitest-config": "workspace:*",
"@types/micromatch": "^4.0.1",
"@types/node": "^18",
"@vitest/coverage-v8": "~3.0.5",
"babel-loader": "~9.1.0",
"enhanced-resolve": "~5.12.0",
"pkg-install": "~1.0.0",
"vitest": "~3.0.5",
"webpack": "~5.89.0"
}
}

View File

@@ -0,0 +1,53 @@
/*
* 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 { RushConfiguration } from '@rushstack/rush-sdk';
const getRushConfiguration = (() => {
let rushConfig: RushConfiguration;
return () => {
if (!rushConfig) {
rushConfig = RushConfiguration.loadFromDefaultLocation({});
}
return rushConfig;
};
})();
import OriginPkgRootWebpackPlugin from '@coze-arch/pkg-root-webpack-plugin-origin';
type PkgRootWebpackPluginOptions = Record<string, unknown>;
class PkgRootWebpackPlugin extends OriginPkgRootWebpackPlugin {
constructor(options?: Partial<PkgRootWebpackPluginOptions>) {
const rushJson = getRushConfiguration();
const rushJsonPackagesDir = rushJson.projects.map(
item => item.projectFolder,
);
// .filter(item => !item.includes('/apps/'));
const mergedOptions = Object.assign({}, options || {}, {
root: '@',
packagesDirs: rushJsonPackagesDir,
// 排除apps/*,减少处理时间
excludeFolders: [],
});
super(mergedOptions);
}
}
export default PkgRootWebpackPlugin;
export { PkgRootWebpackPlugin };

View File

@@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"compilerOptions": {
"sourceMap": false,
"types": ["vitest/globals", "node"],
"strictNullChecks": true,
"outDir": "lib",
"rootDir": "src",
"tsBuildInfoFile": "lib/tsconfig.build.tsbuildinfo"
},
"include": ["./src"],
"references": [
{
"path": "../../../config/eslint-config/tsconfig.build.json"
},
{
"path": "../../../config/ts-config/tsconfig.build.json"
},
{
"path": "../../../config/vitest-config/tsconfig.build.json"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"composite": true
},
"references": [
{
"path": "./tsconfig.build.json"
},
{
"path": "./tsconfig.misc.json"
}
],
"exclude": ["**/*"]
}

View File

@@ -0,0 +1,18 @@
{
"extends": "@coze-arch/ts-config/tsconfig.node.json",
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["./__tests__", "vitest.config.ts"],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
],
"compilerOptions": {
"sourceMap": false,
"types": ["vitest/globals", "node"],
"strictNullChecks": true,
"rootDir": "./",
"outDir": "./dist"
}
}

View File

@@ -0,0 +1,33 @@
/*
* 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.
*/
// FIXME: Unable to resolve path to module 'vitest/config'
import { defaultExclude } from 'vitest/config';
import { defineConfig } from '@coze-arch/vitest-config';
export default defineConfig({
preset: 'node',
dirname: __dirname,
test: {
testTimeout: 30 * 1000,
globals: true,
mockReset: false,
coverage: {
provider: 'v8',
exclude: ['.eslintrc.js', 'lib', ...defaultExclude],
},
},
});