From 719c3de3bc45da44960c08f6cb733ea046695a39 Mon Sep 17 00:00:00 2001 From: harpsealjs Date: Mon, 22 Jul 2024 16:06:36 +0800 Subject: [PATCH] test: add test case for runtime condition (#7250) --- .../runtime/runtime-condition/a.js | 25 +++++++++++ .../runtime/runtime-condition/ax.js | 6 +++ .../runtime/runtime-condition/b.js | 9 ++++ .../runtime/runtime-condition/bx.js | 8 ++++ .../runtime-condition/concatenated-shared.js | 1 + .../runtime/runtime-condition/dep.js | 1 + .../runtime/runtime-condition/dep2.js | 1 + .../runtime-condition/rspack.config.js | 44 +++++++++++++++++++ .../runtime/runtime-condition/shared.js | 16 +++++++ .../runtime/runtime-condition/super.js | 1 + .../runtime/runtime-condition/test.config.js | 9 ++++ 11 files changed, 121 insertions(+) create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/a.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/ax.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/b.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/bx.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/concatenated-shared.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep2.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/shared.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/super.js create mode 100644 packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/test.config.js diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/a.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/a.js new file mode 100644 index 00000000000..8ba395b98e6 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/a.js @@ -0,0 +1,25 @@ +import { val, val2b } from "./shared"; + +it("should have correct runtime id", () => { + expect(val).toBe(84); + expect(val2b).toBe(42); + expect(__webpack_require__.j).toBe("a-runtime"); +}); + +it("should include runtime condition check code", () => { + const fs = __non_webpack_require__("fs"); + const path = __non_webpack_require__("path"); + const url = __non_webpack_require__("url"); + + const __filename = url.fileURLToPath(import.meta.url); + const source = fs.readFileSync( + path.join( + __filename, + "../../../../js/config/runtime/runtime-condition/shared.js" + ), + "utf-8" + ); + expect(source).toContain(`"a-runtime" == __webpack_require__.j`) + expect(source).toContain(`"b-runtime" == __webpack_require__.j`); + expect(source).toContain(`/^[ab]x\\-name$/.test(__webpack_require__.j)`); +}) \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/ax.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/ax.js new file mode 100644 index 00000000000..a23f0be6c8b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/ax.js @@ -0,0 +1,6 @@ +import { val, val2b } from "./concatenated-shared"; + +it("should have the correct value", () => { + expect(val).toBe(84); + expect(val2b).toBe(42); +}); diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/b.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/b.js new file mode 100644 index 00000000000..590a2c85457 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/b.js @@ -0,0 +1,9 @@ +import { other, val2c, Test } from "./shared"; + +it("should have correct runtime id", () => { + expect(other).toBe("other"); + expect(val2c).toBe(42); + expect(Test).toBeTypeOf("function"); + expect(new Test()).toBeInstanceOf(Test); + expect(__webpack_require__.j).toBe("b-runtime"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/bx.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/bx.js new file mode 100644 index 00000000000..1d24b378858 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/bx.js @@ -0,0 +1,8 @@ +import { other, val2c, Test } from "./concatenated-shared"; + +it("should have the correct value", () => { + expect(other).toBe("other"); + expect(val2c).toBe(42); + expect(Test).toBeTypeOf("function"); + expect(new Test()).toBeInstanceOf(Test); +}); diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/concatenated-shared.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/concatenated-shared.js new file mode 100644 index 00000000000..42feac2d697 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/concatenated-shared.js @@ -0,0 +1 @@ +export * from "./shared?1"; diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep.js new file mode 100644 index 00000000000..7a4e8a723a4 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep.js @@ -0,0 +1 @@ +export default 42; diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep2.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep2.js new file mode 100644 index 00000000000..888cae37af9 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/dep2.js @@ -0,0 +1 @@ +module.exports = 42; diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/rspack.config.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/rspack.config.js new file mode 100644 index 00000000000..a73d402de9b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/rspack.config.js @@ -0,0 +1,44 @@ +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: { + "a-name": { + import: "./a", + runtime: "a-runtime", + }, + "b-name": { + import: "./b", + runtime: "b-runtime", + }, + "ax-name": "./ax.js", + "bx-name": "./bx.js" + }, + target: "web", + output: { + filename: "[id].js", + }, + optimization: { + chunkIds: "named", + moduleIds: "named", + minimize: false, + usedExports: true, + concatenateModules: true, + splitChunks: { + cacheGroups: { + forceMerge: { + test: /shared/, + enforce: true, + name: "shared", + chunks: "all" + } + } + } + }, + module: { + rules: [ + { + test: /dep/, + sideEffects: false + } + ] + }, +}; diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/shared.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/shared.js new file mode 100644 index 00000000000..b621dff945c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/shared.js @@ -0,0 +1,16 @@ +import value from "./dep"; +import value2 from "./dep2"; +import * as dep2 from "./dep2"; +import Super from "./super"; + +const derived = value; + +export const val = /*#__PURE__*/ (() => value + derived)(); + +export const val2a = value2; +export const val2b = value2; +export const val2c = value2; + +export const other = "other"; + +export class Test extends Super {} diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/super.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/super.js new file mode 100644 index 00000000000..d470a77c21a --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/super.js @@ -0,0 +1 @@ +export default class Super {} diff --git a/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/test.config.js b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/test.config.js new file mode 100644 index 00000000000..feb8f8dd994 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/runtime/runtime-condition/test.config.js @@ -0,0 +1,9 @@ +module.exports = { + findBundle: function (i, options) { + return [ + "a-runtime.js", + "shared.js", + "a-name.js", + ]; + } +};