2022-05-11 08:28:40 +03:00
|
|
|
import swc from "../../..";
|
|
|
|
import { dirname, join } from "path";
|
|
|
|
import { platform } from "os";
|
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
|
|
it("should override react", async () => {
|
2022-06-27 15:32:06 +03:00
|
|
|
const { code } = await swc.transform(
|
|
|
|
`export default function foo() {
|
2022-05-11 08:28:40 +03:00
|
|
|
return <div>Hello</div>;
|
2022-06-27 15:32:06 +03:00
|
|
|
}`,
|
|
|
|
{
|
|
|
|
jsc: {
|
|
|
|
target: "es2017",
|
|
|
|
transform: {
|
|
|
|
react: {
|
|
|
|
runtime: "automatic",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
parser: {
|
|
|
|
syntax: "typescript",
|
|
|
|
tsx: true,
|
|
|
|
dts: true,
|
|
|
|
dynamicImport: true,
|
|
|
|
},
|
|
|
|
externalHelpers: true,
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
type: "es6",
|
|
|
|
},
|
2022-05-11 08:28:40 +03:00
|
|
|
}
|
2022-06-27 15:32:06 +03:00
|
|
|
);
|
|
|
|
expect(code).toMatchInlineSnapshot(`
|
|
|
|
"import { jsx as _jsx } from \\"react/jsx-runtime\\";
|
|
|
|
export default function foo() {
|
|
|
|
return /*#__PURE__*/ _jsx(\\"div\\", {
|
|
|
|
children: \\"Hello\\"
|
|
|
|
});
|
2022-09-14 11:24:15 +03:00
|
|
|
}
|
2022-06-27 15:32:06 +03:00
|
|
|
"
|
|
|
|
`);
|
2022-05-11 08:28:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should merge correctly", async () => {
|
2022-06-27 15:32:06 +03:00
|
|
|
const filename = join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"tests",
|
|
|
|
"issue-4606",
|
|
|
|
"1",
|
|
|
|
"index.tsx"
|
|
|
|
);
|
|
|
|
console.log(filename);
|
|
|
|
const { code } = await swc.transformFile(filename, {
|
|
|
|
jsc: {
|
|
|
|
parser: {
|
|
|
|
syntax: "typescript",
|
|
|
|
tsx: true,
|
|
|
|
dts: true,
|
|
|
|
dynamicImport: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
type: "es6",
|
|
|
|
},
|
2022-05-11 08:28:40 +03:00
|
|
|
});
|
2022-06-27 15:32:06 +03:00
|
|
|
// It should transpile react jsx with automatic runtime
|
|
|
|
expect(code).toMatchInlineSnapshot(`
|
|
|
|
"import { jsx as _jsx } from \\"react/jsx-runtime\\";
|
|
|
|
export default function foo() {
|
|
|
|
return /*#__PURE__*/ _jsx(\\"div\\", {
|
|
|
|
children: \\"Hello\\"
|
|
|
|
});
|
2022-09-14 11:24:15 +03:00
|
|
|
}
|
2022-06-27 15:32:06 +03:00
|
|
|
"
|
|
|
|
`);
|
2022-05-11 10:08:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("module config should merged correctly", async () => {
|
2022-06-27 15:32:06 +03:00
|
|
|
const filename = join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"tests",
|
|
|
|
"issue-4606",
|
|
|
|
"2",
|
|
|
|
"index.tsx"
|
|
|
|
);
|
|
|
|
console.log(filename);
|
|
|
|
const { code } = await swc.transformFile(filename, {
|
|
|
|
module: {
|
|
|
|
type: "commonjs",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// It should transpile react jsx with automatic runtime
|
|
|
|
expect(code).toMatchInlineSnapshot(`
|
2022-09-14 11:24:15 +03:00
|
|
|
"\\"use strict\\";
|
|
|
|
Object.defineProperty(exports, \\"__esModule\\", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
Object.defineProperty(exports, \\"default\\", {
|
|
|
|
enumerable: true,
|
2023-04-15 16:57:00 +03:00
|
|
|
get: function() {
|
|
|
|
return foo;
|
|
|
|
}
|
2022-09-14 11:24:15 +03:00
|
|
|
});
|
2023-03-31 09:15:21 +03:00
|
|
|
const _jsxruntime = require(\\"react/jsx-runtime\\");
|
2022-09-14 11:24:15 +03:00
|
|
|
function foo() {
|
2023-03-31 09:15:21 +03:00
|
|
|
return /*#__PURE__*/ (0, _jsxruntime.jsx)(\\"div\\", {
|
2022-09-14 11:24:15 +03:00
|
|
|
children: \\"Hello\\"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"
|
|
|
|
`);
|
2022-06-27 15:32:06 +03:00
|
|
|
});
|