2020-05-22 14:36:39 +03:00
|
|
|
const swc = require("../../../");
|
2021-10-27 01:39:06 +03:00
|
|
|
const path = require("path");
|
2020-05-22 14:36:39 +03:00
|
|
|
|
|
|
|
it("should handle minify", () => {
|
|
|
|
const src = '/* Comment */import foo, {bar} from "foo"';
|
|
|
|
|
|
|
|
expect(
|
|
|
|
swc
|
|
|
|
.transformSync(src, {
|
|
|
|
minify: true
|
|
|
|
})
|
|
|
|
.code.trim()
|
2021-08-03 18:52:47 +03:00
|
|
|
).toBe(`import foo,{bar}from"foo"`);
|
2020-05-22 14:36:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle sourceMaps = false", () => {
|
|
|
|
const src = '/* Comment */import foo, {bar} from "foo"';
|
|
|
|
const out = swc.transformSync(src, {
|
|
|
|
sourceMaps: false
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.map).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle exportNamespaceFrom", () => {
|
|
|
|
const src = "export * as Foo from 'bar';";
|
|
|
|
const out = swc.transformSync(src, {
|
|
|
|
jsc: {
|
|
|
|
parser: {
|
|
|
|
syntax: "ecmascript",
|
|
|
|
exportNamespaceFrom: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code).toContain("import * as _Foo from 'bar';");
|
|
|
|
expect(out.code).toContain("export { _Foo as Foo }");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("should handle jsc.target = es5", () => {
|
|
|
|
const out = swc.transformSync(`foo.default`, {
|
|
|
|
swcrc: false,
|
|
|
|
jsc: {
|
|
|
|
target: "es5"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(out.code.trim()).toBe(`foo.default;`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("(sync) should handle module input", () => {
|
|
|
|
const m = swc.parseSync("class Foo {}");
|
|
|
|
const out = swc.transformSync(m, {
|
|
|
|
swcrc: false,
|
|
|
|
jsc: {
|
|
|
|
target: 'es2016',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code.replace(/\n/g, "")).toBe("class Foo {}");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("(async) should handle module input", async () => {
|
|
|
|
const m = await swc.parse("class Foo {}");
|
|
|
|
const out = await swc.transform(m, {
|
|
|
|
jsc: {
|
|
|
|
target: "es2016"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code.replace(/\n/g, "")).toBe("class Foo {}");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("(sync) should handle plugin", () => {
|
|
|
|
const out = swc.transformSync("class Foo {}", {
|
2021-01-29 16:19:04 +03:00
|
|
|
plugin: m => ({ ...m, body: [] })
|
2020-05-22 14:36:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code).toBe("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("(async) should handle plugin", async () => {
|
|
|
|
const out = await swc.transform("class Foo {}", {
|
2021-01-29 16:19:04 +03:00
|
|
|
plugin: m => ({ ...m, body: [] })
|
2020-05-22 14:36:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code).toBe("");
|
|
|
|
});
|
|
|
|
|
2021-10-19 09:37:58 +03:00
|
|
|
it("(async) should handle dynamic import", async () => {
|
2020-05-22 14:36:39 +03:00
|
|
|
const out = await swc.transform("import('foo');", {
|
|
|
|
jsc: {
|
|
|
|
target: "es3",
|
|
|
|
parser: {
|
|
|
|
syntax: "ecmascript",
|
|
|
|
dynamicImport: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code.replace(/;/g, "").trim()).toBe(`import('foo')`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle nullish coalescing", async () => {
|
|
|
|
const out = await swc.transform("a ?? 'foo';", {
|
|
|
|
jsc: {
|
|
|
|
parser: {
|
|
|
|
syntax: "ecmascript",
|
|
|
|
nullishCoalescing: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(out.code).toBe(`a !== null && a !== void 0 ? a : 'foo';
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle for of statement in an async function", async () => {
|
|
|
|
const out = swc.transformSync(
|
|
|
|
`async function foo() {
|
|
|
|
for (let a of b) {
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.code).toBeTruthy()
|
|
|
|
});
|
2021-01-29 16:19:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
it("should respect isModule = false", async () => {
|
|
|
|
const out = swc.transformSync(
|
|
|
|
`const five = 005`,
|
|
|
|
{
|
|
|
|
isModule: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(out.code.trim()).toEqual(`var five = 5;`)
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should respect isModule = true", async () => {
|
|
|
|
const f = () => swc.transformSync(
|
|
|
|
`const five = 005`,
|
|
|
|
{
|
|
|
|
isModule: true,
|
|
|
|
}
|
|
|
|
);
|
2021-10-19 12:49:18 +03:00
|
|
|
expect(f).toThrowError(/Syntax Error/)
|
2021-01-29 16:19:04 +03:00
|
|
|
});
|
2021-09-14 05:31:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it("should respect `inlineSourcesContent`", async () => {
|
|
|
|
const src = '/* Comment */import foo, {bar} from "foo"';
|
|
|
|
const { map } = await swc.transform(src, {
|
|
|
|
sourceMaps: true,
|
|
|
|
inlineSourcesContent: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const j = JSON.parse(map);
|
|
|
|
|
|
|
|
expect(j).toHaveProperty('sourcesContent')
|
2021-10-21 07:45:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should respect `error.filename = false`", async () => {
|
|
|
|
const src = 'export default <h1>';
|
|
|
|
try {
|
|
|
|
await swc.transform(src, {
|
|
|
|
error: {
|
|
|
|
filename: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
expect(e).not.toContain("-->")
|
|
|
|
}
|
|
|
|
|
2021-10-27 01:39:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should merge parser config", async () => {
|
|
|
|
const filename = path.resolve(
|
|
|
|
__dirname + "/../../tests/issue-2546/input.ts"
|
|
|
|
);
|
|
|
|
|
|
|
|
const { code } = await swc.transformFile(filename, {
|
|
|
|
jsc: {
|
|
|
|
parser: {
|
|
|
|
syntax: "typescript",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(code).not.toBeFalsy()
|
2021-09-14 05:31:32 +03:00
|
|
|
});
|