2022-03-16 17:25:26 +03:00
|
|
|
import swc from "../..";
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should compress", async () => {
|
|
|
|
const { code } = await swc.minify(`
|
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
|
|
|
`);
|
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
|
|
|
`"import foo from\\"@src/app\\";console.log(foo)"`
|
|
|
|
);
|
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should accept object", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
|
|
|
`"import foo from\\"@src/app\\";console.log(foo)"`
|
|
|
|
);
|
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should accpept { mangle = true }", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: true,
|
|
|
|
}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
|
|
|
`"import a from\\"@src/app\\";console.log(a)"`
|
|
|
|
);
|
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should accpept { mangle = object }", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
import foo from '@src/app';
|
|
|
|
console.log(foo)
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
|
|
|
`"import a from\\"@src/app\\";console.log(a)"`
|
|
|
|
);
|
|
|
|
});
|
2021-07-31 09:30:06 +03:00
|
|
|
|
|
|
|
it("should mangle locals", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.minify(
|
|
|
|
`
|
2021-07-31 09:30:06 +03:00
|
|
|
(function(){
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-07-31 09:30:06 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(code).toMatchInlineSnapshot(
|
|
|
|
`"(function(){const a=Math.random()+\\"_\\"+Math.random();console.log(a);console.log(a);console.log(a);console.log(a);console.log(a);console.log(a)})()"`
|
|
|
|
);
|
|
|
|
});
|
2021-08-03 18:52:47 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
describe("soruce map", () => {
|
2021-08-26 13:44:38 +03:00
|
|
|
it("should have `names`", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { map } = await swc.minify(
|
|
|
|
`
|
2021-08-26 13:44:38 +03:00
|
|
|
(function(){
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
sourceMap: true,
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-08-26 13:44:38 +03:00
|
|
|
|
|
|
|
expect(JSON.parse(map)).toHaveProperty("names");
|
2022-03-16 17:25:26 +03:00
|
|
|
expect(JSON.parse(map).names).not.toEqual([]);
|
2021-08-26 13:44:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should have `sources` if file name is speicified", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { map } = await swc.minify(
|
|
|
|
{
|
|
|
|
"foo.js": `(function(){
|
2021-08-26 13:44:38 +03:00
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
2022-03-16 17:25:26 +03:00
|
|
|
})()`,
|
2021-08-26 13:44:38 +03:00
|
|
|
},
|
2022-03-16 17:25:26 +03:00
|
|
|
{
|
|
|
|
sourceMap: true,
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-08-26 13:44:38 +03:00
|
|
|
|
|
|
|
const j = JSON.parse(map);
|
|
|
|
expect(j).toHaveProperty("sources");
|
|
|
|
expect(j.sources).not.toEqual([]);
|
|
|
|
});
|
2022-03-16 17:25:26 +03:00
|
|
|
});
|
2021-08-03 18:52:47 +03:00
|
|
|
|
2022-03-16 17:25:26 +03:00
|
|
|
describe("transform apis", () => {
|
2021-08-03 18:52:47 +03:00
|
|
|
it("handle jsc.minify", async () => {
|
2022-03-16 17:25:26 +03:00
|
|
|
const { code } = await swc.transform(
|
|
|
|
`
|
2021-08-03 18:52:47 +03:00
|
|
|
(function(){
|
|
|
|
const longName = Math.random() + '_' + Math.random();
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
console.log(longName);
|
|
|
|
})()
|
2022-03-16 17:25:26 +03:00
|
|
|
`,
|
|
|
|
{
|
|
|
|
jsc: {
|
|
|
|
minify: {
|
|
|
|
compress: false,
|
|
|
|
mangle: {
|
|
|
|
topLevel: true,
|
|
|
|
},
|
2021-08-03 18:52:47 +03:00
|
|
|
},
|
2022-03-16 17:25:26 +03:00
|
|
|
},
|
|
|
|
minify: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(code).toMatchInlineSnapshot(
|
|
|
|
`"(function(){var a=Math.random()+\\"_\\"+Math.random();console.log(a);console.log(a);console.log(a);console.log(a);console.log(a);console.log(a)})()"`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|