Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rspack test tools diff #4668

Merged
merged 9 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/reusable-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ jobs:
if: ${{ inputs.target == 'x86_64-pc-windows-msvc' && matrix.node != '14' }}
run: pnpm run test:ci

### Diff with webpack
- name: Diff test
timeout-minutes: 15
run: pnpm run test:diff

### write the latest metric into branch gh-pages
### Note that, We can't merge this script, because this script only runs on main branch
- name: Update main branch test compatibility metric
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"build:binding:release": "pnpm --filter @rspack/binding run build:release",
"prepare": "is-ci || husky install",
"test:example": "pnpm --filter \"example-*\" build",
"test:diff": "pnpm --filter \"@rspack/*\" test:diff",
"test:unit": "pnpm --filter \"@rspack/*\" test",
"test:e2e": "pnpm --filter \"@rspack-e2e/*\" test",
"test:ci": "cross-env NODE_OPTIONS=--max_old_space_size=8192 pnpm run build:js && pnpm run test:example && pnpm run test:unit && pnpm test:webpack",
Expand Down
1 change: 1 addition & 0 deletions packages/rspack-test-tools/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @rspack/test-tools
22 changes: 22 additions & 0 deletions packages/rspack-test-tools/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2022-present Bytedance, Inc. and its affiliates.


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions packages/rspack-test-tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://lf3-static.bytednsdoc.com/obj/eden-cn/rjhwzy/ljhwZthlaukjlkulzlp/rspack-banner-1610-dark.png">
<img alt="Rspack Banner" src="https://lf3-static.bytednsdoc.com/obj/eden-cn/rjhwzy/ljhwZthlaukjlkulzlp/rspack-banner-1610.png">
</picture>

# @rspack/test-tools

Test tools for rspack.

## Documentation

See [https://rspack.dev](https://rspack.dev) for details.

## License

Rspack is [MIT licensed](https://github.com/web-infra-dev/rspack/blob/main/LICENSE).
59 changes: 59 additions & 0 deletions packages/rspack-test-tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "@rspack/test-tools",
"version": "0.3.12",
"license": "MIT",
"description": "Test tools for rspack",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"build": "rimraf dist/ && tsc -b ./tsconfig.build.json --force",
"dev": "tsc -b -w"
},
"files": [
"client",
"dist"
],
"publishConfig": {
"access": "public"
},
"homepage": "https://rspack.dev",
"bugs": "https://github.com/web-infra-dev/rspack/issues",
"repository": {
"type": "git",
"url": "https://github.com/web-infra-dev/rspack",
"directory": "packages/rspack-test-tools"
},
"dependencies": {
"@babel/generator": "7.23.0",
"@babel/helpers": "7.23.2",
"@babel/parser": "7.23.0",
"@babel/template": "7.22.15",
"@babel/traverse": "7.23.2",
"@babel/types": "7.23.0",
"@rspack/core": "workspace:*",
"deepmerge": "^4.3.1",
"fs-extra": "^11.1.1",
"jest-diff": "^29.7.0",
"webpack-sources": "3.2.3"
},
"devDependencies": {
"@types/webpack": "5.28.3",
"@types/webpack-sources": "3.2.0",
"webpack": "5.89.0"
},
"peerDependencies": {},
"peerDependenciesMeta": {},
"jest": {
"watchPathIgnorePatterns": [
"<rootDir>/dist",
"<rootDir>/tests/dist"
],
"testEnvironment": "../../scripts/test/patch-node-env.cjs"
}
}
178 changes: 178 additions & 0 deletions packages/rspack-test-tools/src/compare/compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { diffLinesRaw, diffStringsUnified } from "jest-diff";
import { IFormatCodeOptions, formatCode } from "./format-code";
import { replaceRuntimeModuleName } from "./replace-runtime-module-name";
import { parseModules } from "../helper";
import fs from "fs-extra";
import {
ECompareResultType,
TCompareModules,
TCompareResult,
TFileCompareResult,
TModuleCompareResult
} from "../type";

export interface ICompareOptions {
modules?: TCompareModules;
runtimeModules?: TCompareModules;
format: IFormatCodeOptions;
renameModule?: (name: string) => string;
}

export function compareFile(
sourceFile: string,
distFile: string,
compareOptions: ICompareOptions
): TFileCompareResult {
const result: TFileCompareResult = {
type: ECompareResultType.Same,
file: {
source: sourceFile,
dist: distFile
},
modules: {}
};
const sourceExists = fs.existsSync(sourceFile);
const distExists = fs.existsSync(distFile);
if (!sourceExists && !distExists) {
result.type = ECompareResultType.Missing;
return result;
} else if (!sourceExists && distExists) {
result.type = ECompareResultType.OnlyDist;
return result;
} else if (sourceExists && !distExists) {
result.type = ECompareResultType.OnlySource;
return result;
}

const sourceContent = replaceRuntimeModuleName(
fs.readFileSync(sourceFile, "utf-8")
);
const distContent = replaceRuntimeModuleName(
fs.readFileSync(distFile, "utf-8")
);

// const compareContentResult = compareContent(sourceContent, distContent);
// result.detail = compareContentResult.detail;
// result.lines = compareContentResult.lines;
result.type = ECompareResultType.Different;

const sourceModules = parseModules(sourceContent);
const distModules = parseModules(distContent);

for (let type of ["modules", "runtimeModules"]) {
const t = type as "modules" | "runtimeModules";
let moduleList: string[] = [];
if (compareOptions[t] === true) {
moduleList = [
...sourceModules[t].keys(),
...distModules[t].keys()
].filter((i, idx, arr) => arr.indexOf(i) === idx);
} else if (Array.isArray(compareOptions[t])) {
moduleList = compareOptions[t] as string[];
} else {
continue;
}
if (typeof compareOptions.renameModule === "function") {
moduleList = moduleList.map(compareOptions.renameModule);
}
result.modules[t] = compareModules(
moduleList,
sourceModules[t],
distModules[t],
compareOptions.format
);
}
return result;
}

export function compareModules(
modules: string[],
sourceModules: Map<string, string>,
distModules: Map<string, string>,
formatOptions: IFormatCodeOptions
) {
const compareResults: TModuleCompareResult[] = [];
for (let name of modules) {
const renamed = replaceRuntimeModuleName(name);
const sourceContent =
sourceModules.has(renamed) &&
formatCode(sourceModules.get(renamed)!, formatOptions);
const distContent =
distModules.has(renamed) &&
formatCode(distModules.get(renamed)!, formatOptions);

compareResults.push({
...compareContent(sourceContent, distContent),
name
});
}
return compareResults;
}

export function compareContent(
sourceContent: string | false,
distContent: string | false
): TCompareResult {
if (sourceContent) {
if (distContent) {
if (sourceContent === distContent) {
const lines = sourceContent.trim().split("\n").length;
return {
type: ECompareResultType.Same,
lines: {
source: lines,
common: lines,
dist: lines
}
};
} else {
const difference = diffStringsUnified(
sourceContent.trim(),
distContent.trim()
);
const diffLines = diffLinesRaw(
sourceContent.trim().split("\n"),
distContent.trim().split("\n")
);
return {
type: ECompareResultType.Different,
detail: difference,
lines: {
source: diffLines.filter(l => l[0] < 0).length,
common: diffLines.filter(l => l[0] === 0).length,
dist: diffLines.filter(l => l[0] > 0).length
}
};
}
} else {
return {
type: ECompareResultType.OnlySource,
lines: {
source: sourceContent.trim().split("\n").length,
common: 0,
dist: 0
}
};
}
} else {
if (distContent) {
return {
type: ECompareResultType.OnlyDist,
lines: {
source: 0,
common: 0,
dist: distContent.trim().split("\n").length
}
};
} else {
return {
type: ECompareResultType.Missing,
lines: {
source: 0,
common: 0,
dist: 0
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface IFormatCodeOptions {
replacements?: Record<string, string>;
ignorePropertyQuotationMark: boolean;
ignoreModuleId: boolean;
ignoreModuleArugments: boolean;
ignoreModuleArguments: boolean;
}

export function formatCode(raw: string, options: IFormatCodeOptions) {
Expand Down Expand Up @@ -39,7 +39,7 @@ export function formatCode(raw: string, options: IFormatCodeOptions) {
concise: false
}).code;

if (options.ignoreModuleArugments) {
if (options.ignoreModuleArguments) {
result = replaceModuleArgument(result);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/rspack-test-tools/src/compare/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./replace-module-argument";
export * from "./replace-runtime-module-name";
export * from "./format-code";
export * from "./compare";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function replaceModuleArgument(raw: string) {
return raw.trim().replace(/^\(function \([\w_,\s]+\) {/, "(function () {");
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,26 @@ const RUNTIME_MODULE_NAME_MAPPING = {

const RUNTIME_MODULE_PARAM_REGEX = {
"webpack/runtime/get_chunk_filename":
/webpack\/runtime\/get_chunk_filename\/([\w\.\-_\s]+)(\*\/)?/g,
/webpack\/runtime\/get_chunk_filename\/([\w.\-_\s]+)(\*\/)?/g,
"webpack/runtime/get_main_filename":
/webpack\/runtime\/get_main_filename\/([\w\.\-_\s]+)(\*\/)?/g,
/webpack\/runtime\/get_main_filename\/([\w.\-_\s]+)(\*\/)?/g,
"webpack/runtime/chunk_prefetch_function":
/webpack\/runtime\/chunk_prefetch_function\/([\w\.\-_\s]+)(\*\/)?/g
/webpack\/runtime\/chunk_prefetch_function\/([\w.\-_\s]+)(\*\/)?/g
};

export function replaceRuntimeModuleName(raw: string) {
for (let [rspackName, webpackName] of Object.entries(
RUNTIME_MODULE_NAME_MAPPING
)) {
if (RUNTIME_MODULE_PARAM_REGEX[rspackName]) {
if (
RUNTIME_MODULE_PARAM_REGEX[
rspackName as keyof typeof RUNTIME_MODULE_PARAM_REGEX
]
) {
raw = raw.replace(
RUNTIME_MODULE_PARAM_REGEX[rspackName],
RUNTIME_MODULE_PARAM_REGEX[
rspackName as keyof typeof RUNTIME_MODULE_PARAM_REGEX
],
(full, $1, $2) => {
return webpackName.replace("$1", $1.trim()) + ($2 ? " */" : "");
}
Expand Down
Loading
Loading